查找字符串中的第二个最常见元素

时间:2017-02-20 00:12:54

标签: python caesar-cipher

我正在尝试创建一个Caesar Cipher程序。虽然我差不多完成了,但我遇到了两个主要问题,我对如何修复感到有些困惑。我的Caesar Cipher通过查找输入中最常见的字母来工作。因为英语中最常用的字母是' e'我的密码通过从' e'的ASCII值中减去最常见元素的ASCII值来移动每个字母的值。虽然这有效,但我需要我的程序能够找到第二个最常用的元素,如果初始尝试不可读。如果这不起作用,第三个,第四个,等等。我遇到的下一个问题是我的输出看起来并不干净"。它具有可读性,但穿插着奇怪的符号。

maximum_character = unciphered_text[0]
maximum_count = unciphered_text.count(unciphered_text[0])
for char in unciphered_text:
    if char is not " ":
        if unciphered_text.count(char) > maximum_count:
            maximum_character = char

print("The most frequent character used is: ", maximum_character) 

ASCII_maximum = maximum_character.lower()
ASCII_number = ord(ASCII_maximum)
print(ASCII_number)

shift_distance = ord('e')-ASCII_number
print("The shift distance is: ", shift_distance)

def caesar_cipher(unciphered_text, shift_distance):
    ciphered_text = ""
    for char in unciphered_text:
        cipher_process = ord(char)+shift_distance
        post_translation = chr(cipher_process)
        ciphered_text += post_translation 
    return ciphered_text 

answer = caesar_cipher(unciphered_text, shift_distance)
print(answer)

这就是我所说的怪异输出。

输入:

Frzdugv glh pdqb wlphv ehiruh wkhlu ghdwkv; Wkh ydoldqw qhyhu wdvwh ri ghdwk exw rqfh. Ri doo wkh zrqghuv wkdw L bhw kdyh khdug, Lw vhhpv wr ph prvw vwudqjh wkdw phq vkrxog ihdu; Vhhlqj wkdw ghdwk, d qhfhvvdub hqg, Zloo frph zkhq lw zloo frph

输出:

Cowards
die
man_
times
before
their
deaths8
The
valiant
never
taste
of
death
but
once+
Of
all
the
wonders
that
I
_et
have
heard)
It
seems
to
me
most
strange
that
men
should
fear8
Seeing
that
death)
a
necessar_
end)
Will
come
when
it
will
come

0 个答案:

没有答案