在大学里为学校的作业建造一个谜一样的机器是一项非强制性的练习。有几个引导步骤(首先我必须实现一个旋转轮,然后是其中的 3 个,之后我必须实现反射器,然后才能设置轮子的起始位置)。到此为止,一切都很好(它通过了所有测试),但是当我尝试实现插件板(Steckerbrett)时,我的构建未能进行测试,并且在搜索了几个小时的错误(我找不到)之后,我请你帮我。
这是我的代码(请注意,我仍然是个菜鸟,而且我的代码可能不是最整洁的):
#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”作为一个长字符串应该给出一个英文句子(当然只有大写字母和没有空格)
感谢您提前回答!