Домашни > Енигма > Решения > Решението на Гергана Благоева

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

10 точки общо

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

 1def rotor(text, rotor_position):
 2    converted_text = ''
 3    for character in text:
 4        if character == ' ':
 5            converted_text += character
 6        else:
 7            converted_text += rotor_position[character]
 8    return converted_text    
 9
10
11def plugboard(text, plugboard_position):
12    new_list = []
13    for each_list in plugboard_position:
14        new_list.append(list(each_list))
15
16    new_dict = {}
17    for each_list in new_list:
18        new_dict[each_list[0]] = each_list[-1]
19        new_dict[each_list[-1]] = each_list[0]
20
21    converted_text = ''
22    for character in text:
23        converted_text += new_dict.get(character, character)  
24    return converted_text
25
26
27def enigma_encrypt(plugboard_position, rotor_position):
28    def decorator(func):
29        def encrypt_text(text):
30            text = rotor(plugboard(text, plugboard_position), rotor_position)
31            return func(text)
32        return encrypt_text
33    return decorator
34
35
36def enigma_decrypt(plugboard_position, rotor_position):
37    def decorator(func):
38        def decrypt_text(text):
39            new_rotor_position = {value: key for key, value in rotor_position.items()}
40            text = plugboard(rotor(text, new_rotor_position), plugboard_position)
41            return func(text)
42        return decrypt_text
43    return decorator

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

OK

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

n1def rotor (text, rotor_position):n1def rotor(text, rotor_position):
2    converted_text = str()2    converted_text = ''
3    for character in text:3    for character in text:
4        if character == ' ':4        if character == ' ':
5            converted_text += character5            converted_text += character
6        else:6        else:
7            converted_text += rotor_position[character]7            converted_text += rotor_position[character]
8    return converted_text    8    return converted_text    
99
1010
n11def plugboard (text, plugboard_position):n11def plugboard(text, plugboard_position):
12    new_list = []12    new_list = []
13    for each_list in plugboard_position:13    for each_list in plugboard_position:
14        new_list.append(list(each_list))14        new_list.append(list(each_list))
1515
16    new_dict = {}16    new_dict = {}
17    for each_list in new_list:17    for each_list in new_list:
18        new_dict[each_list[0]] = each_list[-1]18        new_dict[each_list[0]] = each_list[-1]
19        new_dict[each_list[-1]] = each_list[0]19        new_dict[each_list[-1]] = each_list[0]
2020
n21    converted_text = str()n21    converted_text = ''
22    for character in text:22    for character in text:
t23        if character in new_dict:t
24            converted_text += new_dict[character]23        converted_text += new_dict.get(character, character)  
25        else:
26            converted_text += character
27    return converted_text24    return converted_text
2825
2926
30def enigma_encrypt(plugboard_position, rotor_position):27def enigma_encrypt(plugboard_position, rotor_position):
31    def decorator(func):28    def decorator(func):
32        def encrypt_text(text):29        def encrypt_text(text):
33            text = rotor(plugboard(text, plugboard_position), rotor_position)30            text = rotor(plugboard(text, plugboard_position), rotor_position)
34            return func(text)31            return func(text)
35        return encrypt_text32        return encrypt_text
36    return decorator33    return decorator
3734
3835
39def enigma_decrypt(plugboard_position, rotor_position):36def enigma_decrypt(plugboard_position, rotor_position):
40    def decorator(func):37    def decorator(func):
41        def decrypt_text(text):38        def decrypt_text(text):
42            new_rotor_position = {value: key for key, value in rotor_position.items()}39            new_rotor_position = {value: key for key, value in rotor_position.items()}
43            text = plugboard(rotor(text, new_rotor_position), plugboard_position)40            text = plugboard(rotor(text, new_rotor_position), plugboard_position)
44            return func(text)41            return func(text)
45        return decrypt_text42        return decrypt_text
46    return decorator43    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op