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

Info icon This 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]
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern