forked from csesoc/Handbook-Parser-Assessment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandbook.py
More file actions
157 lines (95 loc) · 4.41 KB
/
handbook.py
File metadata and controls
157 lines (95 loc) · 4.41 KB
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
"""
Inside conditions.json, you will see a subset of UNSW courses mapped to their
corresponding text conditions. We have slightly modified the text conditions
to make them simpler compared to their original versions.
Your task is to complete the is_unlocked function which helps students determine
if their course can be taken or not.
We will run our hidden tests on your submission and look at your success rate.
We will only test for courses inside conditions.json. We will also look over the
code by eye.
NOTE: We do not expect you to come up with a perfect solution. We are more interested
in how you would approach a problem like this.
"""
import json
# NOTE: DO NOT EDIT conditions.json
with open("./conditions.json") as f:
CONDITIONS = json.load(f)
f.close()
PREFIXES = ['COMP', 'MATH', 'DPST', 'MTRN', 'ELEC']
COURSE_UNITS = 6
COURSE_CODE_SIZE = 8
def check_level(units, courses_list, prereq):
i = 0
while not prereq[i].isdigit():
i += 1
unit_sum = 0
for course in courses_list:
if course.startswith('COMP' + prereq[i]):
unit_sum += COURSE_UNITS
if unit_sum >= units:
return True
else:
return False
def check_courses(units, courses_list, prereq):
unit_sum = 0
for index, character in enumerate(prereq):
for course in courses_list:
if prereq[slice(index, len(prereq))].startswith(course):
unit_sum += COURSE_UNITS
if unit_sum >= units:
return True
else:
return False
def check_statement(courses_list, prereq):
meets_prereq = True
skip = False
for index, character in enumerate(prereq):
if character == ')' and skip:
skip = False
continue
if skip:
continue
if character == '(':
meets_prereq = check_statement(courses_list, prereq[slice(index + 1, len(prereq))])
skip = True
if any(prereq[slice(index, len(prereq))].startswith(prefix) for prefix in PREFIXES) and not prereq[slice(index, len(prereq))].startswith("COMP courses"):
if prereq[slice(index, index + COURSE_CODE_SIZE)] not in courses_list:
meets_prereq = False
else:
meets_prereq = True
if prereq[slice(index, len(prereq))].lower().startswith('and') and not meets_prereq:
return False
if prereq[slice(index, len(prereq))].lower().startswith('or') and meets_prereq:
return True
if prereq[slice(index, len(prereq))].lower().startswith('units of credit in') or prereq[slice(index, len(prereq))].lower().startswith('units oc credit in'):
i = index - 2
while i > 0 and prereq[i] != ' ' and prereq[i] != '(':
i -= 1
UOC = int(prereq[slice(i, index - 1)])
j = index + 19
if prereq[slice(j, len(prereq))].lower().startswith('level'):
meets_prereq = check_level(UOC, courses_list, prereq[slice(j, len(prereq))])
elif prereq[slice(j, len(prereq))].lower().startswith('('):
meets_prereq = check_courses(UOC, courses_list, prereq[slice(j, len(prereq))])
skip = True
continue
if prereq[slice(index, len(prereq))].lower().startswith('units of credit'):
i = index - 2
while i > 0 and prereq[i] != ' ' and prereq[i] != '(':
i -= 1
UOC = int(prereq[slice(i, index - 1)])
if len(courses_list) * COURSE_UNITS < UOC:
meets_prereq = False
return meets_prereq
def is_unlocked(courses_list, target_course):
"""
Given a list of course codes a student has taken, return true if the target_course
can be unlocked by them.
You do not have to do any error checking on the inputs and can assume that
the target_course always exists inside conditions.json
ASSUMPTION: course_list is a list (array) of course codes
"""
prequisites = CONDITIONS[target_course]
if len(prequisites) == 0:
return True
return check_statement(courses_list, prequisites)