Question: Code this using Python3 Given random jumbling of the phrase: the answer to life the universe and everything is forty two (all lower case, no

Code this using Python3

Given random jumbling of the phrase: the answer to life the universe and everything is forty two" (all lower case, no punctuation), you are to develop a Markov Chain Monte Carlo technique to decrypt the message. The basic plan will be to read in a very long text (War and Peace), and use the ordering of the letters in that text to find out how often particular letters in the English language follow one another. Then propose a solution, test the likelihood with the above in mind, and use that information to decide whether or not to accept.

Create a probability matrix thats 2626 that represents lowercase a-z on both axes. Fill this with the likelihood that one letter is followed by another sequentially. Generate a random solution key where every letter maps to another randomly generated letter. You might find import random x = list_of_letters random.shuffle(x) or some similar code to be useful.

Calculate the value P from this sequence of letters from the probability matrix. There are a number of conceivable ways to do this. You could sum the values; take their product; work in log space, etc. You can experiment to figure out what works best. Choose two indices in your solution key to swap at random, and swap them. Evaluate if the probability from this proposed solution is better or worse than the previous probability. If yes, accept. If no, accept under some condition. Continue on like this for some pre-determined amount of iteration. At the end, calculate the percentage of correct letters in your solution phrase (at the right position) compared to the true phrase. How does this compare to if you were to randomly draw a letter for each position? Calculate the rate at which you approach your converged solution as a function of iteration number for each of the simulation variations you attempt (below).

Your jumbled phrase is: dhauxctaldikpwadhafxpgalcauxvagaledhpxrpcwildedti Note that this does not include spaces. You are welcome to create your own jumbled phrase if youd like (and you should do this if you want to include spaces in your cryptograph). The way I did it was something like:

def generate_key( list_of_letters): # dictionary mapping random letter to true letter x = list_of_letters random.shuffle(x) # now the orders are different key = dict(zip(x,string.ascii_lowercase)) reverse_key = dict(zip(string.ascii_lowercase, x)) return key, reverse_key def generate_random_from_phrase(phrase, reverse_key): jumbled_phrase = " " for i in range(len(phrase )): jumbled_phrase += reverse_key [phrase[i]] return jumbled_phrase

Use this link for the war and peace text: http://www.gutenberg.org/files/2600/2600-h/2600-h.htm

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!