Домашни > Енигма > Решения > Решението на Михаил Щерев

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

10 точки общо

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

 1def plugboard(text, plugboard_position):
 2    encypted_text = ""
 3    for letter in text:
 4        new_letter = letter
 5        for pair in plugboard_position:
 6            if letter in pair:
 7                new_letter = "".join(pair - {letter})
 8        encypted_text += new_letter
 9    return encypted_text
10
11
12def rotor(text, rotor_position):
13    encrypted_text = ""
14    for letter in text:
15        encrypted_text += rotor_position.get(letter, ' ')
16    return encrypted_text
17
18
19def enigma_encrypt(plugboard_position, rotor_position):
20    def decorator_encrypt(func):
21        def encrypt(text):
22            return func(rotor(plugboard(text, plugboard_position), rotor_position))
23        return encrypt
24    return decorator_encrypt
25
26def enigma_decrypt(plugboard_position, rotor_position):
27    reverse_rotor_position = {}
28    for key_letter in rotor_position:
29        reverse_rotor_position[rotor_position[key_letter]] = key_letter
30    def decorator_decrypt(func):
31        def decrypt(text):
32            return func(plugboard(rotor(text, reverse_rotor_position), plugboard_position))
33        return decrypt
34    return decorator_decrypt

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

OK

Дискусия
История

f1f1
2def plugboard(text, plugboard_position):2def plugboard(text, plugboard_position):
3    encypted_text = ""3    encypted_text = ""
4    for letter in text:4    for letter in text:
5        new_letter = letter5        new_letter = letter
6        for pair in plugboard_position:6        for pair in plugboard_position:
7            if letter in pair:7            if letter in pair:
8                new_letter = "".join(pair - {letter})8                new_letter = "".join(pair - {letter})
9        encypted_text += new_letter9        encypted_text += new_letter
10    return encypted_text10    return encypted_text
1111
1212
13def rotor(text, rotor_position):13def rotor(text, rotor_position):
14    encrypted_text = ""14    encrypted_text = ""
15    for letter in text:15    for letter in text:
t16        if letter != ' ':t
17            encrypted_text += rotor_position[letter]16        encrypted_text += rotor_position.get(letter, ' ')
18        else:
19            encrypted_text += ' '
20    return encrypted_text17    return encrypted_text
2118
2219
23def enigma_encrypt(plugboard_position, rotor_position):20def enigma_encrypt(plugboard_position, rotor_position):
24    def decorator_encrypt(func):21    def decorator_encrypt(func):
25        def encrypt(text):22        def encrypt(text):
26            return func(rotor(plugboard(text, plugboard_position), rotor_position))23            return func(rotor(plugboard(text, plugboard_position), rotor_position))
27        return encrypt24        return encrypt
28    return decorator_encrypt25    return decorator_encrypt
2926
30def enigma_decrypt(plugboard_position, rotor_position):27def enigma_decrypt(plugboard_position, rotor_position):
31    reverse_rotor_position = {}28    reverse_rotor_position = {}
32    for key_letter in rotor_position:29    for key_letter in rotor_position:
33        reverse_rotor_position[rotor_position[key_letter]] = key_letter30        reverse_rotor_position[rotor_position[key_letter]] = key_letter
34    def decorator_decrypt(func):31    def decorator_decrypt(func):
35        def decrypt(text):32        def decrypt(text):
36            return func(plugboard(rotor(text, reverse_rotor_position), plugboard_position))33            return func(plugboard(rotor(text, reverse_rotor_position), plugboard_position))
37        return decrypt34        return decrypt
38    return decorator_decrypt35    return decorator_decrypt
3936
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op