From 701e2b9255412113c4f3d0649fba822ab2cdf5d2 Mon Sep 17 00:00:00 2001 From: Yury Larin Date: Sun, 19 Feb 2023 13:51:12 +0300 Subject: [PATCH] exersises --- .idea/.gitignore | 3 ++ .idea/basic_exercises.iml | 10 ++++ .../inspectionProfiles/profiles_settings.xml | 6 +++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 +++ for_challenges.py | 18 ++++--- for_dict_challenges.py | 54 +++++++++++++++---- string_challenges.py | 29 +++++----- 9 files changed, 109 insertions(+), 29 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/basic_exercises.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/basic_exercises.iml b/.idea/basic_exercises.iml new file mode 100644 index 00000000..715cad8a --- /dev/null +++ b/.idea/basic_exercises.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..105ce2da --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..30247454 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..09aacff9 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/for_challenges.py b/for_challenges.py index 997754da..b24d81af 100644 --- a/for_challenges.py +++ b/for_challenges.py @@ -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): + print(f"группа {index + 1}: {len(group)}") # Задание 5 # Для каждой пары учеников нужно с новой строки перечислить учеников, которые в неё входят @@ -54,4 +57,5 @@ ['Оля', 'Петя', 'Гриша'], ['Вася', 'Маша', 'Саша', 'Женя'], ] -# ??? \ No newline at end of file +for index, group in enumerate(groups): + print(f'группа {index + 1}: {", ".join(group)}') \ No newline at end of file diff --git a/for_dict_challenges.py b/for_dict_challenges.py index 96062ebc..b4ba4f95 100644 --- a/for_dict_challenges.py +++ b/for_dict_challenges.py @@ -1,3 +1,5 @@ +from collections import Counter + # Задание 1 # Дан список учеников, нужно посчитать количество повторений каждого имени ученика # Пример вывода: @@ -12,8 +14,8 @@ {'first_name': 'Маша'}, {'first_name': 'Петя'}, ] -# ??? - +for student in students: + 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)] +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) + 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}') diff --git a/string_challenges.py b/string_challenges.py index 856add2d..6a0a12b7 100644 --- a/string_challenges.py +++ b/string_challenges.py @@ -1,28 +1,33 @@ # Вывести последнюю букву в слове word = 'Архангельск' -# ??? - +print(word[-1]) # Вывести количество букв "а" в слове word = 'Архангельск' -# ??? - +print(word.lower().count("а")) # Вывести количество гласных букв в слове -word = 'Архангельск' -# ??? - +word_one = 'Архангельск' +glasniye = ["а", "я", "у", "ю", "о", "е", "ё", "э", "и", "ы"] +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 = 'Мы приехали в гости' -# ??? \ No newline at end of file +lenght = 0 +for word in sentence.split(): + lenght += len(word) +else: + print(f'Средняя длина слова в предложении {lenght // len(sentence.split())}')