1def plugboard(text, plugboard_position):
2 translation = dict()
3 for connection in plugboard_position:
4 a, b = connection
5 translation[a] = b
6 translation[b] = a
7
8 return ''.join(translation[char] if char in translation else char for char in text)
9
10def rotor(text, rotor_position):
11 return ''.join(rotor_position[char] if char in rotor_position else char for char in text)
12
13def enigma_encrypt(plugboard_position, rotor_position):
14 def decorator(f):
15 def wrapper(text):
16 encrypted_text = plugboard(text, plugboard_position)
17 encrypted_text = rotor(encrypted_text, rotor_position)
18 return f(encrypted_text)
19 return wrapper
20 return decorator
21
22def enigma_decrypt(plugboard_position, rotor_position):
23 inverted_rotor = {v: k for k, v in rotor_position.items()}
24
25 def decorator(f):
26 def wrapper(text):
27 decrypted_text = rotor(text, inverted_rotor)
28 decrypted_text = plugboard(decrypted_text, plugboard_position)
29 return f(decrypted_text)
30 return wrapper
31 return decorator
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK