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

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

10 точки общо

9 успешни теста
0 неуспешни теста
Код
Скрий всички коментари

 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

Освен имената на променливити, няма какво да кажа за решението. Добра работа! Може би един докстрингове ще го направят "перфектно".
История

f1f1
22
3def plugboard(text, elements):3def plugboard(text, elements):
nn4    """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_letter10                converted_text += res_letter
10                break11                break
11        else:12        else:
12            converted_text += letter13            converted_text += letter
13    return converted_text14    return converted_text
1415
1516
n16def rotor(word, codes):n17def 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:
n20            converted_word += codes[letter]n22            converted_text += codes[letter]
21        else:23        else:
n22            converted_word += lettern24            converted_text += letter
23    return converted_word25    return converted_text
2426
2527
26def enigma_encrypt(plugboard_position, rotor_position):28def enigma_encrypt(plugboard_position, rotor_position):
nn29    """Returns decorator using plugboard and rotor functions to encrypt a text(sentence, word, letter)"""
27    def encrypt_wrapper(func):30    def encrypt_wrapper(func):
n28        def encrypt_word(word):n31        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)
n32        return encrypt_wordn35        return encrypt_text
33    return encrypt_wrapper36    return encrypt_wrapper
3437
3538
36def enigma_decrypt(plugboard_position, rotor_position):39def enigma_decrypt(plugboard_position, rotor_position):
nn40    """Returns decorator using plugboard and rotor functions to decrypt a text(sentence, word, letter)"""
37    def decrypt_wrapper(func):41    def decrypt_wrapper(func):
n38        def decrypt_word(word):n42        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)
t43        return decrypt_wordt47        return decrypt_text
44    return decrypt_wrapper48    return decrypt_wrapper
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1f1
22
3def plugboard(text, elements):3def 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_letter9                converted_text += res_letter
10                break10                break
11        else:11        else:
12            converted_text += letter12            converted_text += letter
13    return converted_text13    return converted_text
1414
1515
16def rotor(word, codes):16def 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 += letter22            converted_word += letter
23    return converted_word23    return converted_word
2424
2525
26def enigma_encrypt(plugboard_position, rotor_position):26def 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_word32        return encrypt_word
33    return encrypt_wrapper33    return encrypt_wrapper
3434
3535
36def enigma_decrypt(plugboard_position, rotor_position):36def 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):
t39            rotor_position_reverse = {value: key for key, value in rotor_position.items()}t39            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_word43        return decrypt_word
44    return decrypt_wrapper44    return decrypt_wrapper
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op