Practice HW11: Sets & Dictionaries

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

sets.py

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:

Recommended Development Steps:

  1. Create the set s and print it out
  2. Print out a random sample of 1 element from s instead (see hints above)
  3. Use the sentinal template in the hints above to create a while loop that prints out a random letter until the value is “A”
  4. Create the set r and print out the union with s
  5. Print out the intersection and difference

dictionary.py

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:

Recommended Development Steps:

  1. Write a loop that prints out the numbers from 1 to 10
  2. In this loop, print out whether the number is even or odd
  3. Create a set for even and a set for odd numbers. Instead of printing in the loop, add the number to the appropriate set. Print out the sets after the loop.
  4. Replace your set variables by adding them to a dictionary 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.

Demonstration

Once you’ve finished doing the HW a single time, you can watch me do it:

Standard Practice HW Caveats

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.