- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
问题是:
Your program needs to decode an encrypted text file called "encrypted.txt". The person who wrote it used a cipher specified in "key.txt". This key file would look similar to the following:
A B
B C
C D
D E
E F
F G
G H
H I
I J
J K
K L
L M
M N
N O
O P
P Q
Q R
R S
S T
T U
U V
V W
W X
X Y
Y Z
Z AThe left column represents the plaintext letter, and the right column represents the corresponding ciphertext.
Your program should decode the "encrypted.txt" file using "key.txt" and write the plaintext to "decrypted.txt".
我有:
decrypt = ""
keyfile = open("key.txt", "r")
cipher = {}
for keyline in keyfile:
cipher[keyline.split()[0]] = keyline.split()[1]
keyfile.close()
encrypt = []
encryptedfile = open("encrypted.txt", "r")
readlines = encryptedfile.readlines()
str = ""
for line in readlines:
str = line
letter = list(str)
for i in range(len(letter)):
if letter[i]==",":
decrypt += ","
elif letter[i] == ".":
decrypt+= "."
elif letter[i] == "!":
decrypt += "!"
elif letter[i] == " ":
decrypt += " "
else:
found = False
count = 0
while (found == False):
if (letter[i] == keyline.split()[0][count]):
decrypt += keyline.split()[1][count]
found = True
count += 1
print decrypt
encryptedfile.close()
decryptedfile = open("decrypted.txt", "w")
decryptedfile.write(decrypt)
decryptedfile.close()
这是 Python 语言。输出确实生成了一个名为 decrypted.txt 的文件,但文件中唯一的内容是 A,这对我来说没有意义。对于这个问题,它应该输出更多,对吗?
最佳答案
使用 key.txt
中给定的 key 解密文件 enrcypted.txt
并将结果保存到 decrypted.txt
:
with open('key.txt', 'r') as keyfile:
pairs = [line.split() for line in keyfile]
columns = [''.join(x) for x in zip(*pairs)]
# Or to make it work with lower case letters as well:
# columns = [''.join(x)+''.join(x).lower() for x in zip(*pairs)]
key = str.maketrans(
*reversed(columns)
)
with open('encrypted.txt', 'r') as encrypted_file:
decrypted = [line.translate(key) for line in encrypted_file]
with open('decrypted.txt', 'w') as decrypted_file:
decrypted_file.writelines(decrypted)
关于python - 凯撒密码 - 错误输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29613073/
我刚刚开始学习 C 语言类(class),并且遇到了命令行参数的问题。分配是这样的(还有更多,但这是开头有关命令行参数的部分): - 你的程序必须接受一个命令行参数,一个非负整数。 - 如果您的程序在
我需要检查命令行参数中是否有非数字字符。例如: ./problem 20x 应该打印出“不是数字”,因为它包含一个 x。我的代码似乎没有循环遍历命令行参数中的所有字符。 我基本上尝试了不同类型的循环,
这里我有从标准输入将字符流输入到数组中的代码。然后将该数组转换为二维数组。然后,它将该数组从行列顺序更改为列行顺序。然后它打印出创建凯撒移位加密的新数组。我遇到的问题是我的数组开始使用第二个用户输入的
我有点被这个问题困住了。当我运行程序时,由于某种原因,循环经过 z 的所有字母都不会打印。问题来自这个链接:http://docs.cs50.net/2016/x/ap/problems/caesar
我是一名优秀的程序员,十分优秀!