COMP 200 Elements of Computer Science &
COMP 130 Elements of Algorithms and Computation
Spring 2012

Supplementary exercises

The primary purpose of these exercises is to give you more practice on some of the topics we've been covering, especially problem decomposition, algorithm development, and using dictionaries.

When discussing substitution ciphers, a couple students asked about generalizing such ciphers. The following exercises are based upon that idea.

Assume you have a dictionary that maps items to list of items. We don't care what kinds of items these are. For example, {"a": [1,2,3], "b":[2,4,5], "c":[3,5], "d": [1,6,2]}. What we want is to “reverse” the mapping, in the sense illustrated by the following desired result: {1: ["a","d"], 2:["a","b","d"], 3:["a","c"], 4:["b"], 5:["b","c"], 6:["d"]}. Note that the output has the same form as the input.

We can break the overall problem down into smaller pieces. We need to be able to “reverse” one key/value pair, e.g., "a":[1,2,3}, and do that repeatedly. In order to do that, we need to be able to “reverse” each key/item-from-value-list pair, e.g., "a" and 1, and do that repeatedly.

Turn the above idea into pseudo-code for this problem.

Write the corresponding Python code.