diff --git a/for_challenges.py b/for_challenges.py index 997754da..e7f9cec4 100644 --- a/for_challenges.py +++ b/for_challenges.py @@ -2,7 +2,8 @@ # Необходимо вывести имена всех учеников из списка с новой строки names = ['Оля', 'Петя', 'Вася', 'Маша'] -# ??? +for name in names: + print(name) # Задание 2 @@ -11,8 +12,10 @@ # Оля: 3 # Петя: 4 + names = ['Оля', 'Петя', 'Вася', 'Маша'] -# ??? +for name in names: + print(name, len(names)) # Задание 3 @@ -25,7 +28,11 @@ 'Маша': False, } names = ['Оля', 'Петя', 'Вася', 'Маша'] -# ??? +for name in names: + if is_male[name]: + print(f'{name} мужской род') + else: + print(f'{name} женский род') # Задание 4 @@ -40,7 +47,9 @@ ['Вася', 'Маша', 'Саша', 'Женя'], ['Оля', 'Петя', 'Гриша'], ] -# ??? +print(f'Всего {len(groups)} группы.') +for number, group in enumerate(groups, 1): + print(f'В группе {number}:{len(group)} ученика') # Задание 5 @@ -54,4 +63,5 @@ ['Оля', 'Петя', 'Гриша'], ['Вася', 'Маша', 'Саша', 'Женя'], ] -# ??? \ No newline at end of file +for number, name in enumerate(groups, 1): + print(f"В группе {number}:{', '.join(name)}") \ No newline at end of file diff --git a/for_dict_challenges.py b/for_dict_challenges.py index 96062ebc..6c0fbe57 100644 --- a/for_dict_challenges.py +++ b/for_dict_challenges.py @@ -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}') # Задание 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}') + + + # Задание 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}') # Задание 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']}") # Задание 5 diff --git a/string_challenges.py b/string_challenges.py index 856add2d..f688727e 100644 --- a/string_challenges.py +++ b/string_challenges.py @@ -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 = 'Мы приехали в гости' -# ??? \ No newline at end of file +words = sentence.split(' ') +avrg = sum(len(word) for word in words)/(len(words)) +print(avrg) \ No newline at end of file