1EMPTY_STRING = ''
2
3
4def plugboard(base_text, pairs):
5 return EMPTY_STRING.join([new_mapping(symbol, pairs) for symbol in base_text])
6
7
8def rotor(base_text, alphabet_map):
9 return EMPTY_STRING.join([alphabet_map.get(symbol, symbol) for symbol in base_text])
10
11
12def enigma_encrypt(plugboard_position, rotor_position):
13 def decorator(function):
14 def engine_encrypt(text):
15 return function(rotor(plugboard(text, plugboard_position), rotor_position))
16 return engine_encrypt
17 return decorator
18
19
20def enigma_decrypt(plugboard_position, rotor_position):
21 def decorator(function):
22 def engine_decrypt(text):
23 return function(plugboard(reverse_rotor(text, rotor_position), plugboard_position))
24 return engine_decrypt
25 return decorator
26
27
28def new_mapping(symbol, pairs):
29 return next((counterpart(symbol, pair) for pair in pairs if symbol in pair), symbol)
30
31
32def counterpart(symbol, pair):
33 return (pair - {symbol}).pop()
34
35
36def reverse_rotor(text, pairs):
37 return rotor(text, reverse_dictionary(pairs))
38
39
40def reverse_dictionary(dictionary):
41 return {value: key for key, value in dictionary.items()}
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
Георги Кунчев
27.10.2023 10:09Имаш няколко помощни функции, които изглеждат разхвърляни в кода. Препоръчвам да ги събереш в началото или в края. Доколко всяка от тях е нужда, е въпрос на лично мнение, така че това няма да го коментирам.
|
f | 1 | EMPTY_STRING = '' | f | 1 | EMPTY_STRING = '' |
2 | 2 | ||||
3 | 3 | ||||
4 | def plugboard(base_text, pairs): | 4 | def plugboard(base_text, pairs): | ||
5 | return EMPTY_STRING.join([new_mapping(symbol, pairs) for symbol in base_text]) | 5 | return EMPTY_STRING.join([new_mapping(symbol, pairs) for symbol in base_text]) | ||
6 | 6 | ||||
7 | 7 | ||||
n | 8 | def new_mapping(symbol, pairs): | n | ||
9 | return next((counterpart(symbol, pair) for pair in pairs if symbol in pair), symbol) | ||||
10 | |||||
11 | |||||
12 | def counterpart(symbol, pair): | ||||
13 | return (pair - {symbol}).pop() | ||||
14 | |||||
15 | |||||
16 | def rotor(base_text, alphabet_map): | 8 | def rotor(base_text, alphabet_map): | ||
n | 17 | return EMPTY_STRING.join([alphabet_map[symbol] for symbol in base_text]) | n | 9 | return EMPTY_STRING.join([alphabet_map.get(symbol, symbol) for symbol in base_text]) |
18 | 10 | ||||
19 | 11 | ||||
20 | def enigma_encrypt(plugboard_position, rotor_position): | 12 | def enigma_encrypt(plugboard_position, rotor_position): | ||
21 | def decorator(function): | 13 | def decorator(function): | ||
22 | def engine_encrypt(text): | 14 | def engine_encrypt(text): | ||
23 | return function(rotor(plugboard(text, plugboard_position), rotor_position)) | 15 | return function(rotor(plugboard(text, plugboard_position), rotor_position)) | ||
24 | return engine_encrypt | 16 | return engine_encrypt | ||
25 | return decorator | 17 | return decorator | ||
26 | 18 | ||||
27 | 19 | ||||
28 | def enigma_decrypt(plugboard_position, rotor_position): | 20 | def enigma_decrypt(plugboard_position, rotor_position): | ||
29 | def decorator(function): | 21 | def decorator(function): | ||
30 | def engine_decrypt(text): | 22 | def engine_decrypt(text): | ||
31 | return function(plugboard(reverse_rotor(text, rotor_position), plugboard_position)) | 23 | return function(plugboard(reverse_rotor(text, rotor_position), plugboard_position)) | ||
32 | return engine_decrypt | 24 | return engine_decrypt | ||
33 | return decorator | 25 | return decorator | ||
34 | 26 | ||||
35 | 27 | ||||
t | t | 28 | def new_mapping(symbol, pairs): | ||
29 | return next((counterpart(symbol, pair) for pair in pairs if symbol in pair), symbol) | ||||
30 | |||||
31 | |||||
32 | def counterpart(symbol, pair): | ||||
33 | return (pair - {symbol}).pop() | ||||
34 | |||||
35 | |||||
36 | def reverse_rotor(text, pairs): | 36 | def reverse_rotor(text, pairs): | ||
37 | return rotor(text, reverse_dictionary(pairs)) | 37 | return rotor(text, reverse_dictionary(pairs)) | ||
38 | 38 | ||||
39 | 39 | ||||
40 | def reverse_dictionary(dictionary): | 40 | def reverse_dictionary(dictionary): | ||
41 | return {value: key for key, value in dictionary.items()} | 41 | return {value: key for key, value in dictionary.items()} |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
n | n | 1 | EMPTY_STRING = '' | ||
2 | |||||
3 | |||||
1 | def plugboard(base_text, pairs): | 4 | def plugboard(base_text, pairs): | ||
n | 2 | return ''.join([new_mapping(symbol, pairs) for symbol in base_text]) | n | 5 | return EMPTY_STRING.join([new_mapping(symbol, pairs) for symbol in base_text]) |
3 | 6 | ||||
4 | 7 | ||||
5 | def new_mapping(symbol, pairs): | 8 | def new_mapping(symbol, pairs): | ||
6 | return next((counterpart(symbol, pair) for pair in pairs if symbol in pair), symbol) | 9 | return next((counterpart(symbol, pair) for pair in pairs if symbol in pair), symbol) | ||
7 | 10 | ||||
8 | 11 | ||||
9 | def counterpart(symbol, pair): | 12 | def counterpart(symbol, pair): | ||
10 | return (pair - {symbol}).pop() | 13 | return (pair - {symbol}).pop() | ||
11 | 14 | ||||
12 | 15 | ||||
13 | def rotor(base_text, alphabet_map): | 16 | def rotor(base_text, alphabet_map): | ||
t | 14 | return ''.join([alphabet_map[symbol] for symbol in base_text]) | t | 17 | return EMPTY_STRING.join([alphabet_map[symbol] for symbol in base_text]) |
15 | 18 | ||||
16 | 19 | ||||
17 | def enigma_encrypt(plugboard_position, rotor_position): | 20 | def enigma_encrypt(plugboard_position, rotor_position): | ||
18 | def decorator(function): | 21 | def decorator(function): | ||
19 | def engine_encrypt(text): | 22 | def engine_encrypt(text): | ||
20 | return function(rotor(plugboard(text, plugboard_position), rotor_position)) | 23 | return function(rotor(plugboard(text, plugboard_position), rotor_position)) | ||
21 | return engine_encrypt | 24 | return engine_encrypt | ||
22 | return decorator | 25 | return decorator | ||
23 | 26 | ||||
24 | 27 | ||||
25 | def enigma_decrypt(plugboard_position, rotor_position): | 28 | def enigma_decrypt(plugboard_position, rotor_position): | ||
26 | def decorator(function): | 29 | def decorator(function): | ||
27 | def engine_decrypt(text): | 30 | def engine_decrypt(text): | ||
28 | return function(plugboard(reverse_rotor(text, rotor_position), plugboard_position)) | 31 | return function(plugboard(reverse_rotor(text, rotor_position), plugboard_position)) | ||
29 | return engine_decrypt | 32 | return engine_decrypt | ||
30 | return decorator | 33 | return decorator | ||
31 | 34 | ||||
32 | 35 | ||||
33 | def reverse_rotor(text, pairs): | 36 | def reverse_rotor(text, pairs): | ||
34 | return rotor(text, reverse_dictionary(pairs)) | 37 | return rotor(text, reverse_dictionary(pairs)) | ||
35 | 38 | ||||
36 | 39 | ||||
37 | def reverse_dictionary(dictionary): | 40 | def reverse_dictionary(dictionary): | ||
38 | return {value: key for key, value in dictionary.items()} | 41 | return {value: key for key, value in dictionary.items()} |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|