1def plugboard(text_to_convert, converters):
2 converted_text = []
3
4 for char in text_to_convert:
5 replacement = char
6 for conversion_pair in converters:
7 if char in conversion_pair:
8 replacement = (conversion_pair - {char}).pop()
9 break
10 converted_text.append(replacement)
11
12 return ''.join(converted_text)
13
14
15def rotor(text_to_convert, mapping):
16 converted_text = ""
17
18 for symbool in text_to_convert:
19 if symbool in mapping:
20 converted_text += mapping[symbool]
21 else:
22 converted_text += symbool
23
24 return converted_text
25
26
27def enigma_encrypt(plugboard_position, rotor_position):
28 def decorator(func):
29 def encrypt(text):
30 text = plugboard(text, plugboard_position)
31 text = rotor(text, rotor_position)
32
33 return func(text)
34 return encrypt
35 return decorator
36
37
38def enigma_decrypt(plugboard_position, rotor_position):
39 def decorator(func):
40 def decrypt(text):
41 reversed_rotor = dict((v, k) for k, v in rotor_position.items())
42 rotor_result = rotor(text, reversed_rotor)
43 decrypted_text = plugboard(rotor_result, plugboard_position)
44
45 return func(decrypted_text)
46 return decrypt
47 return decorator
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
30.10.2023 14:02