Question

Write the function decipher(s, frequencies), which will decipher a Caesar cipher encrypted string s using the...

Write the function decipher(s, frequencies), which will decipher a Caesar cipher encrypted string s using the letter frequencies in frequencies, and return the plaintext string. Here is a sample of how this function would be used:

if __name__ == '__main__':

    # this file path works on my computer.
    # you will need to set this path to work on your computer!
    f = open('/Users/azs/code/bu/cs108/assignments/romeo.txt')
    text = f.read()
    f.close()
    d = letter_frequencies(text)
    s1 = 'h svun aptl h nv pu h nhshef mhy mhy hdhf'
    print(decipher(s1, d))

will produce the following output:

a long time a go in a galaxy far far away

Algorithm and Hints:

  • Create a list called options containing of all 26 possible decipherings of the string s.

    Hint: use a list comprehension. Print out to verify that you have created all possible cipherings.

  • Create a list of scored_options, which is a list of lists, for each possible deciphering in options. Each sublist will contain the Englishness score for that deciphering as well as the deciphered string.

    Hint: use a list comprehension. Print out to verify that you have created all possible scored decipherings.

  • Call built-in function max on your list of scored_options to obtain the sublist with the highest Englishness score.

    Hint: the result of max will be a sublist of [score, string]. You can use indexing to obtain the string, and return it from the function.

Homework Answers

Answer #1


def decipher(s,frequencies):
   result = ""

   # traverse text
   for i in range(len(s)):
       char = s[i]

       # Encrypt uppercase characters
       if (char.isupper()):
           result += chr((ord(char) + frequencies-65) % 26 + 65)

       # Encrypt lowercase characters
       else:
           result += chr((ord(char) + frequencies - 97) % 26 + 97)

   return result

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions