Contents

  1. ./years_dictionary2.py
  2. ./years_dictionary.py

./years_dictionary2.py 1/2

[
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")
    
    # inintialize dictionary
    gradYearToCount = {}
    
    # 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]
        
        # if gradYear is in the dictionary, 
        # add one to its count
        if gradYear in gradYearToCount:
            gradYearToCount[gradYear] += 1
            # alternative solution
            #gradYearToCount[gradYear] = gradYearToCount[gradYear] + 1
        # if it's not in the dictionary
        # map the gradYear to 1
        else:
            gradYearToCount[gradYear] = 1
    
    # close the file
    yearsFile.close()
    
    return gradYearToCount
    
main()

./years_dictionary.py 2/2

[
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")
    
    # inintialize 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.