Question: PYTHON 3.6.2 SHELL This is an exercise in implementing functions to solve a large problem. Some of the function specifications are already dictated by the

PYTHON 3.6.2 SHELL

This is an exercise in implementing functions to solve a large problem. Some of the function specifications are already dictated by the assignment below, but a student is welcome to define any other functions that might be helpful in solving the problem.

Overall Problem

This assignment is to take a collection of values, and then report its distribution of values, showing the most frequent elements first, along with how many times those elements appear.

Here are two examples:

print_sorted_by_frequency([1,4,6,2,7,4,8,2,4,1,8,2,7,4,2,7,4]) 4 2 7 1 8 6 5 4 3 2 2 1 print_sorted_by_frequency('Mississippi') i s p M 4 4 2 1 

Function Specification

Your submission must include these functions, with matching interfaces. The grader will be testing your functions by calling them, so they must be consistent with the tests.

For full credit, maximize code reuse by calling existing functions when possible. After all, one primary purpose of breaking a problem into smaller functions is to avoid any need to cut and paste large portions of identical code.

def get_frequency(data): """Creates a dictionary mapping each element to its frequency""" def sorted_by_frequency(data): """Returns a list of elements in a data and a list of frequencies, most frequent first""" def print_sorted_by_frequency(data) """Displays elements and their frequencies, most frequent first""" 

Following your three function definitions should be these sample tests, plus any others you wish. NOTE: The grader will be using other data for testing in addition to yours.

numbers = [1,4,6,2,7,4,8,2,4,1,8,2,7,4,2,7,4 ] print( get_frequency(numbers) ) # shows the dictionary print_sorted_by_frequency(data ) # displays as above print_sorted_by_frequency('Mississippi') # displays letter frequency as above sort_data = sorted_by_frequency(numbers) print("The most frequent value is ", ) 

For this last one, fill in the blank with the simplest expression you can use for it.

Function Hints

get_frequency: Go through the data one value at a time, and update the dictionary for each value, depending on whether you are encountering it for the first time or not.

sorted_by_frequency: the dictionary can already automatically give you both lists in matching orders, but they will not be sorted. There is a "sorted" function that can produce a sorted list. As you go through the sorted data, see if you can find the matching information from the unsorted data.

print_sorted_by_frequency: This should entirely be an exercise in making the output look good.

Extra Credit Option

One ancient but still common method of encoding information is a substitution cipher, in which all occurrences of each letter are replaced with some other symbol (often another letter). One of the most common present-day ciphers is the Rotate-13, which simply replaces each letter in the English alphabet with the letter that appears 13 steps away.

Of course, it is easy to decode these ciphers if one already knows the encryption method (as is true with the Rotate-13). However, another mechanism that often works is to analyze the frequency of each character in the coded message. One can then assume that it has a similar frequency distribution to any other message in the same language.

One notable example of how to decode in this manner appears in the short story "The Gold Bug" by Edgar Allan Poe. Of course, there are other readily accessible illustrations all over the place, especially for solving puzzles called Cryptograms.

For purposes of this assignment, here is a sample English sentence:

'This is a sample of ordinary text with common letter frequencies' 

And here is an encoded message that has similar letter frequencies in its plain-text version:

'Qswg ylqg lx ykg xgdwgy sh hwlgrfxklt ys qbro brlqbpx' 

Use your functions above, plus any other additional Python code to help decode this sentence.

Some helpful Python functions:

''.join( list of characters )

creates a single string of those characters

message.translate( str.maketrans( given characters, new characters ) )

takes the 'message' and translates it as described

Not all sentences have identical frequencies, so it would only be profitable to translate the most frequent characters instead of all of them, and replace the less frequent with a placeholder, (like a . or ?). Then you could puzzle out the rest on your own.

If you figure out what the new sentence is, include something about it at the bottom of your program file, along with the code you used to discover it.

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!