Contents

  1. ascii_dictionary.py
  2. using_dictionary.py
  3. years_dictionary2.py
  4. years_dictionary3.py
  5. years_dictionary.py

ascii_dictionary.py 1/5

[
top][prev][next]
# Demonstrate use of dictionary, using ASCII values
#

# create an empty dictionary
ascii= {}

ordValue = ord('a')

while ordValue <= ord('z'):
    # add mapping to dictionary of chr(ordValue) --> ordValue (ordinal value)
    char = chr(ordValue)
    ascii[char] = ordValue
    ordValue += 1

print(ascii)

using_dictionary.py 2/5

[
top][prev][next]
# Demonstrate use of dictionary, using ASCII values
#

# create an empty dictionary
ascii= {}

x = ord('a')

while x <= ord('z'):
    # add mapping to dictionary of chr(x) --> x (ordinal value)
    char = chr(x)
    ascii[char] = x
    x+=1

# iterates through the keys in the dictionary
for letter in ascii:
    # print the key and its associated value
    print(letter, ascii[letter])

# display the type that is returned by dictionary methods
print(type(ascii.keys()))
print(type(ascii.values()))
print("The number of keys is", len(ascii.keys()))

# iterate through the values
print("Iterate through the values:")
for val in ascii.values():
    print(val)
    
keyList = list(ascii.keys())
print("as <dict_keys>:\n", ascii.keys())
print("as a list:\n", keyList)

# printing in order by key
keysSorted = list(ascii.keys())
keysSorted.sort()

for letter in keysSorted:  # alternative: sorted(keysSorted)
    # print the key and its associated value
    print(letter, ascii[letter])

    
for letter in sorted(ascii):
    print(letter, ascii[letter])


years_dictionary2.py 3/5

[
top][prev][next]
# Given a file of the form <lastname> <class>
# creates a mapping between the last names and class.
# Prompts the user for a student's last name and displays
# the student's *expected graduation year*.
# by CSCI 111

FILENAME="data/years.dat"

classYearToExpectedGraduationYear = { 'Ugr:Senior': 2016, 'Ugr:Junior': 2017, 'Ugr:Sophomore':2018, 'Ugr:First-Year':2019}

# open the file for reading
yearsFile = open(FILENAME, "r")

# create an empty dictionary that maps the student's last name to the class year
lastnameToClassMap = {}

for line in yearsFile:
    # form of the line is:
    # lastname class
    data = line.split()
    lastname = data[0] # first element is the lastname
    classname = data[1] # second element is the class
    
    # add a mapping from the lastname to the classname
    lastnameToClassMap[lastname] = classname

yearsFile.close()

# Show that reading in the file worked correctly: 
#for lastname in sorted(lastnameToClassMap):
#    print(lastname, "-->", lastnameToClassMap[lastname])

while True:
    lastname = input("What student do you want to check on? ")
    
    # First, check if that lastname is a valid key
    if lastname in lastnameToClassMap:
        classYear = lastnameToClassMap[lastname]
        gradYear = classYearToExpectedGraduationYear[classYear]
        print(lastname, "is a", classYear, "and is expected to graduate in", gradYear)
    else:
        print(lastname, "is not a valid key")
    
    
    

years_dictionary3.py 4/5

[
top][prev][next]
# Given a file of the form <lastname> <class>
# creates and displays a mapping between the class
# and number of students in the class.
# by CSCI 111

FILENAME="data/years.dat"

# open the file for reading
yearsFile = open(FILENAME, "r")

# create an empty dictionary that maps the class year to the number of students in the class

classYearToNumberOfStudents = { }


for line in yearsFile:
    # form of the line is:
    # lastname class
    data = line.split()
    classname = data[1] # second element is the class
    
    # if it's a new classname, add a new entry
    if classname not in classYearToNumberOfStudents:
        classYearToNumberOfStudents[classname] = 1
    else: # I've seen this before
        currCount = classYearToNumberOfStudents[classname]
        currCount += 1
        classYearToNumberOfStudents[classname] = currCount
    
    # alternative way to write this
    #classYearToNumberOfStudents[classname] += 1
    

yearsFile.close()

for year in classYearToNumberOfStudents:
    print(year, classYearToNumberOfStudents[year])

years_dictionary.py 5/5

[
top][prev][next]
# Given a file of the form <lastname> <class>
# creates a mapping between the last names and class.
# Prompts the user for a student's last name and displays
# the student's class year
# by CSCI 111

FILENAME="data/years.dat"

# open the file for reading
yearsFile = open(FILENAME, "r")

# create an empty dictionary that maps the student's last name to the class year
lastnameToClassMap = {}

for line in yearsFile:
    # form of the line is:
    # lastname class
    data = line.split()
    lastname = data[0] # first element is the lastname
    classname = data[1] # second element is the class
    
    # add a mapping from the lastname to the classname
    lastnameToClassMap[lastname] = classname

yearsFile.close()

# Show that reading in the file worked correctly: 
#for lastname in sorted(lastnameToClassMap):
#    print(lastname, "-->", lastnameToClassMap[lastname])

while True:
    lastname = input("What student do you want to check on? ")
    
    # Issue with this solution, alone:
    #print(lastname, "is a", lastnameToClassMap[lastname])
    # --> If user enters a lastname that doesn't exist as a key in the
    # dictionary, program exits with a KeyError.
    # Solution: first, check if that lastname is a valid key
    if lastname in lastnameToClassMap:
        classYear = lastnameToClassMap[lastname]
        print(lastname, "is a", classYear)
    else:
        print(lastname, "is not a valid student")
    
    
    

Generated by GNU Enscript 1.6.6.