为凯撒密码编一本字典



假设输入了一条消息,并且您希望通过移动单词"中的每个字母来对其进行加密;向上字母表";具有x个空格。例如";猫;移动了一个空格就变成了";dbu":

我正试图建立一个以大写和小写字母表为键,以重复字母表(按"移位"的字母数移动(为值的词典。例如shift=1 d={a:b,b:c…a:b,b:c}。这个字母表每次";移位";更改。请告诉我哪里出了问题。请参阅下面的:def-build_shift_dict(self,shift(。提前感谢!:

class Message(object):
### DO NOT MODIFY THIS METHOD ###
def __init__(self, text):
'''
Initializes a Message object

text (string): the message's text
a Message object has two attributes:
self.message_text (string, determined by input text)
self.valid_words (list, determined using helper function load_words
'''
self.message_text = text
self.valid_words = load_words(WORDLIST_FILENAME)
### DO NOT MODIFY THIS METHOD ###
def get_message_text(self):
'''
Used to safely access self.message_text outside of the class

Returns: self.message_text
'''
return self.message_text
### DO NOT MODIFY THIS METHOD ###
def get_valid_words(self):
'''
Used to safely access a copy of self.valid_words outside of the class

Returns: a COPY of self.valid_words
'''
return self.valid_words[:]

import string    
def build_shift_dict(self, shift):
'''
Creates a dictionary that can be used to apply a cipher to a letter.
The dictionary maps every uppercase and lowercase letter to a
character shifted down the alphabet by the input shift. The dictionary
should have 52 keys of all the uppercase letters and all the lowercase
letters only.        

shift (integer): the amount by which to shift every letter of the 
alphabet. 0 <= shift < 26
Returns: a dictionary mapping a letter (string) to 
another letter (string). 
'''
stringLower = string.ascii_lowercase 
stringUpper = string.ascii_uppercase
Alphabet = list(stringLower + stringUpper)
dictionary = {}
string2Lower = list(string.ascii_lowercase[shift:] + string.ascii_lowercase[0:shift])
string2Upper = list(string.ascii_uppercase[shift:] + string.ascii_uppercase[0:shift])
combinedList = string2Lower.append(string2Upper)

for key in Alphabet: 
for value in combinedList: 
dictionary[key] = value 
combinedList.remove(value) 

return dictionary



def apply_shift(self, shift):
'''
Applies the Caesar Cipher to self.message_text with the input shift.
Creates a new string that is self.message_text shifted down the
alphabet by some number of characters determined by the input shift        

shift (integer): the shift with which to encrypt the message.
0 <= shift < 26
Returns: the message text (string) in which every character is shifted
down the alphabet by the input shift
'''
result=''
dictionary = self.build_shift_dict(shift)
messageString = str(Message)
for letter in messageString:
if letter in dictionary.keys:
result += dictionary[i]
return result

您可以去掉很多基于类的东西,因为除了添加样板代码和使事情复杂化之外,它没有做什么。之后你可以把它放回原处,但在这里我已经用一种更像蟒蛇的方式为你写出了核心功能:

import string
def build_shift_dict(shift):
alphabet = string.ascii_lowercase + string.ascii_uppercase
shifted_lower = string.ascii_lowercase[shift:] + string.ascii_lowercase[0:shift]
shifted_upper = string.ascii_uppercase[shift:] + string.ascii_uppercase[0:shift]
shifted_alphabet = shifted_lower + shifted_upper
return {key: value for key, value in zip(alphabet, shifted_alphabet)}

一旦你有了shifted_alphabet,你就可以用原始字母表zip,然后使用字典理解来创建字典。这是一种更像蟒蛇的方式来制作dict,而不是循环


然后,为了应用移位,我们可以使用列表理解来查找消息中每个字母的字典,并创建一个列表,然后将其join重新组合为字符串并返回。

def apply_shift(shift, message):
dictionary = build_shift_dict(shift)
return ''.join(dictionary[letter] for letter in message)

然后使用:

>>> print(apply_shift(2, "Hello"))
Jgnnq

此处提供->https://repl.it/@LukeStorry/63042493


更新:您可以通过使用ordchr来避免创建和使用dict:

def apply_shift_v2(shift, message):
return ''.join(chr(ord(letter) + shift) for letter in message)

这还有一个额外的好处,可以为任何字符工作,而不仅仅局限于设置dict时使用的字母数字字符。

我的建议是,与其创建字典,不如创建简单得多的

def apply_shift(shift, letter):
asci = ord(letter)
new_ascii = 97 + (asci + shift -97) % 26
return chr(new_ascii)
print(apply_shift(1, 'z') )

输出:

a

最新更新