a8q4 - cmDict[cm] = cmDict[cm].append(license) for cm in...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
##CS 116 Assignment 8 Question 4 ## Ru Wang r46wang 20319247 ##build_car_maker: dictionary[string: string] -> dictionary[string: (listof string) [sorted]] ## purpose: this function consumes a car registry dictionary in the form of key:value as license_plate:car_maker with license_plate and car_maker both strings ## and produces a car maker dictionary in the form of key:value as car_maker: [license_plate, license_plate. ..] with the list of license_plate sorted in alphabetical order ## example: ## build_car_maker({}) => {} ## build_car_maker({"CCC 333":"Mazda", "AAA 111":"Mazda", "DDD 444":"Mazda", "BBB 222":"Mazda",}) ## => {"Mazda": ["AAA 111", "BBB 222", "CCC 333", "DDD 444"]} ## build_car_maker({"ABCD 123":"Mazda", "LFS 000":"Kia", "WINNR":"Ford", "XTRA":"Ford", "UR GR8":"Mazda"}) ## => {"Kia":["LFS 000"], "Ford":["WINNR", "XTRA"], "Mazda":["ABCD 123", "UR GR8"]} def build_car_maker(registry): cmDict = {} for license in registry.keys(): cm = registry[license] if not cmDict.has_key( cm ):
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: cmDict[cm] = cmDict[cm].append(license) for cm in cmDict.keys(): cmDict[cm].sort() return cmDict ##Testings: ## build_car_maker({}) => {} print "Test 1" expect = {} ans = build_car_maker({}) print expect == ans assert expect == ans ## build_car_maker({"CCC 333":"Mazda", "AAA 111":"Mazda", "DDD 444":"Mazda", "BBB 222":"Mazda",}) print "Test 2" expect = {"Mazda": ["AAA 111", "BBB 222", "CCC 333", "DDD 444"]} ans = build_car_maker({"CCC 333":"Mazda", "AAA 111":"Mazda", "DDD 444":"Mazda", "BBB 222":"Mazda",}) print expect == ans assert expect == ans ## build_car_maker({"ABCD 123":"Mazda", "LFS 000":"Kia", "WINNR":"Ford", "XTRA":"Ford", "UR GR8":"Mazda"}) print "Test 3" expect = {"Kia":["LFS 000"], "Ford":["WINNR", "XTRA"], "Mazda":["ABCD 123", "UR GR8"]} ans = build_car_maker({"ABCD 123":"Mazda", "LFS 000":"Kia", "WINNR":"Ford", "XTRA":"Ford", "UR GR8":"Mazda"}) print ans == expect assert expect == ans...
View Full Document

Ask a homework question - tutors are online