解码 Ceasar 密码以逻辑错误告终
Decoding the Ceasar Cipher Ends Up with Logic Error
在编码方面我是一个完全的初学者,当我遇到这个问题时我正在接受 CodeWars 培训:
The soldiers of a country use ceasar cipher for messaging. Only letters are changed and everything else is not touched in these messages (like "!" and space). Each message is split into 4 or 5 parts and each part is carried by one runner. The first part starts with 2 letters: The first one shows a random letter and the second one is the same letter that has been changed by shifting (so that the soldiers know the number of the shift). Now write a code for decoding and encoding the message I should have known that you would have a perfect answer for me!!!
编码功能有效。但是,我为解码编写的以下函数却没有。我输入了 "ijJ tipvme ibw", "f lopxo uibu z", "pv xpvme ibwf ", "b qfsgfdu botx", "fs gps nf!!!"
消息,我应该得到 I should have known that you would have a perfect answer for me!!!
,但我却收到了一些无意义的消息 'ž~ ¨¤ª¡™ –«', ' "š £¤¬£ ©–© ®', ' "¤ª ¬¤ª¡™ –«š ', ' "– ¥š§›š˜© –£¨¬', ' "š§ ›¤§ ¢š!!!
我先导入re
:
import re
函数如下:
def decode(arr):
arr = str(arr) #turn what we have into str
array = list(re.split('",', arr)) #get rid of the ' ", ' between every part so that
# it will be a single whole messege in the end and also turn it into
#a list in which every item is a single part
decoded_messege = ""
first_messege = list(array[0]) #turns the first part into a list of letters
letter_shift = ord(first_messege[1]) - ord(first_messege[0]) #realizes the number of shift
for messege in array:
for letter in messege:
if (ord(letter) > 122 or ord(letter) < 65): #the letter is not an alphabet (a space or !)
decoded_messege += letter
else: #the letter is an alphabet
decoded_messege += chr(ord(letter) - letter_shift) #the ascii code is the current ascii number
#minus the shift
decoded_message = decoded_messege[2:] #ignore the first two letters that are
#only for showing shift and don't matter in the message
return decoded_messege
问题是您在计算移位时考虑了前两个字符:"
-->"
,即 0。将其更改为 arr = str(arr)[2:]
此外,return 应该与第一个循环处于同一级别。
import re
def decode(arr):
arr = str(arr)[2:] #turn what we have into str
array = list(re.split('",', arr)) #get rid of the ' ", ' between every part so that
# it will be a single whole messege in the end and also turn it into
#a list in which every item is a single part
decoded_messege = ""
first_messege = list(array[0]) #turns the first part into a list of letters
letter_shift = ord(first_messege[1]) - ord(first_messege[0]) #realizes the number of shift
for messege in array:
for letter in messege:
if letter != '"':
# print(letter)
if (ord(letter) > 122 or ord(letter) < 65): #the letter is not an alphabet (a space or !)
decoded_messege += letter
else: #the letter is an alphabet
decoded_messege += chr(ord(letter) - letter_shift) #the ascii code is the current ascii number #minus the shift
decoded_message = decoded_messege[2:] #ignore the first two letters that are
#only for showing shift and don't matter in the message
return decoded_messege
print(decode('""ijJ tipvme ibw", "f lopxo uibu z", "pv xpvme ibwf ", "b qfsgfdu botx", "fs gps nf!!!""'))
# hiI should hav e known that y ou would have a perfect answ er for me!!!
```
在编码方面我是一个完全的初学者,当我遇到这个问题时我正在接受 CodeWars 培训:
The soldiers of a country use ceasar cipher for messaging. Only letters are changed and everything else is not touched in these messages (like "!" and space). Each message is split into 4 or 5 parts and each part is carried by one runner. The first part starts with 2 letters: The first one shows a random letter and the second one is the same letter that has been changed by shifting (so that the soldiers know the number of the shift). Now write a code for decoding and encoding the message
I should have known that you would have a perfect answer for me!!!
编码功能有效。但是,我为解码编写的以下函数却没有。我输入了 "ijJ tipvme ibw", "f lopxo uibu z", "pv xpvme ibwf ", "b qfsgfdu botx", "fs gps nf!!!"
消息,我应该得到 I should have known that you would have a perfect answer for me!!!
,但我却收到了一些无意义的消息 'ž~ ¨¤ª¡™ –«', ' "š £¤¬£ ©–© ®', ' "¤ª ¬¤ª¡™ –«š ', ' "– ¥š§›š˜© –£¨¬', ' "š§ ›¤§ ¢š!!!
我先导入re
:
import re
函数如下:
def decode(arr):
arr = str(arr) #turn what we have into str
array = list(re.split('",', arr)) #get rid of the ' ", ' between every part so that
# it will be a single whole messege in the end and also turn it into
#a list in which every item is a single part
decoded_messege = ""
first_messege = list(array[0]) #turns the first part into a list of letters
letter_shift = ord(first_messege[1]) - ord(first_messege[0]) #realizes the number of shift
for messege in array:
for letter in messege:
if (ord(letter) > 122 or ord(letter) < 65): #the letter is not an alphabet (a space or !)
decoded_messege += letter
else: #the letter is an alphabet
decoded_messege += chr(ord(letter) - letter_shift) #the ascii code is the current ascii number
#minus the shift
decoded_message = decoded_messege[2:] #ignore the first two letters that are
#only for showing shift and don't matter in the message
return decoded_messege
问题是您在计算移位时考虑了前两个字符:"
-->"
,即 0。将其更改为 arr = str(arr)[2:]
此外,return 应该与第一个循环处于同一级别。
import re
def decode(arr):
arr = str(arr)[2:] #turn what we have into str
array = list(re.split('",', arr)) #get rid of the ' ", ' between every part so that
# it will be a single whole messege in the end and also turn it into
#a list in which every item is a single part
decoded_messege = ""
first_messege = list(array[0]) #turns the first part into a list of letters
letter_shift = ord(first_messege[1]) - ord(first_messege[0]) #realizes the number of shift
for messege in array:
for letter in messege:
if letter != '"':
# print(letter)
if (ord(letter) > 122 or ord(letter) < 65): #the letter is not an alphabet (a space or !)
decoded_messege += letter
else: #the letter is an alphabet
decoded_messege += chr(ord(letter) - letter_shift) #the ascii code is the current ascii number #minus the shift
decoded_message = decoded_messege[2:] #ignore the first two letters that are
#only for showing shift and don't matter in the message
return decoded_messege
print(decode('""ijJ tipvme ibw", "f lopxo uibu z", "pv xpvme ibwf ", "b qfsgfdu botx", "fs gps nf!!!""'))
# hiI should hav e known that y ou would have a perfect answ er for me!!!
```