Python凯撒密码解码器 [英] Python Caesar Cipher Decoder
问题描述
编写一个执行以下操作的程序。首先,它应该读取一行输入,它是编码的消息,并且将由大写字母和空格组成。您的程序必须尝试使用所有26个可能的值S进行解码消息;在这26个可能的原始信息中,打印出具有最高优点的信息。
为方便起见,我们将为您预定义变量letterGoodness,长度为26的列表等于上述频率表中的值
我有这个代码到目前为止:
x = input()
NUM_LETTERS = 26#无法导入模块我正在使用基于Web的平地机/编译器
def SpyCoder(S,N):
y =
为我在S:
x = ord(i)
x + = N
如果x> ord('Z'):
x - = NUM_LETTERS
elif x< ord('A'):
x + = NUM_LETTERS
y + = chr(x)
return y
def GoodnessFinder(S):
y = 0
对于我在S:
如果x!= 32:
x = ord(i)
x - = ord('A')
y + = letterGoodness [x]
return y
def GoodnessComparer(S):
goodnesstocompare = GoodnessFinder(S)
goodness = 0
v =''
for i在范围(0,26)中:
v = SpyCoder(S,i)
goodness = GoodnessFinder(v)
如果善良> goodnesstocompare:
goodnesstocompare = goodness
return v
y = x.split()
z =''
for i in range(0,len(y ))
如果i == len(y) - 1:
z + = GoodnessComparer(y [i])
print(z)
编辑:由Cristian Ciupitu建议的更改
请忽略缩进错误,当我复制代码时可能会出现。
该程序的工作原理如下:
- 将输入并分成列表
- 对于每个列表值,我将其提供给一个优点查找器。
- 它需要字符串的好处,并比较其他对象和更高的优点使得较高的人比较好。
- 然后将该字符串的字符数量移动到我的位置,以查看好处是否更高或更低
我不太确定问题在哪里,第一个测试:LQKP OG CV GKIJV DA VJG BQQ
Pr ints正确的消息:加入我的ZOO
然而下一个测试:UIJT JT B TBNQMF MJOF PG UFYU GPS EFDSZQUJOH
给出一个垃圾串:SGHR HR Z RZLOKD KHMD NE SDWS ENQ CDBQXOSHMF
当它应该是:这是一个样本文本行用于分解
我知道我必须:
尝试每个班次的值
获取单词的好
返回具有最高优点的字符串。 p>
我希望我的解释是有道理的,因为我现在很困惑。
x = input()
NUM_LETTERS = 26#无法导入模块我正在使用基于Web的grader /编译器
def SpyCoder(S,N):
y =
for S in S:
if(i.isupper()):
x = ord(i)
x + = N
如果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
S中的i
如果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 =我在范围(0,26)中的S
:
v = SpyCoder S,i)
goodness = GoodnessFinder(v)
如果善良> goodnesstocompare:
best_v = v
goodnesstocompare = goodness
return best_v
print(GoodnessComparer(x))
感谢您的帮助!
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 much sense that made but let post the question:
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
I have this code so far:
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:
x = ord(i)
x += N
if x > ord('Z'):
x -= NUM_LETTERS
elif x < ord('A'):
x += NUM_LETTERS
y += chr(x)
return y
def GoodnessFinder(S):
y = 0
for i in S:
if x != 32:
x = ord(i)
x -= ord('A')
y += letterGoodness[x]
return y
def GoodnessComparer(S):
goodnesstocompare = GoodnessFinder(S)
goodness = 0
v = ''
for i in range(0, 26):
v = SpyCoder(S, i)
goodness = GoodnessFinder(v)
if goodness > goodnesstocompare:
goodnesstocompare = goodness
return v
y = x.split()
z = ''
for i in range(0, len(y)):
if i == len(y) - 1:
z += GoodnessComparer(y[i])
print(z)
EDIT: Made changes suggested by Cristian Ciupitu Please ignore indentation errors, they probably arose when I copied my code over.
The program works like this:
- Take the input and split it into a list
- For every list value I feed it to a goodness finder.
- It takes the goodness of the string and compares everything else against and when there's a higher goodness it makes the higher one the goodness to compare.
- It then shifts that string of text by i amount to see if the goodness is higher or lower
I'm not quite sure where the problem is, the first test: LQKP OG CV GKIJV DA VJG BQQ
Prints the correct message: JOIN ME AT AT BY THE ZOO
However the next test: UIJT JT B TBNQMF MJOF PG UFYU GPS EFDSZQUJOH
Gives the a junk string of: SGHR HR Z RZLOKD KHMD NE SDWS ENQ CDBQXOSHMF
When it's supposed to be: THIS IS A SAMPLE LINE OF TEXT FOR DECRYPTING
I know I have to:
Try every shift value
Get the 'goodness' of the word
Return the string with the highest goodness.
I hope my explanation made sense as I am quite confused at the moment.
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!
这篇关于Python凯撒密码解码器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!