1def get_complement(letter, change):
2 '''returns the other string in the set; if not in set returns None'''
3 for first, second in change:
4 if first == letter:
5 return second
6 if second == letter:
7 return first
8
9
10def plugboard(text, change):
11 result = ''
12 for letter in text:
13 complement = get_complement(letter, change)
14 result += complement if complement else letter
15
16 return result
17
18
19def rotor(text, dictionary):
20 result = ''
21 for letter in text:
22 result += ' ' if letter == ' ' else dictionary[letter]
23
24 return result
25
26
27def enigma_encrypt(plugboard_position, rotor_position):
28 def decorator(func):
29 def wrapper(text):
30 return func(rotor(plugboard(text, plugboard_position), rotor_position))
31 return wrapper
32 return decorator
33
34
35def enigma_decrypt(plugboard_position, rotor_position):
36 reversed_rotor_position = {val:key for key, val in rotor_position.items()}
37
38 def decorator(func):
39 def wrapper(text):
40 return func(plugboard(rotor(text, reversed_rotor_position), plugboard_position))
41 return wrapper
42 return decorator
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
29.10.2023 08:33