1def plugboard(text, elements):
2 """Returns the result from puting a text(sentence, word, letter) through the plugboard"""
3 converted_text = ''
4 for letter in text:
5 for enter_code in elements:
6 if letter in enter_code:
7 res_letter = (enter_code - set(letter)).pop()
8 converted_text += res_letter
9 break
10 else:
11 converted_text += letter
12 return converted_text
13
14
15def rotor(text, codes):
16 """Returns the result from puting a text(sentence, word, letter) through the rotor"""
17 converted_text = ''
18 for letter in text:
19 if letter in codes:
20 converted_text += codes[letter]
21 else:
22 converted_text += letter
23 return converted_text
24
25
26def enigma_encrypt(plugboard_position, rotor_position):
27 """Returns decorator using plugboard and rotor functions to encrypt a text(sentence, word, letter)"""
28 def encrypt_wrapper(func):
29 def encrypt_text(text):
30 first_step_plugboard = plugboard(text, plugboard_position)
31 second_step_rotor = rotor(first_step_plugboard, rotor_position)
32 return func(second_step_rotor)
33 return encrypt_text
34 return encrypt_wrapper
35
36
37def enigma_decrypt(plugboard_position, rotor_position):
38 """Returns decorator using plugboard and rotor functions to decrypt a text(sentence, word, letter)"""
39 def decrypt_wrapper(func):
40 def decrypt_text(text):
41 rotor_position_reverse = {value: key for key, value in rotor_position.items()}
42 first_step_rotor = rotor(text, rotor_position_reverse)
43 second_step_plugboard = plugboard(first_step_rotor, plugboard_position)
44 return func(second_step_plugboard)
45 return decrypt_text
46 return decrypt_wrapper
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
Георги Кунчев
26.10.2023 15:30Освен имената на променливити, няма какво да кажа за решението. Добра работа! Може би един докстрингове ще го направят "перфектно".
|
f | 1 | f | 1 | ||
2 | 2 | ||||
3 | def plugboard(text, elements): | 3 | def plugboard(text, elements): | ||
n | n | 4 | """Returns the result from puting a text(sentence, word, letter) through the plugboard""" | ||
4 | converted_text = '' | 5 | converted_text = '' | ||
5 | for letter in text: | 6 | for letter in text: | ||
6 | for enter_code in elements: | 7 | for enter_code in elements: | ||
7 | if letter in enter_code: | 8 | if letter in enter_code: | ||
8 | res_letter = (enter_code - set(letter)).pop() | 9 | res_letter = (enter_code - set(letter)).pop() | ||
9 | converted_text += res_letter | 10 | converted_text += res_letter | ||
10 | break | 11 | break | ||
11 | else: | 12 | else: | ||
12 | converted_text += letter | 13 | converted_text += letter | ||
13 | return converted_text | 14 | return converted_text | ||
14 | 15 | ||||
15 | 16 | ||||
n | 16 | def rotor(word, codes): | n | 17 | def rotor(text, codes): |
18 | """Returns the result from puting a text(sentence, word, letter) through the rotor""" | ||||
17 | converted_word = '' | 19 | converted_text = '' | ||
18 | for letter in word: | 20 | for letter in text: | ||
19 | if letter in codes: | 21 | if letter in codes: | ||
n | 20 | converted_word += codes[letter] | n | 22 | converted_text += codes[letter] |
21 | else: | 23 | else: | ||
n | 22 | converted_word += letter | n | 24 | converted_text += letter |
23 | return converted_word | 25 | return converted_text | ||
24 | 26 | ||||
25 | 27 | ||||
26 | def enigma_encrypt(plugboard_position, rotor_position): | 28 | def enigma_encrypt(plugboard_position, rotor_position): | ||
n | n | 29 | """Returns decorator using plugboard and rotor functions to encrypt a text(sentence, word, letter)""" | ||
27 | def encrypt_wrapper(func): | 30 | def encrypt_wrapper(func): | ||
n | 28 | def encrypt_word(word): | n | 31 | def encrypt_text(text): |
29 | first_step_plugboard = plugboard(word, plugboard_position) | 32 | first_step_plugboard = plugboard(text, plugboard_position) | ||
30 | second_step_rotor = rotor(first_step_plugboard, rotor_position) | 33 | second_step_rotor = rotor(first_step_plugboard, rotor_position) | ||
31 | return func(second_step_rotor) | 34 | return func(second_step_rotor) | ||
n | 32 | return encrypt_word | n | 35 | return encrypt_text |
33 | return encrypt_wrapper | 36 | return encrypt_wrapper | ||
34 | 37 | ||||
35 | 38 | ||||
36 | def enigma_decrypt(plugboard_position, rotor_position): | 39 | def enigma_decrypt(plugboard_position, rotor_position): | ||
n | n | 40 | """Returns decorator using plugboard and rotor functions to decrypt a text(sentence, word, letter)""" | ||
37 | def decrypt_wrapper(func): | 41 | def decrypt_wrapper(func): | ||
n | 38 | def decrypt_word(word): | n | 42 | def decrypt_text(text): |
39 | rotor_position_reverse = {v: k for k, v in rotor_position.items()} | 43 | rotor_position_reverse = {value: key for key, value in rotor_position.items()} | ||
40 | first_step_rotor = rotor(word, rotor_position_reverse) | 44 | first_step_rotor = rotor(text, rotor_position_reverse) | ||
41 | second_step_plugboard = plugboard(first_step_rotor, plugboard_position) | 45 | second_step_plugboard = plugboard(first_step_rotor, plugboard_position) | ||
42 | return func(second_step_plugboard) | 46 | return func(second_step_plugboard) | ||
t | 43 | return decrypt_word | t | 47 | return decrypt_text |
44 | return decrypt_wrapper | 48 | return decrypt_wrapper |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | f | 1 | ||
2 | 2 | ||||
3 | def plugboard(text, elements): | 3 | def plugboard(text, elements): | ||
4 | converted_text = '' | 4 | converted_text = '' | ||
5 | for letter in text: | 5 | for letter in text: | ||
6 | for enter_code in elements: | 6 | for enter_code in elements: | ||
7 | if letter in enter_code: | 7 | if letter in enter_code: | ||
8 | res_letter = (enter_code - set(letter)).pop() | 8 | res_letter = (enter_code - set(letter)).pop() | ||
9 | converted_text += res_letter | 9 | converted_text += res_letter | ||
10 | break | 10 | break | ||
11 | else: | 11 | else: | ||
12 | converted_text += letter | 12 | converted_text += letter | ||
13 | return converted_text | 13 | return converted_text | ||
14 | 14 | ||||
15 | 15 | ||||
16 | def rotor(word, codes): | 16 | def rotor(word, codes): | ||
17 | converted_word = '' | 17 | converted_word = '' | ||
18 | for letter in word: | 18 | for letter in word: | ||
19 | if letter in codes: | 19 | if letter in codes: | ||
20 | converted_word += codes[letter] | 20 | converted_word += codes[letter] | ||
21 | else: | 21 | else: | ||
22 | converted_word += letter | 22 | converted_word += letter | ||
23 | return converted_word | 23 | return converted_word | ||
24 | 24 | ||||
25 | 25 | ||||
26 | def enigma_encrypt(plugboard_position, rotor_position): | 26 | def enigma_encrypt(plugboard_position, rotor_position): | ||
27 | def encrypt_wrapper(func): | 27 | def encrypt_wrapper(func): | ||
28 | def encrypt_word(word): | 28 | def encrypt_word(word): | ||
29 | first_step_plugboard = plugboard(word, plugboard_position) | 29 | first_step_plugboard = plugboard(word, plugboard_position) | ||
30 | second_step_rotor = rotor(first_step_plugboard, rotor_position) | 30 | second_step_rotor = rotor(first_step_plugboard, rotor_position) | ||
31 | return func(second_step_rotor) | 31 | return func(second_step_rotor) | ||
32 | return encrypt_word | 32 | return encrypt_word | ||
33 | return encrypt_wrapper | 33 | return encrypt_wrapper | ||
34 | 34 | ||||
35 | 35 | ||||
36 | def enigma_decrypt(plugboard_position, rotor_position): | 36 | def enigma_decrypt(plugboard_position, rotor_position): | ||
37 | def decrypt_wrapper(func): | 37 | def decrypt_wrapper(func): | ||
38 | def decrypt_word(word): | 38 | def decrypt_word(word): | ||
t | 39 | rotor_position_reverse = {value: key for key, value in rotor_position.items()} | t | 39 | rotor_position_reverse = {v: k for k, v in rotor_position.items()} |
40 | first_step_plugboard = rotor(word, rotor_position_reverse) | 40 | first_step_rotor = rotor(word, rotor_position_reverse) | ||
41 | second_step_rotor = plugboard(first_step_plugboard, plugboard_position) | 41 | second_step_plugboard = plugboard(first_step_rotor, plugboard_position) | ||
42 | return func(second_step_rotor) | 42 | return func(second_step_plugboard) | ||
43 | return decrypt_word | 43 | return decrypt_word | ||
44 | return decrypt_wrapper | 44 | return decrypt_wrapper |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
31.10.2023 09:33
31.10.2023 09:33