I'm doing this exercise from computer science circle. Here's the information:
Write a program which does the following. First, it should read one line of input, which is the encoded message, and will consist of capital letters and spaces. Your program must try decoding the message with all 26 possible values of the shift S; out of these 26 possible original messages, print the one which has the highest goodness. For your convenience, we will pre-define the variable letterGoodness for you, a list of length 26 which equals the values in the frequency table above, letterGoodness = [.0817,.0149,.0278,.0425,.1270,.0223,.0202,...
I've done some coding but it kept saying 'list index out of range' at line 24 (the letterGoodness[diff] line). I checked but couldn't find out why.
Here's the code:
#subtask 1: shifting values
a = input ()
def Decode(S):
b = ""
for i in range (0, len (a)):
if a[i] != " ":
if ord(a[i]) + int(S) <= 90:
c = chr(ord(a[i]) + int(S))
b = b + c
else:
c = chr(ord(a[i]) + int(S) - 65)
b = b + c
else:
b = b + a[i]
return b
#subtask 2: determining goodness
goodness = 0
maxx = 0
for S in range (1, 27):
b = Decode (S)
for ii in range (0, len(b)):
if b[ii] != " ":
diff = (ord(b[ii]) - 65)
goodness = goodness + letterGoodness[diff]
if goodness > maxx:
maxx = goodness
out = b
print (out)