Introduction to Computer Science and Programming
Problem Set 8: Dynamic Programming
: Tuesday, October 21, 2008
Thursday, October 30, 2008
At an institute of higher education that shall be nameless, it used to be the case that a human advisor would help each student
formulate a list of subjects that would meet the student’s objectives. However, because of financial troubles, the Institute has
decided to replace human advisors with software. Given the amount of work a student wants to do, the program returns a list of
subjects that maximizes the amount of value.
The goal of this problem set is to implement a dynamic programming algorithm and learn how to pass functions as arguments.
Please let us know how long you spend on each problem. We want to be careful not to overload you by giving out problems that
take longer than we anticipated.
You may work with other students. However, each student should write up and hand in his or her assignment separately. Be sure
to indicate with whom you have worked. For further details, please review the collaboration policy as stated in the syllabus.
Download and save these files into the same folder.
ps8.py: the skeleton you’ll fill in for Problems 1–5
subjects.txt: the list of subjects with value and work information.
Problem 1: Building A Subject Dictionary
The first step is to implement
, which loads a list of subjects from a file. Each line of the file
contains a string of the form “name,value,work”, where the name is a string, the value is an integer indicating how much a
student learns by taking the subject, and the work is an integer indicating the number of hours a student must spend to pass
should return a dictionary mapping subject names to tuples, where the tuples are pairs of
(learning value, work hours) integers.
Returns a dictionary mapping subject name to (value, work), where the name
is a string and the value and work are integers. The subject information is
read from the file named by the string filename. Each line of the file
contains a string of the form "name,value,work".
returns: dictionary mapping subject name to (value, work)
# The following sample code reads lines from the specified file and prin