Question: For problem #7, you'll be reimplementing the character-frequency histogram functions you created for problem #6, only this time, you'll be using dictionaries instead of lists

For problem #7, you'll be reimplementing the character-frequency histogram functions you created for problem #6, only this time, you'll be using dictionaries instead of lists of two-item lists. More specificially, you should be representing a histogram as a dictionary where the keys are single-character strings and the values are integers (representing the frequencies). So for example, the character-frequency histogram for "Banana!"would be {'B':1, 'a':3, 'n':2, '!':1}.

a. Put all of the functions for this problem into a Python file called char_hist2.py. As before, include a comment explaining the purpose of the module (the same as with problem #6), and another comment describing the data definition you'll be using to represent the character histograms (different from problem #6).

b. In this case, you don't need to define an in_hist() function. This is because using our new data definition for histograms, there's already a very simple command that can be used to determine whether or not a particular character appears in the histogram.

What command would you use to determine whether the character 'x' occurs in the histogram hist1?

Put your answer as a new comment under the previous two comments.

c. Reimplement the get_freq() function so that it works with your new dictionary-based histograms.

So for example, get_freq( 'a', {'B':1, 'a':3, 'n':2, '!':1} ) should return 3 and get_freq( 'X', {'B':1, 'a':3, 'n':2, '!':1} ) should return 0.

Hint: This should be a lot easier than the version you created for char_hist1.py. No loops are needed at all.

d. Define a function make_hist() that takes a string and makes a new histogram from that string.

So make_hist("Banana!") should return {'B':1, 'a':3, 'n':2, '!':1}.

Hint: You might (or might not) want to define a version of add_to_hist() to use as a helper function.

e. Define a function histoprint() that takes a dictionary representing a histogram, and prints out a "graphical" representation of the histogram, one entry per output line. Each printed line should represent a key-value pair in the dictionary: starting with the key (a single character string), followed by a colon, a space, and then a number of hash symbols (#) representing the size of the value. For example, histoprint(make_hist("Mississippi")) should look something like this:

i: #### M: # p: ## s: #### 

Please note that the order in the above printout is arbitrary, since dictionaries do not have a predefined order, so your printout from histoprint(make_hist("Mississippi")) might also look something like this:

M: # i: #### p: ## s: #### 

Hint: Remember that you can concatenate together however many copies of a string you want (such as "#") using the * operator.

Used topics in class: strings, objects, selections, lists, dictionaries, tuples, loops, functions. The book used in class Introduction to Programming with Python

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!