0

シーザー暗号をデコードするプログラムと、デコードする複数の行を含むいくつかのテキスト ファイルがあります。

講師のコード チェッカーによると、テキストの後には常に空白行がありますが、自分でコードを実行しても何も表示されません。

最後の文字を削除すると、テキストの最後の文字または数字のみが削除され、改行は削除されません。

これが私のコードです:

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()
4

1 に答える 1

1

printデフォルトでは、出力の後に改行を追加します。Python 2 では、print decrypted,(末尾のコンマに注意してください) を使用して、末尾の改行を抑制します。Python 3 では、 を使用しますprint(decrypted, end='')。または、代わりに、sys.stdout.write(decrypted)フォーマットなしで出力を書き込むために使用することもできます。

于 2016-05-03T15:03:31.313 に答える