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 ]
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 ]
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 ]
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 ]