我有一個程式,解碼一個凱撒密碼和幾個文本檔案與多行解碼。在
根據我的講師代碼檢查器,文本後面總是有一個空白行,但是當我自己運作代碼時,我看不到任何東西。在
删除最後一個字元隻會删除文本中的最後一個字母或數字,而不會删除換行符。在
我的代碼是:import sys
import string
import collections
ciphertext_lines = sys.stdin.readlines()
ciphertext = ''
for i in ciphertext_lines:
ciphertext += i
alphanum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
def getShiftVal():
string = ''
for line in ciphertext:
string = string + line
most_common_letter = ((collections.Counter(string).most_common(2)[1])[0])
shift_val = (alphanum.index(most_common_letter) - 4)
return shift_val
def decrypt(ciphertext, n):
alphabet_numbers = collections.deque(string.ascii_uppercase + string.digits)
alphanum = ''.join(list(alphabet_numbers))
alphabet_numbers.rotate(n)
alphanum_rotated = ''.join(list(alphabet_numbers))
return ciphertext.translate(str.maketrans(alphanum, alphanum_rotated))
def main():
n = getShiftVal()
decrypted = decrypt(ciphertext, n)
print(decrypted)
if __name__ == '__main__':
main()