1def incode_text_with_dict(text, incode_dict):
2 return "".join([incode_dict[i] if i in incode_dict else i for i in text])
3
4
5def plugboard(text, plugboard_position):
6 # this function can be done a little bit faster, but uglier in my opinion
7 incode_dict = {}
8
9 for first_letter, seccond_letter in plugboard_position:
10 incode_dict[first_letter] = seccond_letter
11 incode_dict[seccond_letter] = first_letter
12
13 return incode_text_with_dict(text, incode_dict)
14
15
16def rotor(text, rotor_position):
17 return incode_text_with_dict(text, rotor_position)
18
19
20def enigma_encrypt(plugboard_position, rotor_position):
21 def decorator_func(func):
22 def encripted_func(text):
23 return func(rotor(plugboard(text, plugboard_position), rotor_position))
24
25 return encripted_func
26
27 return decorator_func
28
29
30def reverse_rotor_positions(rotor_position):
31 return {letter2: letter1 for letter1, letter2 in rotor_position.items()}
32
33
34def enigma_decrypt(plugboard_position, rotor_position):
35 def decorator_func(func):
36 def decripted_func(text):
37 return func(
38 plugboard(
39 rotor(text, reverse_rotor_positions(rotor_position)),
40 plugboard_position,
41 )
42 )
43
44 return decripted_func
45
46 return decorator_func
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
29.10.2023 09:12