From cbaf250a8d8e91eef9089d3c132af62cac8a3b2f Mon Sep 17 00:00:00 2001 From: Olga Glazunova Date: Thu, 5 Dec 2024 14:37:46 +0500 Subject: [PATCH 1/2] =?UTF-8?q?=D0=92=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BC=D0=B0=D1=88=D0=BA=D0=B0=20basic=5Fexercises?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- for_challenges.py | 19 +++++++++++++- for_dict_challenges.py | 58 +++++++++++++++++++++++++++++++++++++++++- string_challenges.py | 23 +++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/for_challenges.py b/for_challenges.py index 997754da..6f2e6380 100644 --- a/for_challenges.py +++ b/for_challenges.py @@ -2,6 +2,7 @@ # Необходимо вывести имена всех учеников из списка с новой строки names = ['Оля', 'Петя', 'Вася', 'Маша'] +print('\n'.join(names)) # ??? @@ -12,7 +13,8 @@ # Петя: 4 names = ['Оля', 'Петя', 'Вася', 'Маша'] -# ??? +for name in names: + print(f"{name}: {len(name)}") # Задание 3 @@ -25,6 +27,10 @@ 'Маша': False, } names = ['Оля', 'Петя', 'Вася', 'Маша'] +for name,gender in is_male.items(): + gender = "М" if gender else "Ж" + print(f"{name}: {gender}") + # ??? @@ -40,6 +46,13 @@ ['Вася', 'Маша', 'Саша', 'Женя'], ['Оля', 'Петя', 'Гриша'], ] + +count_groups = len(groups) +print(f"Всего {count_groups} группы") + +for group_number,group in enumerate(groups, start = 1): + count_students = len(group) + print(f"Группа {group_number}: {count_students} ученика") # ??? @@ -54,4 +67,8 @@ ['Оля', 'Петя', 'Гриша'], ['Вася', 'Маша', 'Саша', 'Женя'], ] + +for group_number,group in enumerate(groups, start = 1): + students = ', '.join(group) + print(f"Группа {group_number}: {students}") # ??? \ No newline at end of file diff --git a/for_dict_challenges.py b/for_dict_challenges.py index fd86d344..a1e179ae 100644 --- a/for_dict_challenges.py +++ b/for_dict_challenges.py @@ -12,7 +12,18 @@ {'first_name': 'Маша'}, {'first_name': 'Петя'}, ] -# ??? + +names = dict() + +for student in students: + if student['first_name'] not in names.keys(): + names[student['first_name']] = 1 + else: + names[student['first_name']] += 1 + +for name, count in names.items(): + print(f"{name}:{count}") + # ??? # Задание 2 @@ -26,6 +37,22 @@ {'first_name': 'Маша'}, {'first_name': 'Оля'}, ] +names = dict() + +for student in students: + if student['first_name'] not in names.keys(): + names[student['first_name']] = 1 + else: + names[student['first_name']] += 1 + +frequent_name = None +name_count = 0 + +for name, count in names.items(): + if count > name_count: + name_count = count + frequent_name = name +print(f"Самое частое имя: {frequent_name}") # ??? @@ -51,6 +78,16 @@ {'first_name': 'Саша'}, ], ] + +from collections import Counter + +class_num = 1 +for item in school_students: + temp = [name['first_name'] for name in item] + count = Counter(temp) + print(f'Самое частое имя в классе {class_num}: {count.most_common()[0][0]}') + class_num += 1 + # ??? @@ -72,6 +109,25 @@ 'Миша': True, 'Даша': False, } + +print(school[0], type(school[0])) + +for class_info in school: + class_name = class_info['class'] + students = class_info['students'] + + boys = 0 + girls = 0 + + for student in students: + name = student['first_name'] + if is_male.get(name): + boys +=1 + else: + girls +=1 + +print(f'Класс {class_name}: девочки {girls}, мальчики {boys}') + # ??? diff --git a/string_challenges.py b/string_challenges.py index 856add2d..99553266 100644 --- a/string_challenges.py +++ b/string_challenges.py @@ -1,28 +1,51 @@ # Вывести последнюю букву в слове word = 'Архангельск' +last = word[-1] +print(last) # ??? # Вывести количество букв "а" в слове word = 'Архангельск' +a= word.lower() +print(a.count('а')) # ??? # Вывести количество гласных букв в слове word = 'Архангельск' +vowels = "аеёиоуыэюяАЕЁОИУЫЭЮЯ" +count_vow = 0 +for letter in word.lower(): + if letter in vowels: + count_vow += 1 + +print(count_vow) + # ??? # Вывести количество слов в предложении sentence = 'Мы приехали в гости' +words = sentence.split() +num_words = len(words) +print(num_words) # ??? # Вывести первую букву каждого слова на отдельной строке sentence = 'Мы приехали в гости' +words = sentence.split() +for word in words: + print(word[0]) # ??? # Вывести усреднённую длину слова в предложении sentence = 'Мы приехали в гости' +words = sentence.split() +num_words = len(words) +for word in words: + avg_word_len = len(sentence) / num_words +print(f"Средняя длина слова в предложении {avg_word_len}") # ??? \ No newline at end of file From af19b1a843616eb6a376816367d0ca5183d15a60 Mon Sep 17 00:00:00 2001 From: Olga Glazunova Date: Fri, 6 Dec 2024 12:16:38 +0500 Subject: [PATCH 2/2] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D0=B0,=20=D1=87=D1=82=D0=BE=D0=B1=D1=8B=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=85=D0=BE=D0=B4=D0=B8=D0=BB=20=D0=BF=D0=BE=20=D1=81?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=BA=D1=83=20=D0=B8=D0=BC=D0=B5=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- for_challenges.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/for_challenges.py b/for_challenges.py index 6f2e6380..53842771 100644 --- a/for_challenges.py +++ b/for_challenges.py @@ -27,9 +27,11 @@ 'Маша': False, } names = ['Оля', 'Петя', 'Вася', 'Маша'] -for name,gender in is_male.items(): - gender = "М" if gender else "Ж" - print(f"{name}: {gender}") +for name in names: + if name in is_male: +# print(name) + gender = "М" if is_male[name] else "Ж" + print(f"{name}: {gender}") # ???