Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
env/
27 changes: 21 additions & 6 deletions for_challenges.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Необходимо вывести имена всех учеников из списка с новой строки

names = ['Оля', 'Петя', 'Вася', 'Маша']
# ???
print('\nЗадание 1')
[print(name) for name in names]
Comment thread
Maksimous777 marked this conversation as resolved.


# Задание 2
Expand All @@ -12,7 +13,8 @@
# Петя: 4

names = ['Оля', 'Петя', 'Вася', 'Маша']
# ???
print('\nЗадание 2')
[print(f'{name}: {len(name)}') for name in names]
Comment thread
Maksimous777 marked this conversation as resolved.


# Задание 3
Expand All @@ -24,8 +26,16 @@
'Вася': True,
'Маша': False,
}
names = ['Оля', 'Петя', 'Вася', 'Маша']
# ???
names = ['Оля', 'Петя', 'Вася', 'Маша', 'Максим']
print('\nЗадание 3')
for name in names:
try:
if is_male[name]:
print(f'{name}: Мужской')
else:
print(f'{name}: Женский')
except KeyError:
print(f'Не знаю такого имени: {name}')
Comment on lines +32 to +38
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#42 (comment)
Добавил try...except



# Задание 4
Expand All @@ -40,7 +50,10 @@
['Вася', 'Маша', 'Саша', 'Женя'],
['Оля', 'Петя', 'Гриша'],
]
# ???
print('\nЗадание 4')
print(f'Всего {len(groups)} группы')
for index, group in enumerate(groups, start=1):
print(f"Группа {index}: {len(group)} ученика")


# Задание 5
Expand All @@ -54,4 +67,6 @@
['Оля', 'Петя', 'Гриша'],
['Вася', 'Маша', 'Саша', 'Женя'],
]
# ???
print('\nЗадание 5')
[print(f"Группа {index}: {', '.join(group)}")
for index, group in enumerate(groups, start=1)]
75 changes: 61 additions & 14 deletions for_dict_challenges.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import Counter
# Задание 1
# Дан список учеников, нужно посчитать количество повторений каждого имени ученика
# Пример вывода:
Expand All @@ -12,7 +13,12 @@
{'first_name': 'Маша'},
{'first_name': 'Петя'},
]
# ???

print('Задание 1')
qty_students = (Counter(student['first_name'] for student in students))
for student in qty_students:
print(f'{student}: {qty_students[student]}')
print()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough.
работать будет. А можешь любое из этих заданий сделать напрямую через словари, без counter?



# Задание 2
Expand All @@ -26,7 +32,12 @@
{'first_name': 'Маша'},
{'first_name': 'Оля'},
]
# ???

print('Задание 2')
most_common_student = Counter(student['first_name']
for student in students).most_common(1)
print(
f'Самое частое имя среди учеников: {most_common_student[0][0]}\n')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Хорошо!



# Задание 3
Expand All @@ -44,26 +55,35 @@
{'first_name': 'Маша'},
{'first_name': 'Маша'},
{'first_name': 'Оля'},
],[ # это – третий класс
], [ # это – третий класс
{'first_name': 'Женя'},
{'first_name': 'Петя'},
{'first_name': 'Женя'},
{'first_name': 'Саша'},
],
]
# ???

print('Задание 3')
for grade in range(len(school_students)):
max_students = Counter(student['first_name']
for student in school_students[grade]).most_common(1)
print(
f'Самое частое имя в классе {grade + 1}: {max_students[0][0]}')
print()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


# Задание 4
# Для каждого класса нужно вывести количество девочек и мальчиков в нём.
# Пример вывода:
# Класс 2a: девочки 2, мальчики 0
# Класс 2a: девочки 2, мальчики 0
# Класс 2б: девочки 0, мальчики 2

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': '2a', 'students': [
{'first_name': 'Маша'}, {'first_name': 'Оля'}]},
{'class': '2б', 'students': [
{'first_name': 'Олег'}, {'first_name': 'Миша'}]},
{'class': '2б', 'students': [{'first_name': 'Даша'}, {
'first_name': 'Олег'}, {'first_name': 'Маша'}]},
]
is_male = {
'Олег': True,
Expand All @@ -72,24 +92,51 @@
'Миша': True,
'Даша': False,
}
# ???


print('Задание 4')
for grade in school:
male = 0
female = 0
for student in grade['students']:
if is_male[student['first_name']]:
male += 1
else:
female += 1
print(
f"Класс {grade['class']}: девочки {female}, мальчики {male}")
print()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

# Задание 5
# По информации о учениках разных классов нужно найти класс, в котором больше всего девочек и больше всего мальчиков
# Пример вывода:
# Больше всего мальчиков в классе 3c
# Больше всего девочек в классе 2a

school = [
{'class': '2a', 'students': [{'first_name': 'Маша'}, {'first_name': 'Оля'}]},
{'class': '3c', 'students': [{'first_name': 'Олег'}, {'first_name': 'Миша'}]},
{'class': '2a', 'students': [
{'first_name': 'Маша'}, {'first_name': 'Оля'}]},
{'class': '3c', 'students': [
{'first_name': 'Олег'}, {'first_name': 'Миша'}]},
]
is_male = {
'Маша': False,
'Оля': False,
'Олег': True,
'Миша': True,
}
# ???
print('Задание 5')
male = []
female = []
for grade in range(len(school)):
male.append(0)
female.append(0)
for student in school[grade]['students']:
if is_male[student['first_name']]:
male[grade] += 1
else:
female[grade] += 1

most_male = school[male.index(max(male))]['class']
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут мне кажется усложненно. Я бы предложила переделать через хранение доппеременных. Макс_число мальчиков + класс в котором это число и аналогично для девочек. На таких объемах это по длительности работы программы будет неотличимо. Но если мы говорим условно обо всех классах москвы, то уже разница будет ощутима

most_female = school[female.index(max(female))]['class']
print(
f"Больше всего мальчиков в классе {most_male}")
print(
f"Больше всего девочек в классе {most_female}")