Домашни > Енигма > Решения > Решението на Пламена Стоянова

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

5 точки общо

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

 1def plugboard(text, set):
 2    result = ""
 3    dictionary = {' ':' '}
 4
 5    for i in set:
 6        i = list(i)
 7        dictionary[i[0]] = i[1]
 8        dictionary[i[1]] = i[0]
 9    
10    for letter in text:
11        result += dictionary.get(letter, letter)
12    
13    return result
14
15def rotor(text, dict):
16    result = ""
17
18    for letter in text:
19        result += dict[letter]
20    
21    return result
22
23
24def enigma_encrypt(plugboard_position, rotor_position):
25    def decorator(function):
26        def wrapper(text):
27            text = rotor(plugboard(text, plugboard_position), rotor_position)
28            
29            return function(text)
30        
31        return wrapper
32    
33    return decorator
34
35
36                
37def enigma_decrypt(plugboard_position, rotor_position):
38    def decorator(function):
39        def wrapper(text):
40            def reverse_rotor(dictionary):
41                result = dict()
42                for key in dictionary.keys():
43                    result[dictionary[key]] = key
44
45                return result
46            text = plugboard(rotor(text, reverse_rotor(rotor_position)), plugboard_position)
47            return function(text)
48        return wrapper
49    return 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 46, in wrapper
text = plugboard(rotor(text, reverse_rotor(rotor_position)), plugboard_position)
File "/tmp/solution.py", line 19, in rotor
result += dict[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 46, in wrapper
text = plugboard(rotor(text, reverse_rotor(rotor_position)), plugboard_position)
File "/tmp/solution.py", line 19, in rotor
result += dict[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 27, in wrapper
text = rotor(plugboard(text, plugboard_position), rotor_position)
File "/tmp/solution.py", line 19, in rotor
result += dict[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 19, in rotor
result += dict[letter]
KeyError: ' '

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

FAILED (errors=4)

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

f1def plugboard(text, set):f1def plugboard(text, set):
2    result = ""2    result = ""
3    dictionary = {' ':' '}3    dictionary = {' ':' '}
44
5    for i in set:5    for i in set:
6        i = list(i)6        i = list(i)
7        dictionary[i[0]] = i[1]7        dictionary[i[0]] = i[1]
8        dictionary[i[1]] = i[0]8        dictionary[i[1]] = i[0]
9    9    
10    for letter in text:10    for letter in text:
11        result += dictionary.get(letter, letter)11        result += dictionary.get(letter, letter)
12    12    
13    return result13    return result
1414
15def rotor(text, dict):15def rotor(text, dict):
16    result = ""16    result = ""
1717
18    for letter in text:18    for letter in text:
19        result += dict[letter]19        result += dict[letter]
20    20    
21    return result21    return result
2222
2323
24def enigma_encrypt(plugboard_position, rotor_position):24def enigma_encrypt(plugboard_position, rotor_position):
25    def decorator(function):25    def decorator(function):
26        def wrapper(text):26        def wrapper(text):
27            text = rotor(plugboard(text, plugboard_position), rotor_position)27            text = rotor(plugboard(text, plugboard_position), rotor_position)
28            28            
29            return function(text)29            return function(text)
30        30        
31        return wrapper31        return wrapper
32    32    
33    return decorator33    return decorator
3434
n35def reverse_rotor(dictionary):n
36    result = dict()
37    for key in dictionary.keys():
38        result[dictionary[key]] = key
3935
n40    return resultn
41                36                
42def enigma_decrypt(plugboard_position, rotor_position):37def enigma_decrypt(plugboard_position, rotor_position):
43    def decorator(function):38    def decorator(function):
44        def wrapper(text):39        def wrapper(text):
tt40            def reverse_rotor(dictionary):
41                result = dict()
42                for key in dictionary.keys():
43                    result[dictionary[key]] = key
44 
45                return result
45            text = plugboard(rotor(text, reverse_rotor(rotor_position)), plugboard_position)46            text = plugboard(rotor(text, reverse_rotor(rotor_position)), plugboard_position)
46            return function(text)47            return function(text)
47        return wrapper48        return wrapper
48    return decorator49    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op