Домашни > Енигма > Решения > Решението на Милица Тончева

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

6 точки общо

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

 1def plugboard(text, couples_of_letters):
 2    new_string = ''
 3    for letter in text:
 4        for (x, y) in couples_of_letters:
 5            if letter == x:
 6                letter = y
 7            elif letter == y:
 8                letter = x
 9        new_string += letter
10    
11    return new_string
12
13
14def rotor(text, symbol_dictionary):
15    new_string = ''
16    for letter in text:
17        letter = symbol_dictionary[letter]
18        new_string += letter
19    
20    return new_string
21
22
23def enigma_encrypt(plugboard_position=[], rotor_position={}):
24    def decorator(func):
25        def encrypt(text):
26            new_string = plugboard(text, plugboard_position)
27            new_string = rotor(new_string, rotor_position)
28            func(new_string)
29        return encrypt
30    return decorator
31
32
33def enigma_decrypt(plugboard_position=[], rotor_position={}):
34    def decorator_dec(func):
35        def decrypt(text):
36            new_string = rotor(text, {v: k for k, v in rotor_position.items()})
37            new_string = plugboard(new_string, plugboard_position)
38            func(new_string)
39        return decrypt
40    return decorator_dec

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
new_string = rotor(text, {v: k for k, v in rotor_position.items()})
File "/tmp/solution.py", line 17, in rotor
letter = symbol_dictionary[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 36, in decrypt
new_string = rotor(text, {v: k for k, v in rotor_position.items()})
File "/tmp/solution.py", line 17, in rotor
letter = symbol_dictionary[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 encrypt
new_string = rotor(new_string, rotor_position)
File "/tmp/solution.py", line 17, in rotor
letter = symbol_dictionary[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 17, in rotor
letter = symbol_dictionary[letter]
KeyError: ' '

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

FAILED (errors=4)

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

f1def plugboard(text, couples_of_letters):f1def plugboard(text, couples_of_letters):
2    new_string = ''2    new_string = ''
3    for letter in text:3    for letter in text:
4        for (x, y) in couples_of_letters:4        for (x, y) in couples_of_letters:
5            if letter == x:5            if letter == x:
6                letter = y6                letter = y
7            elif letter == y:7            elif letter == y:
8                letter = x8                letter = x
9        new_string += letter9        new_string += letter
10    10    
11    return new_string11    return new_string
1212
1313
14def rotor(text, symbol_dictionary):14def rotor(text, symbol_dictionary):
15    new_string = ''15    new_string = ''
16    for letter in text:16    for letter in text:
17        letter = symbol_dictionary[letter]17        letter = symbol_dictionary[letter]
18        new_string += letter18        new_string += letter
19    19    
20    return new_string20    return new_string
2121
2222
23def enigma_encrypt(plugboard_position=[], rotor_position={}):23def enigma_encrypt(plugboard_position=[], rotor_position={}):
24    def decorator(func):24    def decorator(func):
25        def encrypt(text):25        def encrypt(text):
26            new_string = plugboard(text, plugboard_position)26            new_string = plugboard(text, plugboard_position)
27            new_string = rotor(new_string, rotor_position)27            new_string = rotor(new_string, rotor_position)
28            func(new_string)28            func(new_string)
29        return encrypt29        return encrypt
30    return decorator30    return decorator
3131
3232
33def enigma_decrypt(plugboard_position=[], rotor_position={}):33def enigma_decrypt(plugboard_position=[], rotor_position={}):
34    def decorator_dec(func):34    def decorator_dec(func):
35        def decrypt(text):35        def decrypt(text):
36            new_string = rotor(text, {v: k for k, v in rotor_position.items()})36            new_string = rotor(text, {v: k for k, v in rotor_position.items()})
37            new_string = plugboard(new_string, plugboard_position)37            new_string = plugboard(new_string, plugboard_position)
38            func(new_string)38            func(new_string)
39        return decrypt39        return decrypt
40    return decorator_dec40    return decorator_dec
4141
t42plugboard_position = [{'a', 'c'}, {'t', 'z'}]t
43rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',
44                  's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',
45                  'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',
46                  'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',
47                  'm': 'r', 'c': 'k'}
4842
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def plugboard(text, couples_of_letters):f1def plugboard(text, couples_of_letters):
2    new_string = ''2    new_string = ''
3    for letter in text:3    for letter in text:
4        for (x, y) in couples_of_letters:4        for (x, y) in couples_of_letters:
5            if letter == x:5            if letter == x:
6                letter = y6                letter = y
7            elif letter == y:7            elif letter == y:
8                letter = x8                letter = x
9        new_string += letter9        new_string += letter
10    10    
11    return new_string11    return new_string
1212
1313
14def rotor(text, symbol_dictionary):14def rotor(text, symbol_dictionary):
15    new_string = ''15    new_string = ''
16    for letter in text:16    for letter in text:
n17        for key in symbol_dictionary:n
18            if letter == key:
19                letter = symbol_dictionary[key]17        letter = symbol_dictionary[letter]
20                break
21        new_string += letter18        new_string += letter
22    19    
23    return new_string20    return new_string
2421
2522
n26def enigma_encrypt(plugboard_position = [], rotor_position = {}):n23def enigma_encrypt(plugboard_position=[], rotor_position={}):
27    def decorator(func):24    def decorator(func):
28        def encrypt(text):25        def encrypt(text):
29            new_string = plugboard(text, plugboard_position)26            new_string = plugboard(text, plugboard_position)
30            new_string = rotor(new_string, rotor_position)27            new_string = rotor(new_string, rotor_position)
31            func(new_string)28            func(new_string)
32        return encrypt29        return encrypt
33    return decorator30    return decorator
3431
3532
n36def enigma_decrypt(plugboard_position = [], rotor_position = {}):n33def enigma_decrypt(plugboard_position=[], rotor_position={}):
37    def decorator_dec(func):34    def decorator_dec(func):
38        def decrypt(text):35        def decrypt(text):
39            new_string = rotor(text, {v: k for k, v in rotor_position.items()})36            new_string = rotor(text, {v: k for k, v in rotor_position.items()})
40            new_string = plugboard(new_string, plugboard_position)37            new_string = plugboard(new_string, plugboard_position)
41            func(new_string)38            func(new_string)
42        return decrypt39        return decrypt
43    return decorator_dec40    return decorator_dec
4441
tt42plugboard_position = [{'a', 'c'}, {'t', 'z'}]
43rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',
44                  's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',
45                  'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',
46                  'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',
47                  'm': 'r', 'c': 'k'}
48 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def plugboard(text, couples_of_letters):f1def plugboard(text, couples_of_letters):
n2    newString = ''n2    new_string = ''
3    for letter in text:3    for letter in text:
4        for (x, y) in couples_of_letters:4        for (x, y) in couples_of_letters:
5            if letter == x:5            if letter == x:
6                letter = y6                letter = y
7            elif letter == y:7            elif letter == y:
8                letter = x8                letter = x
n9        newString += lettern9        new_string += letter
10    10    
n11    return newStringn11    return new_string
1212
1313
14def rotor(text, symbol_dictionary):14def rotor(text, symbol_dictionary):
n15    newString = ''n15    new_string = ''
16    for letter in text:16    for letter in text:
17        for key in symbol_dictionary:17        for key in symbol_dictionary:
18            if letter == key:18            if letter == key:
19                letter = symbol_dictionary[key]19                letter = symbol_dictionary[key]
20                break20                break
n21        newString += lettern21        new_string += letter
22    22    
n23    return newStringn23    return new_string
2424
2525
26def enigma_encrypt(plugboard_position = [], rotor_position = {}):26def enigma_encrypt(plugboard_position = [], rotor_position = {}):
27    def decorator(func):27    def decorator(func):
28        def encrypt(text):28        def encrypt(text):
n29            newString = plugboard(text, plugboard_position)n29            new_string = plugboard(text, plugboard_position)
30            newString = rotor(newString, rotor_position)30            new_string = rotor(new_string, rotor_position)
31            func(newString)31            func(new_string)
32        return encrypt32        return encrypt
33    return decorator33    return decorator
3434
3535
36def enigma_decrypt(plugboard_position = [], rotor_position = {}):36def enigma_decrypt(plugboard_position = [], rotor_position = {}):
37    def decorator_dec(func):37    def decorator_dec(func):
38        def decrypt(text):38        def decrypt(text):
t39            newString = rotor(text, {v: k for k, v in rotor_position.items()})t39            new_string = rotor(text, {v: k for k, v in rotor_position.items()})
40            newString = plugboard(newString, plugboard_position)40            new_string = plugboard(new_string, plugboard_position)
41            func(newString)41            func(new_string)
42        return decrypt42        return decrypt
43    return decorator_dec43    return decorator_dec
4444
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op