建立一个谜团是非强制性的练习。在大学的学校作业机器。有几个指导步骤(首先我必须实现一个旋转轮,然后其中3个,在此之后我必须实现反射器,然后能够设置轮子的起始位置)。在此之前一切都很顺利(它通过了每次测试),但是当我尝试实现插板(Steckerbrett)时,我的构建无法进行测试,并且在搜索了几个小时的错误后(我找不到)我要求你帮助我。
这是我的代码(请注意我仍然是noobie,我的代码可能不是最好的代码):
#Enigma build (everything included)
class Enigma:
def __init__(self, wheels, reflector, starting_position, plugs, message):
self.message = message
self.wheels = Wheels(wheels, starting_position)
self.reflector = reflector
self.plugboard = Plugboard(plugs)
self.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
self.wheels.set_starting_position()
def execute(self):
output = ""
self.message = self.plugboard.forward(self.message)
for char in self.message:
if char in self.alphabet:
coded_char = char
if coded_char in self.plugboard.plugs_forward:
coded_char = self.plugboard.plugs_forward[coded_char]
coded_char = coding_forward(coded_char, self.alphabet, self.wheels.wheel1)
coded_char = coding_forward(coded_char, self.alphabet, self.wheels.wheel2)
coded_char = coding_forward(coded_char, self.alphabet, self.wheels.wheel3)
coded_char = coding_forward(coded_char, self.alphabet, self.reflector)
coded_char = coding_backward(coded_char, self.alphabet, self.wheels.wheel3)
coded_char = coding_backward(coded_char, self.alphabet, self.wheels.wheel2)
coded_char = coding_backward(coded_char, self.alphabet, self.wheels.wheel1)
output = "".join([output, coded_char])
self.wheels.rotate()
output = self.plugboard.backward(output)
return output
class Wheels:
def __init__(self, wheels, position):
self.wheel1 = wheels[0]
self.wheel2 = wheels[1]
self.wheel3 = wheels[2]
self.position = position
self.rotation_counter = 0
def rotate(self):
self.rotation_counter += 1
self.wheel1 = "".join([self.wheel1[1:], self.wheel1[0]])
if self.rotation_counter % 26 == 0:
self.wheel2 = "".join([self.wheel2[1:], self.wheel2[0]])
if self.rotation_counter % 676 == 0:
self.wheel3 = "".join([self.wheel3[1:], self.wheel3[0]])
self.rotation_counter = 0
def set_starting_position(self):
turn1 = ord(self.position[0]) - ord('A')
self.wheel1 = "".join([self.wheel1[turn1:],self.wheel1[:turn1]])
turn2 = ord(self.position[1]) - ord('A')
self.wheel2 = "".join([self.wheel2[turn2:],self.wheel2[:turn2]])
turn3 = ord(self.position[2]) - ord('A')
self.wheel3 = "".join([self.wheel3[turn3:],self.wheel3[:turn3]])
class Plugboard:
def __init__(self, plugs):
self.plugs_forward = {a:b for a,b in plugs}
self.plugs_backward = {b:a for a,b in plugs}
def forward(self, string):
output = []
for char in string:
if char in self.plugs_forward:
output.append(self.plugs_forward[char])
else:
output.append(char)
return "".join(output)
def backward(self, string):
output = []
for char in string:
if char in self.plugs_backward:
output.append(self.plugs_backward[char])
else:
output.append(char)
return "".join(output)
def coding_forward(char, wheel_side_1, wheel_side_2):
coded_char = ""
char_ix = wheel_side_1.index(char)
coded_char = wheel_side_2[char_ix]
return coded_char
def coding_backward(char, wheel_side_1, wheel_side_2):
coded_char = ""
char_ix = wheel_side_2.index(char)
coded_char = wheel_side_1[char_ix]
return coded_char
user_input = input()
newEnigma = Enigma(["QOFJSPIRUTNLMXWBZEGYKCADVH","ZHQYASLFCTRPKUWDVBIMJXGOEN", "JQBVTIEUXANMYDKPSWLZHGFROC"],
"YRUHQSLDPXNGOKMIEBFZCWVJAT", "KEY", ["QA", "HJ", "KI", "EV", "GC", "PO", "LK", "NX", "AS", "TE"], user_input)
print(newEnigma.execute())
现在这个" EGJTUABUVJWZCYURYOIDFZZKHZCWQQGMKTTTNADFMBNFOQXTSDGANPROIWLLBZJVRXIRQAINSHCURXQLPK"因为一个长串应该给出一个英文句子(当然只有大写字母,没有空格)
提前感谢您的答案!
答案 0 :(得分:0)
你有几个插头插入E
& A
输入中的plugboard settings
;我认为这可能会造成你的问题。:
["QA", "HJ", "KI", "EV", "GC", "PO", "LK", "NX", "AS", "TE"]
^A ^E ^A ^E
您可能需要验证插板输入是否符合要求。
这是一种方法:
def verify_plugs(plug_connections):
seen = set([letter for pairs in plug_connections for letter in pairs])
return len(seen) == len(plug_connections) * 2
plug_connections = ["QA", "HJ", "KI", "EV", "GC", "PO", "LK", "NX", "AS", "TE"]
verify_plugs(plug_connections)
False