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Опитай да не слагаш толкова много празни редове във функциите си. Губи се тяхната цялост и кодът се чете по-трудно.
|
n | 1 | def plugboard(str, list): | n | 1 | def plugboard(plugboard_str, plugboard_list): |
2 | conv = '' | 2 | conv = '' | ||
n | 3 | 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() | ||
n | 10 | is_found = True | n | ||
11 | break | 7 | break | ||
n | 12 | n | 8 | else: | |
13 | if is_found == False: | ||||
14 | conv += symbol | 9 | conv += symbol | ||
n | 15 | n | |||
16 | 10 | ||||
17 | return conv | 11 | return conv | ||
18 | 12 | ||||
19 | 13 | ||||
n | 20 | def rotor(str, dict): | n | 14 | def rotor(rotor_str, rotor_dict): |
21 | conv = '' | 15 | conv = '' | ||
n | 22 | n | |||
23 | for symbol in str: | 16 | for symbol in rotor_str: | ||
24 | is_found = False | 17 | 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 | ||||
34 | 18 | ||||
35 | return conv | 19 | return conv | ||
36 | 20 | ||||
37 | 21 | ||||
38 | def enigma_encrypt(plugboard_position, rotor_position): | 22 | def enigma_encrypt(plugboard_position, rotor_position): | ||
39 | 23 | ||||
n | 40 | def encrypt_decorator(func): | n | 24 | def encrypt_decorator(encrypt_func): |
41 | 25 | ||||
n | 42 | def encrypt(str): | n | 26 | 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)) | ||
45 | 29 | ||||
46 | return encrypt | 30 | return encrypt | ||
47 | 31 | ||||
48 | return encrypt_decorator | 32 | return encrypt_decorator | ||
49 | 33 | ||||
50 | 34 | ||||
51 | def enigma_decrypt(plugboard_position, rotor_position): | 35 | def enigma_decrypt(plugboard_position, rotor_position): | ||
52 | 36 | ||||
n | 53 | def decrypt_decorator(func): | n | 37 | def decrypt_decorator(decrypt_func): |
54 | 38 | ||||
n | 55 | def decrypt(str): | n | 39 | 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 | ||||
t | 61 | new_str = rotor(str, new_dict) | t | 42 | 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 decrypt | 45 | return decrypt | ||
65 | 46 | ||||
66 | return decrypt_decorator | 47 | return decrypt_decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|