diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..5ddaabae --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Module", + "type": "python", + "request": "launch", + "module": "for_dict_challenges", + "justMyCode": true, + "console": "internalConsole" + } + ] +} \ No newline at end of file diff --git a/for_challenges.py b/for_challenges.py index 997754da..06c2cf01 100644 --- a/for_challenges.py +++ b/for_challenges.py @@ -2,7 +2,12 @@ # Необходимо вывести имена всех учеников из списка с новой строки names = ['Оля', 'Петя', 'Вася', 'Маша'] -# ??? +for name1 in names: + print(name1) +print() +name2 = '\n'.join(names) +print(name2) +print() # Задание 2 @@ -12,8 +17,9 @@ # Петя: 4 names = ['Оля', 'Петя', 'Вася', 'Маша'] -# ??? - +for name3 in names: + print(f'{name3}: {len(name3)}') +print() # Задание 3 # Необходимо вывести имена всех учеников из списка, рядом с именем вывести пол ученика @@ -25,7 +31,13 @@ 'Маша': False, } names = ['Оля', 'Петя', 'Вася', 'Маша'] -# ??? +for name in names: + if is_male[name] == False: + print(f'{name}, женский') + else: + print(f'{name}, мужской') +print() + # Задание 4 @@ -40,7 +52,9 @@ ['Вася', 'Маша', 'Саша', 'Женя'], ['Оля', 'Петя', 'Гриша'], ] -# ??? +for group in range(1,len(groups)+1): + print(f'Группа {group}: {len(groups[group-1])} ученика.') + print() # Задание 5 @@ -54,4 +68,6 @@ ['Оля', 'Петя', 'Гриша'], ['Вася', 'Маша', 'Саша', 'Женя'], ] -# ??? \ No newline at end of file +for name in range(len(groups)): + names = ', '.join(groups[name]) + print(f'Группа{name+1}: {names}') \ No newline at end of file diff --git a/for_dict_challenges.py b/for_dict_challenges.py index 96062ebc..72703a6b 100644 --- a/for_dict_challenges.py +++ b/for_dict_challenges.py @@ -12,8 +12,19 @@ {'first_name': 'Маша'}, {'first_name': 'Петя'}, ] -# ??? +def diction_name_count(lis): + repeat = {} + for key_name in range(len(lis)): + name = lis[key_name]['first_name'] + if name not in repeat: + repeat[name] = 1 + else: + repeat[name] += 1 + return repeat +for name,count in diction_name_count(students).items(): + print(f'{name}: {count}') +print() # Задание 2 # Дан список учеников, нужно вывести самое часто повторящееся имя @@ -26,7 +37,18 @@ {'first_name': 'Маша'}, {'first_name': 'Оля'}, ] -# ??? +repeat = diction_name_count(students) +def max_name(diction): + max_count = 0 + for name,count in diction.items(): + if count > max_count: + max_count = count + max_n = name + return max_n + + +print(f'самое частое имя среди учеников: {max_name(repeat)}') +print() # Задание 3 @@ -51,8 +73,10 @@ {'first_name': 'Саша'}, ], ] -# ??? +for classes in range(len(school_students)): + print(f'Самое частое имя в классе {classes+1}: {max_name(diction_name_count(school_students[classes]))} ') +print() # Задание 4 # Для каждого класса нужно вывести количество девочек и мальчиков в нём. @@ -72,7 +96,27 @@ 'Миша': True, 'Даша': False, } -# ??? + +for classy in range(len(school)): + clas = school[classy] + nomer_classa = clas['class'] + boys = [] + girls = [] + student = clas['students'] + for n in student: + name = n['first_name'] + if is_male[name] == True: + boys.append(name) + else: + girls.append(name) + count_boys = len(boys) + count_girls = len(girls) + print(f'Класс {nomer_classa}: девочки {count_girls}, мальчики {count_boys}') +print() + + + + # Задание 5 @@ -91,5 +135,36 @@ 'Олег': True, 'Миша': True, } -# ??? +clas_count_boys = 0 +clas_count_girls = 0 +clas_max_boys = '' +clas_max_girls = '' + +for classy in range(len(school)): + clas = school[classy] + nomer_classa = clas['class'] + boys = [] + girls = [] + student = clas['students'] + + for elem in student: + name = elem['first_name'] + + if is_male[name] == True: + boys.append(name) + else: + girls.append(name) + + count_boys = len(boys) + count_girls = len(girls) + + if count_boys > clas_count_boys: + clas_count_boys = count_boys + clas_max_boys = nomer_classa + if count_girls > clas_count_girls: + clas_count_girls = count_girls + clas_max_girls = nomer_classa +print(f'Больше всего мальчиков в классе {clas_max_boys}') +print(f'Больше всего девочек в классе {clas_max_girls}') +print() diff --git a/string_challenges.py b/string_challenges.py index 856add2d..4750b51c 100644 --- a/string_challenges.py +++ b/string_challenges.py @@ -1,28 +1,46 @@ # Вывести последнюю букву в слове word = 'Архангельск' -# ??? +print(word[-1]) +print() # Вывести количество букв "а" в слове word = 'Архангельск' -# ??? +print(len(word)) +print() # Вывести количество гласных букв в слове word = 'Архангельск' -# ??? +vowels ='аоуыэеёиюя' +vowels_list = [] +for letter in word.lower(): + if letter in vowels: + vowels_list.append(letter) +print(len(vowels_list)) +print() # Вывести количество слов в предложении sentence = 'Мы приехали в гости' -# ??? - +sentence = sentence.split() +print(len(sentence)) +print() # Вывести первую букву каждого слова на отдельной строке sentence = 'Мы приехали в гости' -# ??? +sentence = sentence.split() +for word in sentence: + print(word) +print() # Вывести усреднённую длину слова в предложении sentence = 'Мы приехали в гости' -# ??? \ No newline at end of file +sentence = sentence.split() +sum = 0 +for word in sentence: + sum += len(word) +average = sum//len(sentence) +print(average) +print()