Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def loadSubjects(filename):
- """
- 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 prints
- # each one.
- inputFile = open(filename)
- subDict = {}
- for line in inputFile:
- lSplit = string.split(string.strip(line),',')
- subDict[lSplit[0]] [VALUE] = (int(lSplit[1]),int(lSplit[2]))
- return subDict
- # TODO: Instead of printing each line, modify the above to parse the name,
- # value, and work of each subject and create a dictionary mapping the name
- # to the (value, work).
- def printSubjects(subjects):
- """
- Prints a string containing name, value, and work of each subject in
- the dictionary of subjects and total value and work of all subjects
- """
- totalVal, totalWork = 0,0
- if len(subjects) == 0:
- return 'Empty SubjectList'
- res = 'Course\tValue\tWork\n======\t====\t=====\n'
- subNames = subjects.keys()
- subNames.sort()
- for s in subNames:
- val = subjects[s][VALUE] ## What is this syntax?
- work = subjects[s][WORK] ## What is this syntax?
- res = res + s + '\t' + str(val) + '\t' + str(work) + '\n'
- totalVal += val
- totalWork += work
- res = res + '\nTotal Value:\t' + str(totalVal) +'\n'
- res = res + 'Total Work:\t' + str(totalWork) + '\n'
- print res
Advertisement
Add Comment
Please, Sign In to add comment