Домашни > Енигма > Решения > Решението на Иван Дянков

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

8 точки общо

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

 1def get_other_character_in_set(character_to_switch, set_of_characters):
 2    diff_set = set_of_characters.difference([character_to_switch])
 3    element = next(iter(diff_set))
 4    return element
 5
 6
 7def plugboard(str_to_convert, list_of_pairs):
 8    for count, char in enumerate(str_to_convert):
 9        for current_set in list_of_pairs:
10            if char in current_set:
11                str_to_convert = str_to_convert[:count] + str_to_convert[count:].replace(char,
12                                                                            get_other_character_in_set(char, current_set))
13    return str_to_convert
14
15
16def rotor(str_to_convert, dict_of_pairs):
17    for count, char in enumerate(str_to_convert):
18        if char in dict_of_pairs:
19            str_to_convert = str_to_convert[:count] + str_to_convert[count:].replace(char, dict_of_pairs[char])
20    return str_to_convert
21
22
23def enigma_encrypt(plugboard_position, rotor_position):
24    def encrypt(func_to_encrypt):
25        def encrypt_text(str_to_encrypt):
26            str_to_encrypt = plugboard(str_to_encrypt, plugboard_position)
27            str_to_encrypt = rotor(str_to_encrypt, rotor_position)
28            return func_to_encrypt(str_to_encrypt)
29        return encrypt_text
30    return encrypt
31
32
33def enigma_decrypt(plugboard_position, rotor_position):
34    def decrypt(func_to_decrypt):
35        def decrypt_text(str_to_decrypt):
36            inverted_dict = {current_value: current_key for current_key, current_value in rotor_position.items()}
37            str_to_decrypt = rotor(str_to_decrypt, inverted_dict)
38            str_to_decrypt = plugboard(str_to_decrypt, plugboard_position)
39            return func_to_decrypt(str_to_decrypt)
40        return decrypt_text
41    return decrypt

..FF.....
======================================================================
FAIL: test_full_letter_set (test.TestDecryptor)
Test the decryptor function with all letters in the rotor.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 112, in test_full_letter_set
self.assertEqual(decrypted('mlx fuver cbakn jad guoyq aixb mlx pzhw sat'),
AssertionError: 'the quick brown fox jumps mvbr thb lazy dtg' != 'the quick brown fox jumps over the lazy dog'
- the quick brown fox jumps mvbr thb lazy dtg
? ^ ^ ^ ^
+ the quick brown fox jumps over the lazy dog
? ^ ^ ^ ^

======================================================================
FAIL: test_full_letter_set (test.TestEncryptor)
Test the encryptor function with all letters in the rotor.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 89, in test_full_letter_set
self.assertEqual(encrypted('the quick brown fox jumps over the lazy dog'),
AssertionError: 'mjg cavsk nrqfy zqd haulp qxgr yjg itob eqw' != 'mjg cavsk nrqfy zqd haulp qxgr mjg itob eqw'
- mjg cavsk nrqfy zqd haulp qxgr yjg itob eqw
? ^
+ mjg cavsk nrqfy zqd haulp qxgr mjg itob eqw
? ^

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

FAILED (failures=2)

Дискусия
Виктор Бечев
31.10.2023 16:22

Добре написано като цяло домашно, браво.
История
Това решение има само една версия.