Preparation

a. Create a folder called lab-10-22 in your StuWork directory.

b. Open a Terminal and navigate to your lab-10-22 directory.

c. Create a file named data_processing.py and put all of the code from it today in there.

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.

some_math_courses = [
    "f18 6  2/30/0 MATH.111.01 (51348) Introduction to Calculus",
    "f18 6  4/30/0 MATH.111.02 (51349) Introduction to Calculus",
    "f18 6  4/30/0 MATH.111.03 (51350) Introduction to Calculus",
    "f18 6 -1/25/1 MATH.120.01 (51351) Calculus 2",
    "f18 6 -4/25/2 MATH.120.02 (51352) Calculus 2",
    "f18 6 -2/25/0 MATH.120.03 (51353) Calculus 2",
    "f18 6 -2/25/3 MATH.120.04 (51354) Calculus 2",
    "f18 6 22/30/0 MATH.210.00 (51368) Calculus 3",
    "f18 6  2/25/2 MATH.211.01 (51355) Multivariable Calculus",
    "f18 6 -1/25/4 MATH.211.02 (51356) Multivariable Calculus",
    "f18 6 -3/25/2 MATH.211.03 (51357) Multivariable Calculus",
    "f18 6 -1/32/5 MATH.215.01 (51359) Introduction to Statistics",
    "f18 6 4/32/10 MATH.215.02 (51360) Introduction to Statistics",
    "f18 6 -1/23/1 MATH.232.01 (51361) Linear Algebra",
    "f18 6 10/30/0 MATH.232.02 (51369) Linear Algebra",
    "f18 6  6/23/0 MATH.236.00 (51362) Mathematical Structure",
    "f18 6  3/24/1 MATH.245.00 (51363) Applied Regression Analysis",
    "f18 6 11/30/0 MATH.251.00 (51370) Chaotic Dynamics",
    "f18 6  2/30/0 MATH.265.00 (51364) Probability",
    "f18 6  3/30/0 MATH.265.02 (51365) Probability"]

a. Copy the above list of courses into your data_processing.py file.

b. Look up your assignment 4 solution and copy and paste the following methods from your assignment: course_title, course_number, and course_available.

c. Open up a REPL and import your file for today.

>>> from data_processing import *

d. Now let’s experiment with the sort method. Try typing in the following:

>>> some_math_courses.sort()

Why do you think the order of the courses came our this way?

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

>>> some_math_courses.sort(key=course_title)

f. Now try sorting by the number of available spots in the course.

g. Now try sorting by the course number but in reverse order.

Exercise 2

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 = [course_title(c) for c in some_math_courses]

b. Do the some thing for the following:

>>> lst = [c for c in some_math_courses if course_available(c) > 0]

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

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

Exercise 3

Notice that we’ve been representing the courses as one long string this whole time. This means that when working with course data, we always have to be thinking about the encoding of the courses.

a. Discuss with your partner what the benefits may be of designing a class called Course that encapsulates the course data.

b. List what data you think should be stored within the object and give them names. Will you just store the course string, or will you store each component of the course separately? Why?

c. Implement your class called Course with a constructor that takes in a string-encoded course and initializes the object along with methods for extracting the title and course number.

class Course:
    def __init__(self, course_string):
        """Creates a course object from the given string encoding"""

        # Not yet implemented

    def getTitle():
        """Returns the title of the course"""

        # Not yet implemented

    def getNumber():
        """Returns the course number of the course"""

        # Not yet implemented

d. Close and reopen your REPL and test out your new class.

>>> c = Course("f18 6  2/30/0 MATH.111.01 (51348) Introduction to Calculus")
>>> c.getTitle()
>>> c.getNumber()

e. Now you can convert all of the math courses to Course objects using the following:

>>> courses = [Course(c) for c in some_math_courses]

f. Now sort the courses by title by typing:

>>> courses.sort(key=Course.getTitle)