Contents
- descendSort2.py
- descendSort.py
- majors_dictionary.py
- years_dictionary2.py
descendSort2.py 1/4
[top][prev][next]
# Demonstrate passing lists to functions
# CS111
def main():
# test descendSort3Nums
list = [1,2,3]
descendSort3Nums(list)
print list
list = [0, 5, -3]
descendSort3Nums(list)
print list
list = [7,4,1]
descendSort3Nums(list)
print list
# input: a list containing three numbers
# sorts the list in descending order
# Note: does not return anything, no output
def descendSort3Nums(list3):
list3.sort()
list3.reverse()
main()
descendSort.py 2/4
[top][prev][next]
# Demonstrate passing lists to functions
# CS111
def main():
# test descendSort3Nums
list = [1,2,3]
descendSort3Nums(list)
print list
list = [0, 5, -3]
descendSort3Nums(list)
print list
list = [7,4,1]
descendSort3Nums(list)
print list
# input: a list containing three numbers
# sorts the list in descending order
# Note: does not return anything, no output
def descendSort3Nums(list3):
if list3[1] > list3[0]:
# swap 'em
tmp = list3[0]
list3[0] = list3[1]
list3[1] = tmp
if list3[2] > list3[1]:
# swap 'em
tmp = list3[1]
list3[1] = list3[2]
list3[2] = tmp
if list3[1] > list3[0]:
# swap 'em
tmp = list3[0]
list3[0] = list3[1]
list3[1] = tmp
main()
majors_dictionary.py 3/4
[top][prev][next]
# Read in majors from a file and put into a dictionary
# that maps the major to how many students have that major.
# by Sara Sprenkle
UNDECLARED="Undeclared"
def main():
MAJORSFILE="data/majors.all.dat"
majorsData = file(MAJORSFILE, "r")
# create an empty dictionary that will map last names to majors
majorToCount={}
for line in majorsData:
line = line.strip()
# FIRST " " SPLITS NAME AND MAJORS
spacePos = line.find(" ")
if spacePos== -1:
# person is undeclared
addStudent( majorToCount )
else:
name = line[:spacePos]
# print "name=", name
major = line[spacePos+1:]
# print "major=", major
addStudent( majorToCount, major)
majorsData.close()
printMajorInfo(majorToCount)
# Update the mapping of majors to number of majors.
# Input: dictionary of majors to number of majors (majorToCount),
# and optional string major (defaults to Undeclared)
def addStudent(majorToCount, major=UNDECLARED):
if majorToCount.get(major) != None:
majorToCount[major]+=1
else:
majorToCount[major]=1
# Print the number of majors for each major, sorting
# alphabetically by the major
# Input: mapping of majors to the number of majors
def printMajorInfo(majorToCount):
# not actually sorted at this point
sortedKeys = majorToCount.keys()
sortedKeys.sort()
print "%25s %5s" %( "MAJOR", "NUM")
print "-"*31
for major in sortedKeys:
count = majorToCount[major]
print "%25s %5d" % (major, count)
main()
years_dictionary2.py 4/4
[top][prev][next]
# Read in years from a file and put into a dictionary that maps years to number
# of students in that year.
# Also provides an example of modifying a dictionary, passed as a parameter.
# CS111
def main():
YEARSFILE="data/years.dat"
yearsData = file(YEARSFILE, "r")
# create an empty dictionary that will map last names to years
yearToCount={}
for line in yearsData:
line = line.strip()
# FIRST " " SPLITS NAME AND YEARS
spacePos = line.find(" ")
if spacePos== -1:
print "Error: no name/year pair in line:", line
else:
name = line[:spacePos]
# print "name=", name
year = line[spacePos+1:]
# print "year=", year
updateFreq(yearToCount, year)
yearsData.close()
# print the information nicely
for year in yearToCount:
count = yearToCount[year]
if count > 1:
print "There are", count, year + "s."
else:
print "There is 1", year + "s."
# Update the mapping in the dictionary for the
# frequency of the key
def updateFreq( dictionary, key ):
if dictionary.get(key) != None:
dictionary[key]+=1
else:
dictionary[key]=1
main()
Generated by GNU enscript 1.6.4.