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
Submission
Please show your source code and run your programs. Only programs that have perfect functionality will be accepted as complete.