# Python test, Spring 2013 # Problem #1 ========================================= def FibList(n): # Take care of the 3 beginning cases... FirstOnes = [ [], [1], [1, 1] ] if n <= 2: return FirstOnes[n] L = [1, 1] for i in range(3, n+1): L.append(L[-1]+L[-2]) return L # Problem #2 ============================================ def ReadData(filename): # we're guaranteed there's data in the file, so we don't need try/except fin = open(filename, 'r') lines = fin.read().split('\n') fin.close() TheList = [] # skip the first line for line in lines[1:]: fields = line.split(',') # make sure there are enough fields, ignore line if not if len(fields) == 3: d={'name':fields[0], 'gender':fields[1], 'grade':int(fields[2])} TheList.append(d) return TheList # Problem #3 =========================================== def TableByTens(filename): # we might as well use the solution we just sweated to produce listdicts = ReadData(filename) print '
Grade range | ' print 'Number of people |
'+str(lownum)+' - '+str(highnum)+' | '+str(n)+' |