-
Notifications
You must be signed in to change notification settings - Fork 0
/
student.py
198 lines (159 loc) · 5.21 KB
/
student.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
"""
In this script the Student class is created in order to be used in the main script to evaluate possible outcomes for
graduation.
"""
class Student:
year = 0 # Since in first year of METU you cannot take classes from upper years it is important.
semester = 0 # 1 is Fall, 2 is Spring and 3 is Summer.
lessons = [] # Lessons ordered by semester as a nested list.
cgpa = -1.0
credit = 0
status = True # True for non-probation, False for probation.
graduation = False
def __init__(self, year, semester, lessons):
"""
:param year: int
:param semester: int
:param lessons: list(list(Lesson)) # Inner lists ordered by semester.
"""
self.year = year
self.semester = semester
self.lessons = lessons
lesson_list = []
former_cgpa = -1.0
for curr in lessons:
if self.cgpa >= 0.0:
former_cgpa = self.cgpa
self.cgpa = 0.0
else:
self.cgpa = 0.0
if former_cgpa >= 0.0:
self.cgpa = former_cgpa * self.credit
for lesson in curr:
if lesson.get_code() in [x.get_code() for x in lesson_list]:
self.cgpa -= list(filter(lambda x: x.get_code() == lesson.get_code(),
lesson_list))[-1].get_credits()
else:
self.credit += lesson.get_credit()
lesson_list.append(lesson)
self.cgpa += lesson.get_credits()
self.cgpa /= self.credit
if former_cgpa < 0.0 and self.cgpa < 0.0:
self.cgpa = 0.0
elif 0.0 <= former_cgpa < 2.0 and self.cgpa < 2.0:
self.status = False
if self.status:
self.graduation = True
def new_semester(self):
"""
:return: None
"""
self.lessons.append([])
self.semester = self.semester % 3 + 1
def add_lesson(self, lesson):
"""
:param lesson: Lesson
:return: bool # True if successfully added, otherwise False.
"""
if not self.is_lesson_ok(lesson):
return False
found = False
if not self.get_status():
code = lesson.get_code()
for curr in self.lessons:
for less in curr:
if less.get_code() == code:
found = True
break
if found:
break
if not found:
return False
if self.semester not in lesson.get_semesters():
return False
self.lessons[-1].append(lesson)
return True
def end_semester(self):
"""
:return: None
"""
lesson_list = []
for curr in self.lessons:
for less in curr:
lesson_list.append(less)
semester = self.lessons[-1]
if len(semester) == 0:
return
former_cgpa = self.cgpa
self.cgpa *= self.credit
for lesson in semester:
if lesson.get_code() in [x.get_code() for x in lesson_list]:
self.cgpa -= list(filter(lambda x: x.get_code() == lesson.get_code(),
lesson_list))[-1].get_credits()
else:
self.credit += lesson.get_credit()
self.cgpa += lesson.get_credits()
self.cgpa /= self.credit
if former_cgpa < 2.0 and self.cgpa < 2.0:
self.status = False
if self.status:
self.graduation = True
def is_graduated(self):
"""
:return: bool
"""
return self.graduation
def get_cgpa(self):
"""
:return: float
"""
return self.cgpa
def get_status(self):
"""
:return: bool
"""
return self.status
def get_lessons(self):
"""
:return: list(list(Lesson)) # Inner list represents semesters.
"""
return self.lessons
def get_max_lessons(self):
"""
:return: int
"""
if self.semester == 3:
return 2
if self.cgpa < 2.0:
return 6
if self.cgpa < 2.5:
return 7
if self.cgpa < 3.0:
return 8
if self.cgpa < 3.5:
return 9
return 10
def get_semester(self):
"""
:return: int # 1 for Fall, 2 for Spring and 3 for Summer.
"""
return self.semester
def is_lesson_ok(self, lesson):
"""
Checks if given lesson's prerequisites satisfied.
:param lesson: Lesson
:return: bool
"""
prereq = lesson.get_prerequisites()
if len(prereq) == 0:
return True
old_lessons = []
for curr in self.lessons:
for less in curr:
old_lessons.append(less)
for req in prereq:
if not len(list(filter(
lambda x: x.get_code() == req and x.get_grade() != 'U' and
(x.get_credit() == 0 or x.get_credits() / x.get_credit() >= 1.0), old_lessons))) > 0:
return False
return True