In this practice HW you will create 2 programs that use data structures (i.e., sets, lists, tuples, and dictionaries). Make sure to read the hints & recommended development steps for each program.
Rx: <10 min Av: 10-20 min Sd: 20-30 min DNF: 30+ min
Create a program sets.py
that declares the following set:
s = {"A", "B", "C", "D", "E"}
and prints a random element of the set until the first element is sampled. In other words, the while loop continues printing letters in the set s
until “A” is the next letter. The output should have no “A”’s.
Then, create another set:
r = {"A", "C", "E", "G"}
and print the results of the following set operations: union, intersection, and difference.
Hints:
The following code randomly selects an element from a set:
import random
s = {"A", "B", "C", “D”, “E”}
print(random.sample(s, 1))
For more information see the random sample function.
I recommend following the sentinel while loop pattern:
value = initialize
while value != sentinel:
process value
value = updated value
Recommended Development Steps:
s
and print it outs
instead (see hints above)r
and print out the union with s
Create a program dictionary.py
that creates a simple dictionary with two keys, even & odd, that stores the set of even & odd numbers from 1 to 10 and prints it out.
Hints:
set()
function to initialize an empty set.Recommended Development Steps:
d
instead. For example, if you had a set named even
, replace every occurrence of that variable with d['even']
. Make sure you initialize d
to be an empty dictionary (d = {}
). Print out the dictionary after your loop to confirm the behavior is the same as in the prior step.Once you’ve finished doing the HW a single time, you can watch me do it:
You’ll learn significantly less from watching me solve the HW if you haven’t attempted the HW yourself first.
While it’s an achievement to finish the HW no matter how long it takes, you might experience “diminishing returns” if you work longer than the DNF (Did Not Finish) time. Thus, it might be strategic to stop working at the DNF time and watch my solution.
After watching my solution, I recommend that you repeat the HW if you have not achieved at least Av (Average) performance. If so, be sure to:
Feel free to keep trying until you make Rx if that’s of interest to you.