Домашни > Енигма > Решения > Решението на Тихомир Божков

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

9 точки общо

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

 1def plugboard(text, plugboard_position):
 2    result = []
 3    for character_in_text in text:
 4        for pair in plugboard_position:
 5            if character_in_text in pair:
 6                result.append(pair.difference({character_in_text}).pop())
 7                break
 8        else:
 9            result.append(character_in_text)
10    return ''.join(result)
11
12def rotor(text, rotor_position):
13    transformed_text = []
14    for original_char in text:
15        mapped_char = rotor_position.get(original_char, original_char)
16        transformed_text.append(mapped_char)
17    return ''.join(transformed_text)
18
19def enigma_encrypt(plugboard_position, rotor_position):
20    def decorator(func):
21        def encrypt_and_call(text):
22            encrypted_text = plugboard(text, plugboard_position)
23            encrypted_text = rotor(encrypted_text, rotor_position)
24            func(encrypted_text)
25            return encrypted_text
26        return encrypt_and_call
27    return decorator
28
29def enigma_decrypt(plugboard_position, rotor_position):
30    def decorator(func):
31        def decrypt_and_call(text):
32            decrypted_text = rotor(text, {mapped_char: original_char for original_char, mapped_char in rotor_position.items()})
33            decrypted_text = plugboard(decrypted_text, plugboard_position)
34            func(decrypted_text)
35            return decrypted_text
36        return decrypt_and_call
37    return decorator
38
39"""
40Could I rewrite the rotor functions as
41def rotor(text, rotor_position):
42    return ''.join(rotor_position.get(original_char, original_char) for original_char in text)
43"""

F........
======================================================================
FAIL: test_full_letter_set (test.TestCombination)
Test decrypting an encrypted text against itself.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 139, in test_full_letter_set
self.assertEqual(combined('i love python'), 'i love python')
AssertionError: 'a gnjd korynm' != 'i love python'
- a gnjd korynm
+ i love python

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

FAILED (failures=1)

Дискусия
История
Това решение има само една версия.