Python Caesar Cipher Decoder

后端 未结 3 600
抹茶落季
抹茶落季 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:14

    I'm working on the same tutorial and used a slightly different method. This avoided creating and calling functions:

    inp = input()     #to hold code text
    code = list(inp)  #store code as a list
    soln = []         #store the 'Goodness' for each of 26 possible answers
    y=0               #variable to hold total goodness during calculations
    clear = []        #will hold decoded text
    pos=0             #position marker for a list
    
    #for every possible value of shift
    #note range as 0 to 25 are valid shifts and shift 26 = shift 0
    
    for shift in range(0,26):
       for i in code:                  #loop through each letter in code
          if i == " ":                 #spaces have no score so omit them
             continue
          else:                        #if it's a letter
             x = ord(i)-shift          #apply the test shift
             if x < 65:                #prevent shifting outside A-Z range
                x = x + 26             
             x = x - 64                #turn ord into character position in A-Z with A=1
             x = letterGoodness[x-1]   #turn this into the Goodness score
             y = y + x                 #add this to a running total
       soln.insert(shift-1,y)          #AFTER decoding all letters in code, add total(y) to list of scores
       y = 0                           #reset y before next test value
    
    bestSoln=max(soln)                 #find highest possible score
    
    for i in range(0,26):              #check the list of solutions for this score
       if soln[i]==bestSoln:           #the position in this list is the shift we need
           bestShift = i+1             #+1 as the first solution is 0
    
    for i in code:                     #now decode the original text using our best solution
       if i == " ":                    #spaces are not encoded so just add these to the string
          clear.insert(pos," ")        #pos used to track next position for final string
          pos = pos + 1
          continue
       else:
          x = ord(i)-bestShift         #same operation as before
          if x < 65:
             x = x + 26
       z = chr(x)
       clear.insert(pos,z)             #add the decoded letter to the clear text
       pos = pos + 1
    print("".join(clear))              #join the list of clear text into one string and print it
    

    Note that many parts of this code could (and should) be compressed, for example

    x = x - 64
    x = letterGoodness[x-1]
    y = y + x
    

    They are left expanded to 'show my working' for a tutorial exercise.

提交回复
热议问题