1def plugboard(text, pairs):
2 end_text = ""
3 for letter in text:
4 for twain in pairs:
5 if letter in twain:
6 end_text += twain.difference({letter}).pop()
7 break
8 else:
9 end_text += letter
10 return end_text
11
12def rotor(text, dictionary):
13 return "".join(map(lambda letter: dictionary.get(letter, letter), text))
14
15def enigma_encrypt(plugboard_position, rotor_position):
16 def decorator(func):
17 def encrypt(text):
18 text = plugboard(text, plugboard_position)
19 text = rotor(text, rotor_position)
20 return func(text)
21 return encrypt
22 return decorator
23
24def enigma_decrypt(plugboard_position, rotor_position):
25 def decorator(func):
26 def decrypt(text):
27 text = rotor(text, {letter : change_to for change_to, letter in rotor_position.items()})
28 text = plugboard(text, plugboard_position)
29 return func(text)
30 return decrypt
31 return decorator
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
Виктор Бечев
31.10.2023 14:14Чисто решение, нямам реални коментари.
|
31.10.2023 14:09