Type: Individual Assignment

Due: by 10:00 PM on Wednesday, January 30, 2019

Prologue

This is an individual assignment. You are expected to work on your own and not collaborate with your classmates. You may certainly discuss concepts with your classmates, but be sure to work through solving the problems individually.

Problem 1: Working with Course Data

In this problem, you will be working with a string encoding of Carleton course information. Below is an example of such a string.

example_course = "f18 6  2/30/0 MATH.111.01 (51348) Introduction to Calculus"

Each course string has the following components which can be separated by any number of space characters.

  • Term. The term of the course; for example, f18 for Fall 2018, w15 for Winter 2015, etc.
  • Credits. The number of course credits (which is usually always 6).
  • Enrollment. The enrollment information which is formated in a Available/Capacity/Waitlist fashion; for example 2/30/0.
  • Course Abbreviation. This includes the department, course number, and section number separated by periods. For example, MATH.111.01.
  • Course ID. The course ID is surrounded by parentheses. For example, (51348).
  • Course Title. The remaining part of the string is the title and can consist of any number of characters including spaces.

Starter File

In this problem, all of your code will go into a single file. We’ve provided a started file for you that contains some sample course data. You can find the file below.

Be sure to add yourselves as authors to the file!

Part A

It would be useful to have easy ways of extracting out the individual parts of a course string. Write the following functions in your courses.py starter file. (Note that all of the functions take a single parameter which is a string-encoded course in the format mentioned above.)

  • term(course): returns the term of the course as a string (e.g. "f18")
  • credits(course): returns the credits of the course as a float (e.g. "6.0")
  • available(course): returns the number of available spots in the course as an int
  • capacity(course): returns the course capacity of the course as an int
  • waitlist(course): returns the size of the course waitlist as an int
  • department(course): returns the department of the course (e.g. "MATH")
  • number(course): returns the course number as an int (e.g. 111)
  • section(course): returns the course section as an int (e.g. 2)
  • id(course): returns the course ID as an int (e.g. 51348)
  • title(course): returns the course title as an str (e.g. "Introduction to Calculus")

To test your functions, it is possible to import your courses.py library into the REPL. To do so, open up a REPL in the same directory as your courses.py file. Then you can access your functions in the following way:

>>> import courses
>>> example = "f18 6  2/30/0 MATH.111.01 (51348) Introduction to Calculus"
>>> courses.term(example)
'f18'
>>> courses.id(example)
51348
>>> courses.credits(example)
6.0

Notice that we imported all the functions in courses.py in the same way we can import the functions from the math module. We also need to prepend the module name, courses, to the beginning of all our function calls since it is an imported library.

Remember that when a function returns a value, it doesn’t print the value. Above, the REPL is showing what value is returned by calling function and not what is printed. None of these functions you write should include a print statement.

Part B

There are other useful pieces of information that we can compute from a string-encoded course. Write the following functions and add them to your courses.py file.

  • enrollment(course): returns the total number of students enrolled in the course as an int. (You can compute this from the course capacity and the number of available spots.)
  • level(course): returns the level of the course as a str. For example, MATH 111 is a 100-level course, so it should return "100" in this case.

Remember that a mantra of computer scientists is Don’t Repeat Yourself (DRY). Reuse the work you already did in Part A by calling those functions instead of copying and pasting code.

Part C

The COURSES directory assigned for our class is cs111-01-w19. This is automatically generated from the course information.

Write a function folder_name(course) that returns the name of the course directory for the course. For this, you’ll need to turn the department abbreviation to lowercase. To do this, consider using the lower() method on strings. Below is an example of how to use it.

>>> dept = "MATH"
>>> dept.lower()
'math'

Notice that the output of dept.lower() is "math" in all lowercase characters.

Part D

It is also nice to compute values based on an entire list of string-encoded courses. Write the following functions.

  • num_courses_at_level(courses, level): given a list of courses courses and a string level such as "100" or "200", returns the total number of courses in courses that have level equal to level.
  • average_enrollment_size(courses): given a list of courses, returns the average enrollment size of all the courses.

Note that the courses parameter in this part is a list of courses. Therefore, to test these functions you should do something like:

>>> from courses import *
>>> num_courses_at_level(some_math_courses, "100")
7
>>> num_courses_at_level(some_math_courses, "200")
13
>>> num_courses_at_level(some_math_courses, "300")
0

Part E

Finish writing the main() function so that it prints out the number of courses at each level in the provided in the some_math_courses list, as well as the average enrollment size of all of the courses. Remember that when you call a function within the same module, you do not need to prepend the module name courses to the beginning of the function.

Problem 2: Lists of Numbers

In this problem, place all of your code into a single file named numbers.py.

Part A

Write a function named closest_to_zero(numbers) that takes a list of numbers as a parameter and returns the closest number to zero in that list. Below are a few sample calls.

>>> closest_to_zero([5,7,3])
3
>>> closest_to_zero([1032.7,-33,13,5.9])
5.9

Part B

Write a function named list_double(numbers) that takes a list of numbers as a parameter and returns a new list where each number in the list is double the value in the original list.

>>> list_double([5,7,3])
[10, 14, 6]
>>> list_double([-3,22,12.3,0])
[-6, 44, 24.6, 0]

Hint: In Part B and also Part C below, you will likely need to use a definite loop of the following structure:

for num in range(len(numbers)):
    # body of loop goes here

This loops over the indices of the list which will allow you to create a new list. Remember that range(5) is a sequence of numbers starting at 0 and ending at 4.

Part C

Write a function named list_double_in_place(numbers) that takes a list of numbers as a parameter and modifies the list so that each number in the list is double the value in the original list. The function should return nothing—it should only modify the list passed into the function.

>>> val = [5,7,3]
>>> list_double_in_place(val)
>>> print(val)
[10, 14, 6]
>>> list_double_in_place(val)
>>> print(val)
[20, 28, 12]

How to Turn in Your Code

a. Make sure that you and your partner are clearly identified at the top of every file you have created for this assignment.

b. All of your files need to be placed into the Hand-in/USERNAME/assignment4 directory. You can do this visually in Finder by copying all of your files in your StuWork/USERNAME/assignment4 directory into your Hand-in/USERNAME/assignment4 directory. (Note that you only need to do this once—it is not necessary for every member of your group to submit separately.)

c. Send all of your files to your partner so they have access to them, too!

Important Grading Criteria

We will evaluate your work with the following criteria in mind.

  1. Correctness. Does your code do what is asked? (e.g. named correctly, input and outputs correct values, etc.)
  2. Formatting. Is your code formatted so that it is easy to understand?
  3. Elegance. Is your code concise and elegant? Does it use subroutines when appropriate rather than copying and pasting certain parts?