Contents

  1. ./visualize_years_dictionary.py
  2. ./years_dictionary2.py
  3. ./years_dictionary.py

./visualize_years_dictionary.py 1/3

[
top][prev][next]
# Put this code into Python Visualizer so you can see what's happening
# to the dictionary.
# by CSCI 111

def main():

    gradYearToCount = {}
    
    while True: 
        year = input("What is the student's grad year? ")

        if year == "":
            break
    
        if year in gradYearToCount:
            gradYearToCount[year] += 1
        else:
            gradYearToCount[year] = 1
    

    for gradYear in sorted(gradYearToCount):
        print(gradYear, gradYearToCount[gradYear])
            
    
main()

./years_dictionary2.py 2/3

[
top][prev][next]
# Given a file of the form <firstname> <classyear>,
# report how many students are in each class year
# by CSCI 111

FILENAME="data/roster.dat"

def main():

    gradYearToCount = countStudentsInEachGraduationYear(FILENAME)
    
    # Quick check of dictionary's contents
    print(gradYearToCount)
    
    for gradYear in sorted(gradYearToCount):
        print(gradYear, gradYearToCount[gradYear])
            

def countStudentsInEachGraduationYear(filename):
    """
    Parses the filename, which is in the form
        name gradyear
        name gradyear
        ...
    Returns a dictionary that maps the graduation year to the number of
    students in that graduation year
    """
    # open the file for reading
    yearsFile = open(filename, "r")
    
    # accumulate the dictionary
    yearToCount = {}
    
    # go through the file line by line
    for line in yearsFile: 
        # add the info on the line to the dictionary
        contentsList = line.split()
        gradYear = contentsList[1]
        if gradYear in yearToCount:
            # add 1 to the count for that year
            yearToCount[gradYear] += 1
            
            """
            Alternatives
            yearToCount[gradYear] = yearToCount[gradYear] + 1
            
            count = yearToCount[gradYear]
            yearToCount[gradYear] = count + 1
            """
        else:
           yearToCount[gradYear] = 1
    
    
    # close the file
    yearsFile.close()
    
    return yearToCount
    

    
main()

./years_dictionary.py 3/3

[
top][prev][next]
# Given a file of the form <firstname> <class>
# creates a mapping between the first names and class
# (In progress...)
# by CSCI 111

FILENAME="data/roster.dat"

def main():

    #nameToYear = { "Joey" : 24, "Dawson": 23, "Jen": 25, "Pacey": 26, "Jack": 24}
    nameToYear = createDictionaryFromFile(FILENAME)
    
    # For debugging: display the info -- is it correct?
    # print(nameToYear)
    
    #for firstName in sorted(nameToYear):
    #    print(firstName, nameToYear[firstName])
    
    print("This program will repeatedly prompt you for a")
    print("student's first name and tell you their graduation year.")
    print("It stops if you enter nothing for a name.")
    
    #repeatedly (until the user enters nothing)
    while True:
        # prompt for the student name and display their class year
        studentName = input("What is the student's name? ")
        
        if studentName == "":
            break
            
        if studentName in nameToYear:
            print(studentName, "is in the class of", nameToYear[studentName])
        else:
            print(studentName, "is not in our directory")
            
            
    
def createDictionaryFromFile(filename):
    """
    Parses the filename, which is in the form
        name gradyear
        name gradyear
        ...
    Returns a dictionary that maps each name to gradyear
    """
    # open the file for reading
    yearsFile = open(filename, "r")
    
    # initialize dictionary
    firstNameToGradYear = {}
    
    # go through the file line by line
    for line in yearsFile: 
        # add the info on the line to the dictionary
        contentsList = line.split()
        firstname = contentsList[0] 
        gradYear = contentsList[1]
        firstNameToGradYear[ firstname ] = gradYear
    
    # close the file
    yearsFile.close()
    
    return firstNameToGradYear

main()





Generated by GNU Enscript 1.6.5.90.