# CS111 Dictionaries lab # Fall 2020 # # ------------------------------------------------------------ # # # --------------------------- # Provided code; do not edit # --------------------------- from nameYear import * def makeCS111Dict(students): '''Returns a name:year dictionary built from the nameYear.py file ''' newdict = {} for person in students: # person looks like this: ['Kathryn', '2021'] name = person[0] year = person[1] newdict[name] = year return newdict # ------------------------------------------------------------ # Write your invertDict function here # ------------------------------------------------------------ def invertDict(origDict): # return an inverted dictionary inverted = {} for student in origDict: year = origDict[student] if year in inverted: # year already exists as key in new dict inverted[year].append(student) else: # create new key:value pair inverted[year] = [student] # make sure it is a list return inverted #--------------# # Testing code # #--------------# def test(): """ Put all of your testing code in this function. Feel free to modify or comment out the tests that are already here. """ print("Start of testing.") print("Create the 111 dict") cs111dict = makeCS111Dict(nameYear) print(len(cs111dict)) print("Invert the 111 dict") inverted = invertDict(cs111dict) print(inverted.keys()) print("**2021**\n",inverted['2021']) print("**2022**\n",inverted['2022']) print("**2023**\n",inverted['2023']) print("**2024**\n",inverted['2024']) print("Done with testing.") #---------------# # Testing setup # #---------------# # Do not modify this code; if you want to test something, write code in # the `test` function above. if __name__ == "__main__": test()