The*_*man 5 python encryption decoding
在我的课程中,我的任务是创建一个Caesar Cipher解码器,它接受一串输入并使用字母频率找到最好的字符串.如果不确定有多大意义,但发布问题:
编写一个执行以下操作的程序.首先,它应该读取一行输入,即编码消息,并且将由大写字母和空格组成.您的程序必须尝试使用shift S的所有26个可能值来解码消息; 在这26条可能的原始信息中,打印出具有最高优点的信息.为方便起见,我们将为您预先定义变量letterGoodness,长度为26的列表,等于上面频率表中的值

到目前为止我有这个代码:
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)
Run Code Online (Sandbox Code Playgroud)
编辑:Cristian Ciupitu建议做出更改请忽略缩进错误,当我复制代码时,它们可能会出现.
该程序的工作方式如下:
我不太清楚问题出在哪里,第一次测试:LQKP OG CV GKIJV DA VJG BQQ
打印正确的信息:加入我的动物园
然而下一个测试:UIJT JT B TBNQMF MJOF PG UFYU GPS EFDSZQUJOH
给出一个垃圾串:SGHR HR Z RZLOKD KHMD NE SDWS ENQ CDBQXOSHMF
当它应该是:这是一个用于解密的文本样本行
我知道我必须:
尝试每个班次值
获得单词的'goodness'
返回具有最高优点的字符串.
我希望我的解释有意义,因为我现在很困惑.
我的最终解决方案有效,感谢出色的 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))
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助!
| 归档时间: |
|
| 查看次数: |
17294 次 |
| 最近记录: |