CPSC150A
Scientific Computing

Activity 13

Dictionaries

Maximum Value

Write a function called maximum_value(a_dictionary), which takes a dictionary. Assume that all values in a_dictionary are integers and that there is at least one entry in a_dictionary. The function should return the largest value in the dictionary.

Example

Test Code Output
print(maximum_value({‘a’:1, ‘b’:2, ‘c’:1})) 2
print(maximum_value({‘a’:1, ‘b’:2, ‘c’:3})) 3

Increment Entry

Write a function called increment_entry(a_dictionary, a_key), which takes a dictionary and a key. Assume that all values in a_dictionary are integers and that a_key is immutable. If a_key is in a_dictionary, the function should increment the value associated with a_key. If a_key is not in a_dictionary, the function should create a new entry that associates a_key with the value 1.

Example

Test Code Output
a_dictionary = {‘a’:1, ‘b’:2, ‘c’:1}
increment_entry(a_dictionary, ‘b’)
print(a_dictionary)
{‘a’: 1, ‘b’: 3, ‘c’: 1}
a_dictionary = {‘a’:1, ‘b’:2, ‘c’:1}
increment_entry(a_dictionary, ‘d’)
print(a_dictionary)
{‘a’: 1, ‘b’: 2, ‘c’: 1, ‘d’: 1}

Histogram

The Caesar cipher that we created in a previous activity is pretty easy to break even if you don’t know how how many places the characters are shifted, there are only 26 possibilities. (It is only 25 possibilities if you assume the shift isn’t 26 characters which is the same as a shift of 0.) A harder cipher to break is a substitution cipher, where each character is mapped to another character in the alphabet. There are 26!, or 403291461126605635584000000, different substitutions to check!

Breaking a substitution cipher is still possible because the mapping doesn’t change the frequency that letters occur. For example, e is the most commonly occuring letter in english text. So whatever letter occurs most frequently in an enciphered text is probably e. A histogram of the occurances of each character in an enciphered text makes breaking a substitution cipher easier.

Details

Create a program that prompts the user for some text and prints a sorted (in descending order) histogram that represents the number of occurrences of the characters in the entered text. The program should use a dictionary to represent the histogram, with the keys of the dictionary as characters and the values of the dictionary as integers.

Example

$ Enter some text: hello
l: ##
h: #
o: #
e: #

$ Enter some text: This is a test!
s: ###
 : ###
i: ##
t: ##
T: #
h: #
a: #
e: #
!: #

Hint

  1. Create a function called compute_histogram(text). The function should return a dictionary that represents a histogram of the number of occurrences of the characters in text. The keys of the dictionary should be characters and the values of the dictionary should be integers.

    You are going to follow the accumulator pattern for this activity. However, you aren’t going to have a single accumulator. You are going to have multiple accumulators all stored in a dictionary. This dictionary is your histogram that you are going to return.

    The for loop should iterate over each of the characters in text. For each character, increment the value of the character’s entry in the dictionary. Note, you must handle the special case of the character not already having an entry in the dictionary.

  2. To print the histogram in descending order, first find the entry with the largest value in the dictionary. Use the dictionary values method and the built-in max function to find the largest value.

    Create a function reverse_lookup(dictionary, value) that returns the key of an entry in dictionary that has the value value. Note, there may be more than one key with the specified value, in this case it doesn’t matter which key associated with the value is returned.

    Use the reverse_lookup function to print the character with the largest value in the dictionary. And use the * operator to print the appropriate number of # characters. Then use the del operator to delete the entry from the histogram dictionary. Repeat this process for the next largest value until the dictionary is empty.

Submission

Please show your source code and run your programs. Only programs that have perfect functionality will be accepted as complete.