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

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

6 точки общо

5 успешни теста
4 неуспешни теста
Код (Този път успях на време ;))
Скрий всички коментари

 1def get_corresponding_letter(letter, code):
 2    for (x, y) in code:
 3        if letter == x:
 4            return y
 5        elif letter == y:
 6            return x
 7    
 8    return letter
 9
10def plugboard(text, code):
11    encoded_text = ""
12    
13    for letter in text:
14        encoded_text += get_corresponding_letter(letter, code)
15        
16    return encoded_text
17
18def rotor(text, code):
19    encoded_text = ""
20    
21    for letter in text:
22         encoded_text += code[letter]
23         
24    return encoded_text
25
26def enigma_encrypt(plugboard_position = [], rotor_position = {}):
27    def encrypt(func):
28        def encrypt_text(text):
29            text = plugboard(text, plugboard_position)
30            text = rotor(text, rotor_position)
31            
32            return func(text)
33        return encrypt_text
34    return encrypt
35
36def enigma_decrypt(plugboard_position = [], rotor_position = {}):
37    def decrypt(func):
38        def decrypt_text(text):
39            text = rotor(text, {value: key for key, value in rotor_position.items()})
40            text = plugboard(text, plugboard_position)
41            
42            return func(text)
43        return decrypt_text
44    return decrypt

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 39, in decrypt_text
text = rotor(text, {value: key for key, value in rotor_position.items()})
File "/tmp/solution.py", line 22, in rotor
encoded_text += code[letter]
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 39, in decrypt_text
text = rotor(text, {value: key for key, value in rotor_position.items()})
File "/tmp/solution.py", line 22, in rotor
encoded_text += code[letter]
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 30, in encrypt_text
text = rotor(text, rotor_position)
File "/tmp/solution.py", line 22, in rotor
encoded_text += code[letter]
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 22, in rotor
encoded_text += code[letter]
KeyError: ' '

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

FAILED (errors=4)

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