Assignment 4
Type: Individual Assignment
Due: by 10:00 PM on Tuesday, April 23, 2019
- Prologue
- Problem 1: Working with Course Data
- Problem 2: Lists of Numbers
- How to Turn in Your Code
- Important Grading Criteria
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
A huge benefit of computer programming is being able to process large datasets automatically. For example, Carleton tracks the previous course offerings in a special string format, and in this problem, we will be writing algorithms to process that data. In particular, each course can be encoded as a single string such as the following.
example_course = "f18 6 2/30/0 MATH.111.01 (51348) Introduction to Calculus"
These courses strings have 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, most of your code will go into a single file, and we’ve provided you with the following starter file.
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.)
-
course_term(course)
: returns the term of the course as a string (e.g."f18"
) -
course_credits(course)
: returns the credits of the course as afloat
(e.g.6.0
) -
course_available(course)
: returns the number of available spots in the course as anint
-
course_capacity(course)
: returns the course capacity of the course as anint
-
course_waitlist(course)
: returns the size of the course waitlist as anint
-
course_department(course)
: returns the department of the course (e.g."MATH"
) -
course_number(course)
: returns the course number as anint
(e.g.111
) -
course_section(course)
: returns the course section as anint
(e.g.2
) -
course_id(course)
: returns the course ID as anint
(e.g.51348
) -
course_title(course)
: returns the course title as anstr
(e.g."Introduction to Calculus"
)
(Note that the course_term
function is already implemented for you in the starter file as an example.)
To test your functions, you can 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:
>>> from courses import *
>>> example = "f18 6 2/30/0 MATH.111.01 (51348) Introduction to Calculus"
>>> course_term(example)
'f18'
>>> course_id(example)
51348
>>> course_credits(example)
6.0
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 anywhere.
Also keep in mind that any number of spaces can be between the components. For example, your functions should also work on parameters such as:
example = "f18 6 2/30/0 MATH.111.01 (51348) Introduction to Calculus"
To handle these extra spaces, experiment with what the string method split
does when called without any parameter. For example, try executing s.split()
on various strings s
.
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.
-
course_enrollment(course)
: returns the total number of students enrolled in the course as anint
. (You can compute this from the course capacity and the number of available spots.) -
course_level(course)
: returns the level of the course as astr
. 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-03-s19
. 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. (Hint: the string lower()
method will be handy here.)
Part D
In this part, we will write a program to count the number of courses at a certain level. Create a new file called count_courses_with_level.py
. At the top of your file, remember to import your courses.py
library with:
from courses import *
Write a main
function in your count_courses_with_level.py
program that asks the user to type in two things: (1) the name of of a file (e.g. my_courses.txt
) and (2) a course level (e.g. 200
). Your program should then calculate the number of courses at that level in the file and print the result.
Below is an example execution of the program on the my_courses.txt example file of courses.
$ python3 count_courses_with_level.py
What file do you want to examine? my_courses.txt
What level are you interested in? 300
The number of 300 level courses in my_courses.txt is: 23
Note that when you iterate over a file line by line in Python, there will be a trailing newline character \n
at the end of each line. To remove this, you can use the string strip()
method that removes leading and trailing whitespace from a string.
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.
- Correctness. Does your code do what is asked? (e.g. named correctly, input and outputs correct values, etc.)
- Formatting. Is your code formatted so that it is easy to understand?
- Elegance. Is your code concise and elegant? Does it use subroutines when appropriate rather than copying and pasting certain parts?