Домашни > Енигма > Решения > Решението на Атанас Ников

Резултати
10 точки от тестове
0 точки от учител

10 точки общо

9 успешни теста
0 неуспешни теста
Код

 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

Имаш няколко помощни функции, които изглеждат разхвърляни в кода. Препоръчвам да ги събереш в началото или в края. Доколко всяка от тях е нужда, е въпрос на лично мнение, така че това няма да го коментирам.
История

f1EMPTY_STRING = ''f1EMPTY_STRING = ''
22
33
4def plugboard(base_text, pairs):4def 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])
66
77
n8def new_mapping(symbol, pairs):n
9    return next((counterpart(symbol, pair) for pair in pairs if symbol in pair), symbol)
10 
11 
12def counterpart(symbol, pair):
13    return (pair - {symbol}).pop()
14 
15 
16def rotor(base_text, alphabet_map):8def rotor(base_text, alphabet_map):
n17    return EMPTY_STRING.join([alphabet_map[symbol] for symbol in base_text])n9    return EMPTY_STRING.join([alphabet_map.get(symbol, symbol) for symbol in base_text])
1810
1911
20def enigma_encrypt(plugboard_position, rotor_position):12def 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_encrypt16        return engine_encrypt
25    return decorator17    return decorator
2618
2719
28def enigma_decrypt(plugboard_position, rotor_position):20def 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_decrypt24        return engine_decrypt
33    return decorator25    return decorator
3426
3527
tt28def 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):36def reverse_rotor(text, pairs):
37    return rotor(text, reverse_dictionary(pairs))37    return rotor(text, reverse_dictionary(pairs))
3838
3939
40def reverse_dictionary(dictionary):40def reverse_dictionary(dictionary):
41    return {value: key for key, value in dictionary.items()}41    return {value: key for key, value in dictionary.items()}
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

nn1EMPTY_STRING = ''
2 
3 
1def plugboard(base_text, pairs):4def plugboard(base_text, pairs):
n2    return ''.join([new_mapping(symbol, pairs) for symbol in base_text])n5    return EMPTY_STRING.join([new_mapping(symbol, pairs) for symbol in base_text])
36
47
5def new_mapping(symbol, pairs):8def 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)
710
811
9def counterpart(symbol, pair):12def counterpart(symbol, pair):
10    return (pair - {symbol}).pop()13    return (pair - {symbol}).pop()
1114
1215
13def rotor(base_text, alphabet_map):16def rotor(base_text, alphabet_map):
t14    return ''.join([alphabet_map[symbol] for symbol in base_text])t17    return EMPTY_STRING.join([alphabet_map[symbol] for symbol in base_text])
1518
1619
17def enigma_encrypt(plugboard_position, rotor_position):20def 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_encrypt24        return engine_encrypt
22    return decorator25    return decorator
2326
2427
25def enigma_decrypt(plugboard_position, rotor_position):28def 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_decrypt32        return engine_decrypt
30    return decorator33    return decorator
3134
3235
33def reverse_rotor(text, pairs):36def reverse_rotor(text, pairs):
34    return rotor(text, reverse_dictionary(pairs))37    return rotor(text, reverse_dictionary(pairs))
3538
3639
37def reverse_dictionary(dictionary):40def reverse_dictionary(dictionary):
38    return {value: key for key, value in dictionary.items()}41    return {value: key for key, value in dictionary.items()}
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op