Preparation

a. Create a folder called lab-02-13 in your StuWork directory.

b. Open a Terminal and navigate to your lab-02-13 directory.

c. Copy the courses.py and some_courses.txt files from the CourseMaterials/lab-02-13 directory into your lab-02-13 directory.

Exercise 1

Recall that on assignment 4 we worked with the following list of courses. In this exercise, we are going to play around with it again.

a. Open up the some_courses.txt file and examine the formatting of the file. You should recognize how each line is the same formatting from Assignment 4.

b. Open up the courses.py and look at the functions provided. This is a partial solution to Assignment 4 and we can use these functions to process the data in some_courses.txt.

c. Open up a REPL and open the file.

>>> file = open("some_courses.txt")

d. Experiment with the readline() and read(n) functions to see what they do.

e. Now close the file with file.close() and reopen it with open. You can now read the entire file contents into a list of strings using

>>> list_of_courses = list(file)

f. Try looking at the elements of list_of_courses to make sure you know what the above line of code did.

g. Try calling some of the functions from courses.py on the courses in the list such as title, department, and enrollment to make sure they are working correctly.

Exercise 2

Now let’s experiment with the sort method.

a. Try typing in the following:

>>> list_of_courses.sort()

b. To view the current order of the elements of the list, you can print them off with a loop:

>>> for c in list_of_courses: print(c)

What was the reason for sort to arrange the elements in this order? Discuss this with your partner.

c. Now let’s try sorting by the course title.

>>> list_of_courses.sort(key=title)
>>> for c in list_of_courses: print(c)

d. Now use sort and the enrollment function to find the course with the largest enrollment.

e. Now find the course with the largest waitlist.

Exercise 3

In this exercise, we are going to experiment with filtering.

a. Before typing the following line of code into the REPL, discuss with your partner what it is doing and predict what the output is. Then type it into the REPL and verify your prediction.

>>> lst = [title(c) for c in list_of_courses]

b. Do the some thing for the following:

>>> lst = [c for c in list_of_courses if available(c) > 0]

c. Write an expression that collects all the courses that have “Introduction” in their title. Remember that you can check if the course c has a title that contains the string “Introduction” by using the following expression: "Introduction" in title(c).split().

d. Write an expression that collects all the 100 level courses.

Exercise 4

In this exercise, we will experiment writing to a file.

a. Create and open an output file by typing:

>>> outfile = open("sorted_courses.txt", "x")

b. Now sort the list_of_courses by title and write all the course titles to the file by typing:

>>> list_of_courses.sort(key=title)
>>> for c in list_of_courses: print(c, file=outfile)

c. Don’t forget to close the file with the close() method!

d. Open up the file and make sure that the contents were written correctly.

e. Now write a sequence of instructions to create a file level_200_courses.txt that contains all the 200 level courses in list_of_courses.