-
Notifications
You must be signed in to change notification settings - Fork 260
exersises #22
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?
exersises #22
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
|
|
||
| names = ['Оля', 'Петя', 'Вася', 'Маша'] | ||
| # ??? | ||
|
|
||
| print("\n".join(names)) | ||
|
|
||
| # Задание 2 | ||
| # Необходимо вывести имена всех учеников из списка, рядом с именем показать количество букв в нём | ||
|
|
@@ -12,7 +12,9 @@ | |
| # Петя: 4 | ||
|
|
||
| names = ['Оля', 'Петя', 'Вася', 'Маша'] | ||
| # ??? | ||
|
|
||
| for name in names: | ||
| print(name, ":", len(name)) | ||
|
|
||
|
|
||
| # Задание 3 | ||
|
|
@@ -25,8 +27,8 @@ | |
| 'Маша': False, | ||
| } | ||
| names = ['Оля', 'Петя', 'Вася', 'Маша'] | ||
| # ??? | ||
|
|
||
| for name in names: | ||
| print(f"{name}: {'Мужской' if is_male[name] else 'Женский'}") | ||
|
|
||
| # Задание 4 | ||
| # Даны группу учеников. Нужно вывести количество групп и для каждой группы – количество учеников в ней | ||
|
|
@@ -40,8 +42,9 @@ | |
| ['Вася', 'Маша', 'Саша', 'Женя'], | ||
| ['Оля', 'Петя', 'Гриша'], | ||
| ] | ||
| # ??? | ||
|
|
||
| print(f"{len(groups)} всего групп") | ||
| for index, group in enumerate(groups): | ||
|
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. можно в enumerate через start указать номер с которого начинать |
||
| print(f"группа {index + 1}: {len(group)}") | ||
|
|
||
| # Задание 5 | ||
| # Для каждой пары учеников нужно с новой строки перечислить учеников, которые в неё входят | ||
|
|
@@ -54,4 +57,5 @@ | |
| ['Оля', 'Петя', 'Гриша'], | ||
| ['Вася', 'Маша', 'Саша', 'Женя'], | ||
| ] | ||
| # ??? | ||
| for index, group in enumerate(groups): | ||
| print(f'группа {index + 1}: {", ".join(group)}') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| from collections import Counter | ||
|
|
||
| # Задание 1 | ||
| # Дан список учеников, нужно посчитать количество повторений каждого имени ученика | ||
| # Пример вывода: | ||
|
|
@@ -12,8 +14,8 @@ | |
| {'first_name': 'Маша'}, | ||
| {'first_name': 'Петя'}, | ||
| ] | ||
| # ??? | ||
|
|
||
| for student in 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. Тут квадратичная сложность, потому что count будет весб список перебирать каждый раз. Лучше такого избегать |
||
| print(f"{student['first_name']} {students.count(student)}") | ||
|
|
||
| # Задание 2 | ||
| # Дан список учеников, нужно вывести самое часто повторящееся имя | ||
|
|
@@ -26,8 +28,8 @@ | |
| {'first_name': 'Маша'}, | ||
| {'first_name': 'Оля'}, | ||
| ] | ||
| # ??? | ||
|
|
||
| duplicates = [x for i, x in enumerate(students) if i != students.index(x)] | ||
|
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. аналогично, index это как еще один цикл. Итого цикл в цикле |
||
| print(duplicates) | ||
|
|
||
| # Задание 3 | ||
| # Есть список учеников в нескольких классах, нужно вывести самое частое имя в каждом классе. | ||
|
|
@@ -44,15 +46,19 @@ | |
| {'first_name': 'Маша'}, | ||
| {'first_name': 'Маша'}, | ||
| {'first_name': 'Оля'}, | ||
| ],[ # это – третий класс | ||
| ], [ # это – третий класс | ||
| {'first_name': 'Женя'}, | ||
| {'first_name': 'Петя'}, | ||
| {'first_name': 'Женя'}, | ||
| {'first_name': 'Саша'}, | ||
| ], | ||
| ] | ||
| # ??? | ||
|
|
||
| for j, s_class in enumerate(school_students, start=1): | ||
| count_list = [] | ||
| for dic_student in s_class: | ||
| count_list.append(dic_student["first_name"]) | ||
| count_dic = Counter(count_list) | ||
|
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. можно сразу в коунтер добавлять без промежточного списка |
||
| print(f'Самое частое имя среди учеников {j} класса: {count_dic.most_common(1)[0][0]}') | ||
|
|
||
| # Задание 4 | ||
| # Для каждого класса нужно вывести количество девочек и мальчиков в нём. | ||
|
|
@@ -72,8 +78,15 @@ | |
| 'Миша': True, | ||
| 'Даша': False, | ||
| } | ||
| # ??? | ||
|
|
||
| for school_class in school: | ||
| name_list = [student['first_name'] for student in school_class['students']] | ||
| count_girls, count_boys = 0, 0 | ||
| for name in name_list: | ||
| if is_male[name]: | ||
| count_boys += 1 | ||
| else: | ||
| count_girls += 1 | ||
| print(f"Класс {school_class['class']}: девочки {count_girls}, мальчики {count_boys}") | ||
|
|
||
| # Задание 5 | ||
| # По информации о учениках разных классов нужно найти класс, в котором больше всего девочек и больше всего мальчиков | ||
|
|
@@ -91,5 +104,26 @@ | |
| 'Олег': True, | ||
| 'Миша': True, | ||
| } | ||
| # ??? | ||
| count_list = [] | ||
| list_class = [] | ||
| for class_s in school: | ||
| list_class.append(class_s['students']) | ||
| class_g = 0 | ||
| class_b = 0 | ||
| for students in list_class: | ||
| for names in students: | ||
| name = names['first_name'] | ||
| if is_male[name]: | ||
| class_b += 1 | ||
| else: | ||
| class_g += 1 | ||
| count_list.append({"class": class_s['class'], "boys": class_b, 'girls': class_g}) | ||
|
|
||
| girls_max = max([g_count['girls'] for g_count in count_list]) | ||
| boys_max = max([b_count['boys'] for b_count in count_list]) | ||
|
|
||
| class_girls_max = [g_count['class'] for g_count in count_list if g_count['girls'] == girls_max][0] | ||
| class_boys_max = [b_count['class'] for b_count in count_list if b_count['boys'] == boys_max][0] | ||
|
|
||
| print(f'Больше всего мальчиков в классе {class_boys_max}') | ||
| print(f'Больше всего девочек в классе {class_girls_max}') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,33 @@ | ||
| # Вывести последнюю букву в слове | ||
| word = 'Архангельск' | ||
| # ??? | ||
|
|
||
| print(word[-1]) | ||
|
|
||
| # Вывести количество букв "а" в слове | ||
| word = 'Архангельск' | ||
| # ??? | ||
|
|
||
| print(word.lower().count("а")) | ||
|
|
||
| # Вывести количество гласных букв в слове | ||
| word = 'Архангельск' | ||
| # ??? | ||
|
|
||
| word_one = 'Архангельск' | ||
| glasniye = ["а", "я", "у", "ю", "о", "е", "ё", "э", "и", "ы"] | ||
|
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. можно просто написать буквы слитно |
||
| i = 0 | ||
| for letter in word_one.lower(): | ||
| if letter in glasniye: | ||
| i += 1 | ||
| print(f'{i} "раз"') | ||
|
|
||
| # Вывести количество слов в предложении | ||
| sentence = 'Мы приехали в гости' | ||
| # ??? | ||
|
|
||
| print(len(sentence.split())) | ||
|
|
||
| # Вывести первую букву каждого слова на отдельной строке | ||
| sentence = 'Мы приехали в гости' | ||
| # ??? | ||
|
|
||
| for word in sentence.split(): | ||
| print(word[0]) | ||
|
|
||
| # Вывести усреднённую длину слова в предложении | ||
| sentence = 'Мы приехали в гости' | ||
| # ??? | ||
| lenght = 0 | ||
| for word in sentence.split(): | ||
| lenght += len(word) | ||
| else: | ||
| print(f'Средняя длина слова в предложении {lenght // len(sentence.split())}') | ||
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.
не нужно в репозиторий добавлять служебные файлы