For this exercise, you may work alone or with one partner.
Solve at least two of the following practice sets.
Be sure to include docstrings describing what the function does.
Let us know if you need help. The teaching team will come to you.
Once you have completed,
please ask the teaching team to record your participation and you may leave.
(Thanks to Sean Gatewood and Alexander Monaco for creating these practice problems.
Oh no! Professor Upsorn is doing course advising for her favorite 1111 TA, Alexander, when disaster strikes. He has submitted his planned course schedule for 2019 to her in a Python dictionary format! Fortunately for you, he is only planning on taking one course per subject with a total of six subjects. Each department has its own key, with each value being a different course. Help Professor Upsorn advise in time so she can go back to her teaching!
[ inclass-dict1.py ]
Sample solution # Oh no! Professor Upsorn is doing course advising for her favorite 1111 TA, Alexander, when disaster strikes. # He has submitted his planned course schedule for 2019 to her in a Python dictionary format! # Fortunately for you, he is only planning on taking one course per subject with a total of six subjects. # Each department has its own key, with each value being a different course. Help Professor Upsorn advise in time # so she can go back to her teaching! # His course plan was submitted as follows: FavoriteTACourseLoad = { "CS": "2150", "FREN": "2010", "MUSI": "3320", "ASTR": "3420", "CHIN": "1010", "BIOL": "9999" } # 1. How could Professor Upsorn quickly access the course value of a department in the list? print(FavoriteTACourseLoad["CS"]) # or x = (FavoriteTACourseLoad["CS"]) print(x) # 2. What is a way for Professor Upsorn to print out the classes (department + value) # in a neater manner than simply using print()? # Hint: how could you do this with a loop? for i in FavoriteTACourseLoad: print(i + " " + FavoriteTACourseLoad[i]) # 3. What could Professor Upsorn do to check if a certain department is present in the dictionary? # example using Chemistry as department if "CHEM" in FavoriteTACourseLoad: print("CHEM is present in the courseload") else: print("CHEM is not present in the courseload") # 4. If she wanted to give exceptionally bad advice and force Alexander to take more than six courses, # how could Professor Upsorn manually add a department and course number? # example with PLAP 1010 FavoriteTACourseLoad["PLAP"] = "1010" print(FavoriteTACourseLoad) # 5. If Professor Upsorn realized that BIOL 9999 is a graduate level class, how could she # forcibly remove it from the planned courseload? FavoriteTACourseLoad.pop("BIOL") print(FavoriteTACourseLoad) # 6. If Professor Upsorn wanted to change the Chinese class to be level 1020, how would she do this? FavoriteTACourseLoad["CHIN"] = "1020" print(FavoriteTACourseLoad) # 7. (Challenge): What could Professor Upsorn do to quickly check the course level for every department in the thousands and print it? # For example, printing ASTR in the normal dictionary would print "ASTR course is 3000 level." # Hint, use a loop like in #2. for g in FavoriteTACourseLoad: thousands = FavoriteTACourseLoad[g][0] + "000" print(g + " course is " + thousands + " level."
Dear 1111 Engineers,
Here's the deal: I have three giant lists, which together store information about restaurants, items, and prices.
For instance, here's a small section of each:
restaurants = ["Sheetz", "Mellow Mushroom", "Mellow Mushroom", "College Inn", "Sheetz"] items = ["Hot Dog", "Garlic and Spinach Pizza", "Salad", "Spaghetti", "Marinara Sub"] prices = [2.99, 15.99, 12.99, 9.99, 5.99]
However, I keep getting in trouble at restaurants for taking too long with all this crazy indexing! Can you write a function to make me a dictionary that makes this lookup process easier? For instance, given those lists, I should be able to do:
print("A Sheetz hot dog is $", d["Sheetz"]["Hot Dog"], sep="") # prints "A Sheetz hot dog is $2.99" print("Mellow salad is $", d["Mellow Mushroom"]["Salad"], sep="") # prints "Mellow salad is $12.99" # the keyword parameter sep can be used to redefine the seperator between values
Help me, CS 1111. You're my only hope.
- Sean
[ inclass-dict2.py ]
Sample solution def add(dictionary, key1, key2, value): if key1 not in dictionary: dictionary[key1] = {} dictionary[key1][key2] = value def make_dict(restaurants, items, prices): global d for i in range(len(restaurants)): r, i, p = restaurants[i], items[i], prices[i] add(d, r, i, p) d = {} restaurants = ["Sheetz", "Mellow Mushroom", "Mellow Mushroom", "College Inn", "Sheetz"] items = ["Hot Dog", "Garlic and Spinach Pizza", "Salad", "Spaghetti", "Marinara Sub"] prices = [2.99, 15.99, 12.99, 9.99, 5.99] make_dict(restaurants, items, prices) print("A Sheetz hot dog is $", d["Sheetz"]["Hot Dog"], sep="") print("Mellow salad is $", d["Mellow Mushroom"]["Salad"], sep="")
Write a function pop(dictionary,lst) that:
d = { "2+2" : 4, "3+3" : 6, "4+4" : 8, "spaghetti" : "sauce" } lst_to_remove = ["2+2", "4+4", "spaghetti", "Upsorn"] z = pop(d, lst_to_remove) print("z =", z) # prints "z = [4, 8, 'sauce', None]" print("d =", d) # prints "d = {'3+3': 6}"
[ inclass-dict3.py ]
Sample solution def pop(dictionary,lst): out = [] for key in lst: if key in dictionary: out.append(dictionary.pop(key)) else: out.append(None) return out
Write a function that combines two dictionaries.
If a key is in both dictionaries, one of two things should occur:a = { "2+2" : 4, "3+3" : 6, "4+4" : 8, "spaghetti" : "sauce" } b = { "2+2" : 5, "10+3" : 13, "3+3" : 6, "Alex" : "Monaco" } d = combine(a, b) for key in d: print(key, "=", d[key])
2+2 = CONFLICT! 3+3 = 6 4+4 = 8 spaghetti = sauce 10+3 = 13 Alex = Monaco
[ inclass-dict4.py ]
Sample solution def a2b(a, b, out): for key in a: if key in out: continue value = a[key] if key in b and b[key] != value: out[key] = "CONFLICT!" else: out[key] = value return out def combine(a, b): out = {} out = a2b(a, b, out) out = a2b(b, a, out) return out