-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOOP1.py
215 lines (169 loc) · 8.83 KB
/
OOP1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
class Student:
def __init__(self, name, surname):
# Перегрузка метода _init_ для определения атрибутов класса Student
self.name = name
self.surname = surname
self.finished_courses = []
self.courses_in_progress = []
self.grades = {}
self.average_rating = float()
def __str__(self):
grades_count = 0
courses_in_progress_string = ', '.join(self.courses_in_progress)
finished_courses_string = ', '.join(self.finished_courses)
for k in self.grades:
grades_count += len(self.grades[k])
self.average_rating = sum(map(sum, self.grades.values())) / grades_count
res = f'Имя: {self.name}\n' \
f'Фамилия: {self.surname}\n' \
f'Средняя оценка за домашнее задание: {self.average_rating}\n' \
f'Курсы в процессе обучения: {courses_in_progress_string}\n' \
f'Завершенные курсы: {finished_courses_string}'
return res
def rate_hw(self, lecturer, course, grade):
if isinstance(lecturer, Lecturer) and course in self.courses_in_progress and course in lecturer.courses_attached:
if course in lecturer.grades:
lecturer.grades[course] += [grade]
else:
lecturer.grades[course] = [grade]
else:
return 'Ошибка'
def __lt__(self, other):
if not isinstance(other, Student):
print('Такое сравнение некорректно')
return
return self.average_rating < other.average_rating
class Mentor:
def __init__(self, name, surname):
self.name = name
self.surname = surname
self.courses_attached = []
class Lecturer(Mentor):
def __init__(self, name, surname):
super().__init__(name, surname)
self.average_rating = float()
self.grades = {}
def __str__(self):
grades_count = 0
for k in self.grades:
grades_count += len(self.grades[k])
self.average_rating = sum(map(sum, self.grades.values())) / grades_count
res = f'Имя: {self.name}\nФамилия: {self.surname}\nСредняя оценка за лекции: {self.average_rating}'
return res
def __lt__(self, other):
if not isinstance(other, Lecturer):
print('Такое сравнение некорректно')
return
return self.average_rating < other.average_rating
class Reviewer(Mentor):
def rate_hw(self, student, course, grade):
if isinstance(student, Student) and course in self.courses_attached and course in student.courses_in_progress:
if course in student.grades:
student.grades[course] += [grade]
else:
student.grades[course] = [grade]
else:
return 'Ошибка'
def __str__(self):
res = f'Имя: {self.name}\nФамилия: {self.surname}'
return res
# Создаем лекторов и закрепляем их за курсом
best_lecturer_1 = Lecturer('Максим', 'Галкин')
best_lecturer_1.courses_attached += ['Python']
best_lecturer_2 = Lecturer('Иван', 'Ямщиков')
best_lecturer_2.courses_attached += ['Git']
best_lecturer_3 = Lecturer('Саня', 'Янкин')
best_lecturer_3.courses_attached += ['Python']
# Создаем проверяющих и закрепляем их за курсом
cool_reviewer_1 = Reviewer('Тоня', 'Аясян')
cool_reviewer_1.courses_attached += ['Python']
cool_reviewer_1.courses_attached += ['Git']
cool_reviewer_2 = Reviewer('Гриша', 'Холост')
cool_reviewer_2.courses_attached += ['Python']
cool_reviewer_2.courses_attached += ['Git']
# Создаем студентов и определяем для них изучаемые и завершенные курсы
student_1 = Student('Даня', 'Палкин')
student_1.courses_in_progress += ['Python']
student_1.finished_courses += ['Введение в программирование']
student_2 = Student('Галя', 'Свияга')
student_2.courses_in_progress += ['Git']
student_2.finished_courses += ['Основа']
student_3 = Student('Маша', 'Мылка')
student_3.courses_in_progress += ['Python']
student_3.finished_courses += ['Введение в программирование']
# Выставляем оценки лекторам за лекции
student_1.rate_hw(best_lecturer_1, 'Python', 10)
student_1.rate_hw(best_lecturer_1, 'Python', 10)
student_1.rate_hw(best_lecturer_1, 'Python', 10)
student_1.rate_hw(best_lecturer_2, 'Python', 5)
student_1.rate_hw(best_lecturer_2, 'Python', 7)
student_1.rate_hw(best_lecturer_2, 'Python', 8)
student_1.rate_hw(best_lecturer_1, 'Python', 7)
student_1.rate_hw(best_lecturer_1, 'Python', 8)
student_1.rate_hw(best_lecturer_1, 'Python', 9)
student_2.rate_hw(best_lecturer_2, 'Git', 10)
student_2.rate_hw(best_lecturer_2, 'Git', 8)
student_2.rate_hw(best_lecturer_2, 'Git', 9)
student_3.rate_hw(best_lecturer_3, 'Python', 5)
student_3.rate_hw(best_lecturer_3, 'Python', 6)
student_3.rate_hw(best_lecturer_3, 'Python', 7)
# Выставляем оценки студентам за домашние задания
cool_reviewer_1.rate_hw(student_1, 'Python', 8)
cool_reviewer_1.rate_hw(student_1, 'Python', 9)
cool_reviewer_1.rate_hw(student_1, 'Python', 10)
cool_reviewer_2.rate_hw(student_2, 'Git', 8)
cool_reviewer_2.rate_hw(student_2, 'Git', 7)
cool_reviewer_2.rate_hw(student_2, 'Git', 9)
cool_reviewer_2.rate_hw(student_3, 'Python', 8)
cool_reviewer_2.rate_hw(student_3, 'Python', 7)
cool_reviewer_2.rate_hw(student_3, 'Python', 9)
cool_reviewer_2.rate_hw(student_3, 'Python', 8)
cool_reviewer_2.rate_hw(student_3, 'Python', 7)
cool_reviewer_2.rate_hw(student_3, 'Python', 9)
# Выводим характеристики созданных и оцененых студентов в требуемом виде
print(f'Перечень студентов:\n\n{student_1}\n\n{student_2}\n\n{student_3}')
print()
print()
# Выводим характеристики созданных и оцененых лекторов в требуемом виде
print(f'Перечень лекторов:\n\n{best_lecturer_1}\n\n{best_lecturer_2}\n\n{best_lecturer_3}')
print()
print()
# Выводим результат сравнения студентов по средним оценкам за домашние задания
print(f'Результат сравнения студентов (по средним оценкам за домашние задания): '
f'{student_1.name} {student_1.surname} < {student_2.name} {student_2.surname} = {student_1 > student_2}')
print()
# Выводим результат сравнения лекторов по средним оценкам за лекции
print(f'Результат сравнения лекторов (по средним оценкам за лекции): '
f'{best_lecturer_1.name} {best_lecturer_1.surname} < {best_lecturer_2.name} {best_lecturer_2.surname} = {best_lecturer_1 > best_lecturer_2}')
print()
# Создаем список студентов
student_list = [student_1, student_2, student_3]
# Создаем список лекторов
lecturer_list = [best_lecturer_1, best_lecturer_2, best_lecturer_3]
# Создаем функцию для подсчета средней оценки за домашние задания
# по всем студентам в рамках конкретного курса
# в качестве аргументов принимает список студентов и название курса
def student_rating(student_list, course_name):
sum_all = 0
count_all = 0
for stud in student_list:
if stud.courses_in_progress == [course_name]:
sum_all += stud.average_rating
count_all += 1
average_for_all = sum_all / count_all
return average_for_all
# Создаем функцию для подсчета средней оценки за лекции всех лекторов в рамках курса
# в качестве аргумента принимает список лекторов и название курса
def lecturer_rating(lecturer_list, course_name):
sum_all = 0
count_all = 0
for lect in lecturer_list:
if lect.courses_attached == [course_name]:
sum_all += lect.average_rating
count_all += 1
average_for_all = sum_all / count_all
return average_for_all
print(f"Средняя оценка для всех студентов по курсу {'Python'}: {student_rating(student_list, 'Python')}")
print()
print(f"Средняя оценка для всех лекторов по курсу {'Python'}: {lecturer_rating(lecturer_list, 'Python')}")
print()