-
Notifications
You must be signed in to change notification settings - Fork 260
Homework #47
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?
Homework #47
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,7 +12,16 @@ | |||||||
| {'first_name': 'Маша'}, | ||||||||
| {'first_name': 'Петя'}, | ||||||||
| ] | ||||||||
| # ??? | ||||||||
| count_student = {} | ||||||||
| for student_dict in students: | ||||||||
| for key, name in student_dict.items(): | ||||||||
| if name in count_student: | ||||||||
| count_student[name] += 1 | ||||||||
| else: | ||||||||
| count_student[name] = 1 | ||||||||
| for name, count in count_student.items(): | ||||||||
| print(f'{name}: {count}') | ||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
| # Задание 2 | ||||||||
|
|
@@ -26,8 +35,15 @@ | |||||||
| {'first_name': 'Маша'}, | ||||||||
| {'first_name': 'Оля'}, | ||||||||
| ] | ||||||||
| # ??? | ||||||||
|
|
||||||||
| counter_student_name_max = {} | ||||||||
| for students_dict in students: | ||||||||
| for key, name in students_dict.items(): | ||||||||
|
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. видимо эта ошибка идёт через все задания :) |
||||||||
| if name in counter_student_name_max: | ||||||||
| counter_student_name_max[name] += 1 | ||||||||
| else: | ||||||||
| counter_student_name_max[name] = 1 | ||||||||
| name = sorted(counter_student_name_max.items(), key=lambda item: item[1], reverse=True) | ||||||||
|
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. как вариант, ещё можно использовать max(), он тоже умеет с аргументом key |
||||||||
| print(f'Самое частое имя среди учеников: {name[0][0]}') | ||||||||
|
|
||||||||
| # Задание 3 | ||||||||
| # Есть список учеников в нескольких классах, нужно вывести самое частое имя в каждом классе. | ||||||||
|
|
@@ -51,7 +67,24 @@ | |||||||
| {'first_name': 'Саша'}, | ||||||||
| ], | ||||||||
| ] | ||||||||
| # ??? | ||||||||
|
|
||||||||
| def max_count_name(arg, arg2): | ||||||||
|
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.
Suggested change
|
||||||||
| counter_student_name_max = {} | ||||||||
| for students_dict in arg: | ||||||||
| for key, name in students_dict.items(): | ||||||||
| if name in counter_student_name_max: | ||||||||
| counter_student_name_max[name] += 1 | ||||||||
| else: | ||||||||
| counter_student_name_max[name] = 1 | ||||||||
| name_student = sorted(counter_student_name_max.items(), key=lambda item: item[1], reverse=True) | ||||||||
|
|
||||||||
| return f'Самое частое имя в классе {arg2}: {name_student[0][0]}' | ||||||||
|
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. Вообще это законно, но я бы предложил возвращать просто name_student[0][0], а весь пост-процессинг делать за пределами функции. Таким образом мы немножко улучшим чистоту кода. |
||||||||
|
|
||||||||
|
|
||||||||
| number_class = 0 | ||||||||
| for school_class in school_students: | ||||||||
|
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.
Suggested change
|
||||||||
| number_class += 1 | ||||||||
| print(max_count_name(school_class, number_class)) | ||||||||
|
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.
Suggested change
|
||||||||
|
|
||||||||
|
|
||||||||
| # Задание 4 | ||||||||
|
|
@@ -72,7 +105,26 @@ | |||||||
| 'Миша': True, | ||||||||
| 'Даша': False, | ||||||||
| } | ||||||||
| # ??? | ||||||||
|
|
||||||||
| def student_counting(val, numb): | ||||||||
|
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.
Suggested change
|
||||||||
| counter_gender_student = {'men': 0, 'female': 0} | ||||||||
| for student_dict in val: | ||||||||
| for key, name in student_dict.items(): | ||||||||
| if is_male[name] is True: | ||||||||
| counter_gender_student['men'] += 1 | ||||||||
| else: | ||||||||
| counter_gender_student['female'] += 1 | ||||||||
| return f'Класс {numb}: девочки {counter_gender_student["female"]}, мальчики {counter_gender_student["men"]}' | ||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
| for classes in school: | ||||||||
| for key, value in classes.items(): | ||||||||
|
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. ага, здесь тоже просто по ключу берём из словаря то, что надо, цикл не нужен |
||||||||
| if type(value) is type(list()): | ||||||||
| value_list = value | ||||||||
| else: | ||||||||
| number_class = value | ||||||||
| print(student_counting(value_list, number_class)) | ||||||||
|
|
||||||||
|
|
||||||||
| # Задание 5 | ||||||||
|
|
@@ -91,5 +143,26 @@ | |||||||
| 'Олег': True, | ||||||||
| 'Миша': True, | ||||||||
| } | ||||||||
| # ??? | ||||||||
| def student_counting(val, numb): | ||||||||
| counter_gender_student = {'men': 0, 'female': 0} | ||||||||
| for student_dict in val: | ||||||||
| for key, name in student_dict.items(): | ||||||||
| if is_male[name] is True: | ||||||||
| counter_gender_student['men'] += 1 | ||||||||
| else: | ||||||||
| counter_gender_student['female'] += 1 | ||||||||
| if counter_gender_student['men'] > counter_gender_student['female']: | ||||||||
| return f'Больше всего мальчиков в классе {numb}' | ||||||||
| else: | ||||||||
| return f'Больше всего девочек в классе {numb}' | ||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
| for classes in school: | ||||||||
| for key, value in classes.items(): | ||||||||
| if type(value) is type(list()): | ||||||||
| value_list = value | ||||||||
| else: | ||||||||
| number_class = value | ||||||||
| print(student_counting(value_list, number_class)) | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,38 @@ | ||
| # Вывести последнюю букву в слове | ||
| word = 'Архангельск' | ||
| # ??? | ||
| print(word[-1]) | ||
|
|
||
|
|
||
| # Вывести количество букв "а" в слове | ||
| word = 'Архангельск' | ||
| # ??? | ||
| print(word.lower().count('а')) | ||
|
|
||
|
|
||
| # Вывести количество гласных букв в слове | ||
| word = 'Архангельск' | ||
| # ??? | ||
| count = 0 | ||
| for i in word.lower(): | ||
| if i in 'ауоыяюёие': | ||
| count += 1 | ||
| print(count) | ||
|
|
||
|
|
||
| # Вывести количество слов в предложении | ||
| sentence = 'Мы приехали в гости' | ||
| # ??? | ||
| print(len(sentence.split())) | ||
|
|
||
|
|
||
| # Вывести первую букву каждого слова на отдельной строке | ||
| sentence = 'Мы приехали в гости' | ||
| # ??? | ||
| for i in sentence.split(): | ||
| print(i[0]) | ||
|
|
||
|
|
||
| # Вывести усреднённую длину слова в предложении | ||
| sentence = 'Мы приехали в гости' | ||
| # ??? | ||
| count_word = len(sentence.split()) | ||
| count_letter = 0 | ||
| for i in sentence: | ||
| if i.isalpha(): | ||
| count_letter += 1 | ||
| print(count_letter / count_word) |
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.
здесь не нужна итерация по словарю, можно же просто
name = student_dict['first_name']остальное ок