Major Lab 4Teaching Assistants: Alice and Bob
The problem
Daisy is working on an app to help students decide what courses they need to take. She’s scraped some data off the UTM Academic Calendar, but is finding it hard to analyze it.
We’ll be helping her out with our file reading anddictionary parsing skills! 😎
2
Note: this lab is a bit long, but don’t get overwhelmed with the length!
We’ll divide this up together, and use hints to get this done soon
3
Part 1: File I/O
4
Task 1: Open CSV
Complete the function open_csv so it opens a file, reads past the 3 line header, and returns the opened file!
Yes, this is as simple as it seems
5
Estimated Time
5 minutes!
6
Task 2: Read Program Requirements
We are given a CSV formatted like the following:
Program, Dept., Year X, n, course-1, …, course-n, Year Y, m, …
And we return a dictionary formatted as follows:
7
{<Program>: {‘Department’: <Dept>,
<Year X>: [<course-1>, …, <course-n>]
<Year Y>: [...]
}
That looks overwhelming...
It does. It’s different from what you’ve done before, so we’ll recommend some steps (provided as comments in the py file!):
1. Parse one line at a time. Each line has one program’s information
2. Split the line into a list. Remember: this file is a CSV3. Record the program name and department. They’re the
easiest values to extract4. Now loop over the *remaining* list.
8
Looping over remaining List
1. Use a while loop - it will make things easier!2. Each time you see a value like “Year X” in the list, the next value will be the
slice of the list you need to take from this point to get courses. For example:
[‘Year 1’, ‘3’, ‘CSC108’, ‘CSC148’, ‘MAT137, ‘Year 2’, …]
Note that slicing the list to get the next 3 elements after index 1 gets you the courses required in Year 1
3. Add the information you get from this slice to a dictionary, and now only loop over the remaining part of the list! Skip everything you needed for Year X, because you’re done (you’re need something like: lst = lst[ index of last course + 1: ]
9
Pseudocode hints:
Note: these hints are suggestions - you can take a different approach!
10
Examples
11
Program requirements:
{'Specialist Program ERSPE1038': {'Department': 'Information Security (Science)', 'Year 1': ['CSC108H5', ‘CSC148H5’]}})
File:
Specialist Program ERSPE1038, Information Security (Science), Year 1, 2, CSC108H5, CSC148H5\n
More examples in major.py!
12
Estimated time with pseudocode
30-45 minutes!
Suggestion: if you feel like this task is overwhelming you, start it, and then debug/test later in the week
You can also move on to the next functions, and see this again later
13
And you’re done :)
The loop in this function is probably the hardest part of this lab. Once you get this done, you’ve really won half the battle
14
Task 3: Course Requirements
This function is a lot easier than task 2. Ideally, you just need to:
1) Loop over each line2) Split to get [<course>, <prereqs>]3) Split to divide <prereqs>4) Add information to a dictionary
Note: watch out for extra space!
15
Examples
16
Program requirements:
{‘CSC108H5’: [], ‘CSC148H5’: [‘CSC108H5’], ‘CSC209H5’: [‘CSC207H5’, ‘MAT102H5’]
File:
CSC108H5:\nCSC148H5: CSC108H5\nCSC209H5: CSC207H5, MAT102H5\n
More examples in major.py!
17
Estimated Time
< 10-15 minutes
18
19
Part 2: Dictionaries
Task 4: Qualify for Program
A student, for the purposes of this lab, will qualify for a program if they've taken all the 1st year courses required for the program, and satisfy the minimum gpa requirement.
We have 3 parameters:1. Program_name: string which gives you program name (a key in
program info)2. Student record: dictionary with keys gpa (mapped to a float) and list of
courses (mapped to strings, course codes match courses in program_info)
3. Gpa: required gpa for the program4. Program_info: dictionary (like the one you generate in task 2)
20
Task 4: Qualify for Program
Our job is to:1. Check <program>’s information in program_info to make sure that
the student has taken all the first year courses required by the program and that their gpa meets the minimum gpa requirement.
If the student has taken all required 1st year courses and meets the minimum gpa requirement, then return True
21
Examples >>> program_info = {'Major Program ERMAJ2511': { \
'Department': 'Mathematical Sciences (Science)', \
'Year 1': ['MAT102H5', 'CSC108H5'] \
}}
>>> gpa = 3.0
>>> program_name = 'Major Program ERMAJ2511'
>>> student_info = {'gpa': '3.5', 'courses_taken': ['MAT102H5',
'MAT135H5']}
>>> qualify_for_program(program_name, student_info, gpa, program_info)
???
>>> student_info = {'gpa': '3.5', 'courses_taken': ['MAT102H5', 'CSC108H5'}
>>> qualify_for_program(program_name, student_info, gpa, program_info)
???
22
Examples >>> program_info = {'Major Program ERMAJ2511': { \
'Department': 'Mathematical Sciences (Science)', \
'Year 1': ['MAT102H5', 'CSC108H5'] \
}}
>>> gpa = 3.0
>>> program_name = 'Major Program ERMAJ2511'
>>> student_info = {'gpa': '3.5', 'courses_taken': ['MAT102H5',
'MAT135H5']}
>>> qualify_for_program(program_name, student_info, gpa, program_info)
False
>>> student_info = {'gpa': '3.5', 'courses_taken': ['MAT102H5', 'CSC108H5'}
>>> qualify_for_program(program_name, student_info, gpa, program_info)
???
23
Examples >>> program_info = {'Major Program ERMAJ2511': { \
'Department': 'Mathematical Sciences (Science)', \
'Year 1': ['MAT102H5', 'CSC108H5'] \
}}
>>> gpa = 3.0
>>> program_name = 'Major Program ERMAJ2511'
>>> student_info = {'gpa': '3.5', 'courses_taken': ['MAT102H5',
'MAT135H5']}
>>> qualify_for_program(program_name, student_info, gpa, program_info)
False
>>> student_info = {'gpa': '3.5', 'courses_taken': ['MAT102H5', 'CSC108H5'}
>>> qualify_for_program(program_name, student_info, gpa, program_info)
True
24
Task 5: Filter By Prereqs
Given the following parameters:
1. Students: dictionary that maps each <student> to the list of courses that they’ve taken
2. Courses_info: dictionary that maps courses to prerequisites (what Task 3 returns)
3. Course_name: the name of the course that we are checking prerequisites for
*modify* students so that it only contains students who have all the prerequisites required to take course_name
25
Examples >>> courses_info = {'CCT110H5': ['CCT109H5']}
>>> students = {'Naaz': ['CSC108H5', 'MAT102H5'], \
'Haocheng': ['CSC108H5', 'CCT109H5']}
>>> filter_by_prereqs(students, courses_info, 'CCT110H5')
>>> students
???
26
Examples >>> courses_info = {'CCT110H5': ['CCT109H5']}
>>> students = {'Naaz': ['CSC108H5', 'MAT102H5'], \
'Haocheng': ['CSC108H5', 'CCT109H5']}
>>> filter_by_prereqs(students, courses_info, 'CCT110H5')
>>> students
{'Haocheng': ['CSC108H5', 'CCT109H5']}
27
Top Related