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

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

10 точки общо

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

 1def plugboard(plugboard_str, plugboard_list):
 2    conv = ''
 3    for symbol in plugboard_str:
 4        for curr_set in plugboard_list:
 5            if symbol in curr_set:
 6                conv += (curr_set - {symbol}).pop()
 7                break
 8        else:
 9            conv += symbol
10
11    return conv
12
13
14def rotor(rotor_str, rotor_dict):
15    conv = ''
16    for symbol in rotor_str:
17        conv += rotor_dict.get(symbol, symbol)
18
19    return conv
20
21
22def enigma_encrypt(plugboard_position, rotor_position):
23
24    def encrypt_decorator(encrypt_func):
25
26        def encrypt(encrypt_str):
27            new_str = plugboard(encrypt_str, plugboard_position)
28            return encrypt_func(rotor(new_str, rotor_position))
29
30        return encrypt
31        
32    return encrypt_decorator
33
34
35def enigma_decrypt(plugboard_position, rotor_position):
36
37    def decrypt_decorator(decrypt_func):
38
39        def decrypt(decrypt_str):
40            new_dict = {rotor_position[element]: element for element in rotor_position}
41            
42            new_str = rotor(decrypt_str, new_dict)
43            return decrypt_func(plugboard(new_str, plugboard_position))
44        
45        return decrypt
46    
47    return decrypt_decorator

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

OK

Дискусия
Диан Василев
31.10.2023 16:09

Правилно ли е да оставям редове между функциите когато използваме декоратор, или точно за тези празни редове става въпрос, че са излишни?
Георги Кунчев
30.10.2023 09:42

Опитай да не слагаш толкова много празни редове във функциите си. Губи се тяхната цялост и кодът се чете по-трудно.
История

n1def plugboard(str, list):n1def plugboard(plugboard_str, plugboard_list):
2    conv = ''2    conv = ''
n3 n
4    for symbol in str:3    for symbol in plugboard_str:
5        is_found = False
6 
7        for curr_set in list:4        for curr_set in plugboard_list:
8            if symbol in curr_set:5            if symbol in curr_set:
9                conv += (curr_set - {symbol}).pop()6                conv += (curr_set - {symbol}).pop()
n10                is_found = Truen
11                break7                break
n12 n8        else:
13        if is_found == False:
14            conv += symbol9            conv += symbol
n15 n
1610
17    return conv11    return conv
1812
1913
n20def rotor(str, dict):n14def rotor(rotor_str, rotor_dict):
21    conv = ''15    conv = ''
n22 n
23    for symbol in str:16    for symbol in rotor_str:
24        is_found = False17        conv += rotor_dict.get(symbol, symbol)
25 
26        for key in dict:
27            if symbol == key:
28                conv += dict[key]
29                is_found = True
30                break
31 
32        if is_found == False:
33            conv += symbol
3418
35    return conv19    return conv
3620
3721
38def enigma_encrypt(plugboard_position, rotor_position):22def enigma_encrypt(plugboard_position, rotor_position):
3923
n40    def encrypt_decorator(func):n24    def encrypt_decorator(encrypt_func):
4125
n42        def encrypt(str):n26        def encrypt(encrypt_str):
43            new_str = plugboard(str, plugboard_position)27            new_str = plugboard(encrypt_str, plugboard_position)
44            return func(rotor(new_str, rotor_position))28            return encrypt_func(rotor(new_str, rotor_position))
4529
46        return encrypt30        return encrypt
47        31        
48    return encrypt_decorator32    return encrypt_decorator
4933
5034
51def enigma_decrypt(plugboard_position, rotor_position):35def enigma_decrypt(plugboard_position, rotor_position):
5236
n53    def decrypt_decorator(func):n37    def decrypt_decorator(decrypt_func):
5438
n55        def decrypt(str):n39        def decrypt(decrypt_str):
56            new_dict = {}40            new_dict = {rotor_position[element]: element for element in rotor_position}
57 
58            for key in rotor_position:
59                new_dict[rotor_position[key]] = key
60            41            
t61            new_str = rotor(str, new_dict)t42            new_str = rotor(decrypt_str, new_dict)
62            return func(plugboard(new_str, plugboard_position))43            return decrypt_func(plugboard(new_str, plugboard_position))
63        44        
64        return decrypt45        return decrypt
65    46    
66    return decrypt_decorator47    return decrypt_decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op