- Notifications
You must be signed in to change notification settings - Fork 261
challenges#18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
challenges #18
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -4,6 +4,8 @@ | ||
| # Вася: 1 | ||
| # Маша: 2 | ||
| # Петя: 2 | ||
| from collections import Counter | ||
| students = [ | ||
| {'first_name': 'Вася'}, | ||
| @@ -12,7 +14,14 @@ | ||
| {'first_name': 'Маша'}, | ||
| {'first_name': 'Петя'}, | ||
| ] | ||
| # ??? | ||
| def count_name(students): | ||
| students_names = Counter([student['first_name'] for student in students]) | ||
| return students_names | ||
| students_names = count_name(students) | ||
| for name, repeats in students_names.items(): | ||
| print(f'{name}: {repeats}') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Решение без count_students= {}
forstudentinstudents:
ifstudent['first_name'] notincount_students.keys():
count_students[student['first_name']] =1else:
count_students[student['first_name']] +=1print(count_students)
forname, countincount_students.items():
print(f"{name}: {count}") | ||
| # Задание 2 | ||
| @@ -26,7 +35,17 @@ | ||
| {'first_name': 'Маша'}, | ||
| {'first_name': 'Оля'}, | ||
| ] | ||
| # ??? | ||
| def name_max_value(students): | ||
| name_max_value = Counter(names).most_common(1) | ||
| return name_max_value[0][0] | ||
| names = count_name(students) | ||
| most_common_name = name_max_value(names) | ||
| print(f'Самое частое имя в классе: {most_common_name}') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Решение без students_list= [student['first_name'] forstudentinstudents]
print("Самое частое имя среди учеников:", max(set(students_list), key=students_list.count)) | ||
| # Задание 3 | ||
| @@ -51,7 +70,10 @@ | ||
| {'first_name': 'Саша'}, | ||
| ], | ||
| ] | ||
| # ??? | ||
| for i, school_class in enumerate(school_students): | ||
| names = count_name(school_class) | ||
| most_common_name = name_max_value(names) | ||
| print(f'Самое частое имя в классе {i+1}: {most_common_name}') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Переиспользование уже написанных функций хороший подход. fornum, studentsinenumerate(school_students, start=1):
students_list= [student['first_name'] forstudentinstudents]
print(f"Самое частое имя в классе {num}: {max(set(students_list), key=students_list.count)}")Почитай, попробуй разобраться как это работает, если не получится, напиши мне. | ||
| # Задание 4 | ||
| @@ -63,7 +85,7 @@ | ||
| school = [ | ||
| {'class': '2a', 'students': [{'first_name': 'Маша'}, {'first_name': 'Оля'}]}, | ||
| {'class': '2б', 'students': [{'first_name': 'Олег'}, {'first_name': 'Миша'}]}, | ||
| {'class': '2б', 'students': [{'first_name': 'Даша'}, {'first_name': 'Олег'}, {'first_name': 'Маша'}]}, | ||
| {'class': '2в', 'students': [{'first_name': 'Даша'}, {'first_name': 'Олег'}, {'first_name': 'Маша'}]}, | ||
| ] | ||
| is_male = { | ||
| 'Олег': True, | ||
| @@ -72,7 +94,21 @@ | ||
| 'Миша': True, | ||
| 'Даша': False, | ||
| } | ||
| # ??? | ||
| def students_gender(students): | ||
| gender = {'Male': 0, 'Female': 0} | ||
| for student in students: | ||
| name = student['first_name'] | ||
| if is_male[name]: | ||
| gender['Male'] += 1 | ||
| else: | ||
| gender['Female'] += 1 | ||
| return gender | ||
| for school_class in school: | ||
| gender = students_gender(school_class['students']) | ||
| class_name = school_class['class'] | ||
| print(f"Класс {class_name}: девочки {gender['Female']}, мальчики {gender['Male']}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ещё два способа решения для этой задачи для ознакомления: forclass_inschool:
count_boys=0count_girls=0forstudentinclass_['students']:
ifis_male[student['first_name']]:
count_boys+=1else:
count_girls+=1print(f"Класс {class_['class']}: девочки {count_girls}, мальчики {count_boys}")
print("альтернативный способ") # альтернативный способforclass_inschool:
female= [name['first_name'] fornameinclass_['students'] ifnotis_male[name['first_name']]]
male= [name['first_name'] fornameinclass_['students'] ifis_male[name['first_name']]]
print(f"Класс {class_['class']}: девочки {len(female)}, мальчики {len(male)}")There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Когда разберёшь, выполни 5-е задание | ||
| # Задание 5 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,36 @@ | ||
| # Вывести последнюю букву в слове | ||
| word = 'Архангельск' | ||
| # ??? | ||
| word = 'Архангельск' | ||
| print(word[-1]) | ||
| # Вывести количество букв "а" в слове | ||
| word = 'Архангельск' | ||
| # ??? | ||
| print(word.find('а')) | ||
| # Вывести количество гласных букв в слове | ||
| word = 'Архангельск' | ||
| # ??? | ||
| glas = set('аиеёоуыэюя') | ||
| word_set = set(word.lower()) | ||
| print(f'{len(word_set.intersection(glas))} гласных слове') | ||
| # Вывести количество слов в предложении | ||
| sentence = 'Мы приехали в гости' | ||
| # ??? | ||
| sentence = sentence.split(' ') | ||
| print(len(sentence)) | ||
| # Вывести первую букву каждого слова на отдельной строке | ||
| sentence = 'Мы приехали в гости' | ||
| # ??? | ||
| words = sentence.split(' ') | ||
| for word in words: | ||
| print(word[0]) | ||
| # Вывести усреднённую длину слова в предложении | ||
| sentence = 'Мы приехали в гости' | ||
| # ??? | ||
| words = sentence.split(' ') | ||
| avrg = sum(len(word) for word in words)/(len(words)) | ||
| print(avrg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Можно одной строкой в данном случае: