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

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

6 точки общо

5 успешни теста
4 неуспешни теста
Код

 1def plugboard(text, plugboard_position):
 2    converted_text = ''
 3    for char_text in text:
 4        for plug_set in plugboard_position:
 5            if char_text in plug_set:
 6                converted_text += plug_set.difference({char_text}).pop()
 7                break
 8        else:
 9            converted_text += char_text
10    return converted_text
11
12
13def rotor(text, rotor_position):
14    converted_text = ''
15    for char_text in text:
16        converted_text += rotor_position[char_text]
17    return converted_text
18
19
20def enigma_encrypt(plugboard_position, rotor_position):
21    def encrypt_decorator(func):
22        def encrypt_wrapper(text):
23            text = plugboard(text, plugboard_position)
24            text = rotor(text, rotor_position)
25
26            return func(text)
27
28        return encrypt_wrapper
29
30    return encrypt_decorator
31
32
33def enigma_decrypt(plugboard_position, rotor_position):
34    def decrypt_decorator(func):
35        def decrypt_wrapper(text):
36            text = rotor(text, {v: k for k, v in rotor_position.items()})
37            text = plugboard(text, plugboard_position)
38
39            return func(text)
40
41        return decrypt_wrapper
42
43    return decrypt_decorator

E.EE....E
======================================================================
ERROR: 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')
File "/tmp/solution.py", line 36, in decrypt_wrapper
text = rotor(text, {v: k for k, v in rotor_position.items()})
File "/tmp/solution.py", line 16, in rotor
converted_text += rotor_position[char_text]
KeyError: ' '

======================================================================
ERROR: 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'),
File "/tmp/solution.py", line 36, in decrypt_wrapper
text = rotor(text, {v: k for k, v in rotor_position.items()})
File "/tmp/solution.py", line 16, in rotor
converted_text += rotor_position[char_text]
KeyError: ' '

======================================================================
ERROR: 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'),
File "/tmp/solution.py", line 24, in encrypt_wrapper
text = rotor(text, rotor_position)
File "/tmp/solution.py", line 16, in rotor
converted_text += rotor_position[char_text]
KeyError: ' '

======================================================================
ERROR: test_normal_case (test.TestRotor)
Test the rotor function with normally expected input.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 67, in test_normal_case
self.assertEqual(rotor('this is a test input', self.ROTOR_POSITION), 'kbjo jo c kdok jylqk')
File "/tmp/solution.py", line 16, in rotor
converted_text += rotor_position[char_text]
KeyError: ' '

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

FAILED (errors=4)

Дискусия
История

f1def plugboard(text, plugboard_position):f1def plugboard(text, plugboard_position):
2    converted_text = ''2    converted_text = ''
n3    for c in text:n3    for char_text in text:
4        for plug_set in plugboard_position:4        for plug_set in plugboard_position:
n5            if c in plug_set:n5            if char_text in plug_set:
6                converted_text += plug_set.difference({c}).pop()6                converted_text += plug_set.difference({char_text}).pop()
7                break7                break
8        else:8        else:
n9            converted_text += cn9            converted_text += char_text
10    return converted_text10    return converted_text
1111
1212
13def rotor(text, rotor_position):13def rotor(text, rotor_position):
14    converted_text = ''14    converted_text = ''
n15    for c in text:n15    for char_text in text:
16        converted_text += rotor_position[c]16        converted_text += rotor_position[char_text]
17    return converted_text17    return converted_text
1818
1919
20def enigma_encrypt(plugboard_position, rotor_position):20def enigma_encrypt(plugboard_position, rotor_position):
21    def encrypt_decorator(func):21    def encrypt_decorator(func):
22        def encrypt_wrapper(text):22        def encrypt_wrapper(text):
23            text = plugboard(text, plugboard_position)23            text = plugboard(text, plugboard_position)
24            text = rotor(text, rotor_position)24            text = rotor(text, rotor_position)
2525
n26            result = func(text)n26            return func(text)
27            return result
2827
29        return encrypt_wrapper28        return encrypt_wrapper
3029
31    return encrypt_decorator30    return encrypt_decorator
3231
3332
34def enigma_decrypt(plugboard_position, rotor_position):33def enigma_decrypt(plugboard_position, rotor_position):
35    def decrypt_decorator(func):34    def decrypt_decorator(func):
36        def decrypt_wrapper(text):35        def decrypt_wrapper(text):
37            text = rotor(text, {v: k for k, v in rotor_position.items()})36            text = rotor(text, {v: k for k, v in rotor_position.items()})
38            text = plugboard(text, plugboard_position)37            text = plugboard(text, plugboard_position)
3938
t40            result = func(text)t39            return func(text)
41            return result
4240
43        return decrypt_wrapper41        return decrypt_wrapper
4442
45    return decrypt_decorator43    return decrypt_decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op