Python Caesar Cipher Decoder

后端 未结 3 605
抹茶落季
抹茶落季 2021-01-03 06:00

In my lesson I was tasked with creating a Caesar Cipher decoder that takes a string of input and finds the best possible string using a letter frequencies. If not sure how m

3条回答
  •  一向
    一向 (楼主)
    2021-01-03 06:15

    My final solution that works, thanks to the wonderful Cristian Ciupitu.

    x = input()
    NUM_LETTERS = 26 #Can't import modules I'm using a web based grader/compiler
    def SpyCoder(S, N):
       y = ""
       for i in S:
          if(i.isupper()):
             x = ord(i)
             x += N
             if x > ord('Z'):
                x -= NUM_LETTERS
             elif x < ord('A'):
                x += NUM_LETTERS
             y += chr(x)
          else:
             y += " "
       return y
    
    def GoodnessFinder(S):
       y = 0
       for i in S:
          if i.isupper():
             x = ord(i)
             x -= ord('A')
             y += letterGoodness[x]
          else:
             y += 1
       return y
    
    def GoodnessComparer(S):
       goodnesstocompare = GoodnessFinder(S)
       goodness = 0
       v = ''
       best_v = S
       for i in range(0, 26):
         v = SpyCoder(S, i)
         goodness = GoodnessFinder(v)
         if goodness > goodnesstocompare:
             best_v = v
             goodnesstocompare = goodness
       return best_v
    
    
    print(GoodnessComparer(x))
    

    Thank you for all of your help!

提交回复
热议问题