Домашни > Енигма > Решения > Решението на Йолина Вълчева

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

10 точки общо

9 успешни теста
0 неуспешни теста
Код
Скрий всички коментари

 1def plugboard(text, list_of_pairs):
 2    new_text = ""
 3    for letter in text:
 4        for pair in list_of_pairs:
 5            first, second = pair
 6            if letter == first:
 7                letter = second
 8            elif letter == second:
 9                letter = first
10        new_text += letter
11    return new_text
12
13
14def rotor(text, dictionary):
15    new_text = ""
16    for letter in text:
17        if letter in dictionary:
18            letter = dictionary[letter]
19        new_text += letter
20    return new_text
21
22
23def enigma_encrypt(plugboard_position, rotor_position):
24    def decorator_encrypt(function):
25        def encrypt(text):
26            new_text = plugboard(text, plugboard_position)
27            return function(rotor(new_text, rotor_position))
28        return encrypt
29    return decorator_encrypt
30
31
32def enigma_decrypt(plugboard_position, rotor_position):
33    def decorator_decrypt(function):
34        def decrypt(text):
35            # in order to retrieve the "real" letter, must swap the value and the key, since it was previously encrypted by key to value
36            rotor_position_swap = {value: key for key,
37                                   value in rotor_position.items()}
38            new_text = rotor(text, rotor_position_swap)
39            return function(plugboard(new_text, plugboard_position))
40        return decrypt
41    return decorator_decrypt

.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s

OK

Дискусия
История
Това решение има само една версия.