Домашни > Енигма > Решения > Решението на Божидар Кьоров

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

6 точки общо

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

 1def plugboard(message: str, plugboard_position: list):
 2    """Changes every letter from message in a pair from plugboard_position with its pair."""
 3    res = ''
 4    for letter in message:
 5        for element1, element2 in plugboard_position:
 6            if letter == element1:
 7                res += element2
 8                break
 9            elif letter == element2:
10                res += element1
11                break
12        else:
13            res += letter
14    return res
15
16
17def rotor(message: str, rotor_position: dict):
18    """Changes every letter from message with its value from dictionary rotor_position"""
19    res = ''
20    for letter in message:
21        res +=  rotor_position.get(letter)
22    return res
23
24
25def enigma_encrypt(plugboard_position: list, rotor_position: dict):
26    """Returns a decorator for any function with single argument str that encrypts its argument with plugboard and rotor"""
27    def decorator(func):
28        def decorated_func(message: str):
29            message = plugboard(message, plugboard_position)
30            message = rotor(message, rotor_position)
31            return func(message)
32        return decorated_func
33    return decorator
34
35
36def enigma_decrypt(plugboard_position: list, rotor_position: dict):
37    """Returns a decorator for any function with single argument str that decrypts its argument with decrypt_rotor and plugboard"""
38    rotor_position = {rotor_position.get(key): key  for key in rotor_position}
39    def decorator(func):
40        def decorated_func(message: str):
41            message = rotor(message, rotor_position)
42            message = plugboard(message, plugboard_position)
43            return func(message)
44        return decorated_func
45    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 41, in decorated_func
message = rotor(message, rotor_position)
File "/tmp/solution.py", line 21, in rotor
res += rotor_position.get(letter)
TypeError: can only concatenate str (not "NoneType") to str

======================================================================
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 41, in decorated_func
message = rotor(message, rotor_position)
File "/tmp/solution.py", line 21, in rotor
res += rotor_position.get(letter)
TypeError: can only concatenate str (not "NoneType") to str

======================================================================
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 decorated_func
message = rotor(message, rotor_position)
File "/tmp/solution.py", line 21, in rotor
res += rotor_position.get(letter)
TypeError: can only concatenate str (not "NoneType") to str

======================================================================
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 21, in rotor
res += rotor_position.get(letter)
TypeError: can only concatenate str (not "NoneType") to str

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

FAILED (errors=4)

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

n1def plugboard(message : str, plugboard_position : list):n1def plugboard(message: str, plugboard_position: list):
2    """Changes every letter from message in a pair from plugboard_position with its pair."""2    """Changes every letter from message in a pair from plugboard_position with its pair."""
3    res = ''3    res = ''
4    for letter in message:4    for letter in message:
n5        changed = Falsen
6        for element1, element2 in plugboard_position:5        for element1, element2 in plugboard_position:
7            if letter == element1:6            if letter == element1:
8                res += element27                res += element2
n9                changed = Truen
10                break8                break
11            elif letter == element2:9            elif letter == element2:
12                res += element110                res += element1
n13                changed = Truen
14                break11                break
n15        if not changed:n12        else:
16            res += letter13            res += letter
17    return res14    return res
1815
1916
n20def rotor(message : str, rotor_position : dict):n17def rotor(message: str, rotor_position: dict):
21    """Changes every letter from message with its value from dictionary rotor_position"""18    """Changes every letter from message with its value from dictionary rotor_position"""
22    res = ''19    res = ''
23    for letter in message:20    for letter in message:
n24        for element1 in rotor_position:n21        res +=  rotor_position.get(letter)
25            if letter == element1:
26                res += rotor_position[element1]
27                break
28    return res22    return res
2923
n30def decrypt_rotor(message : str, rotor_position : dict):n
31    """Changes every letter from message with its key from dictionary rotor_position"""
32    res = ''
33    for letter in message:
34        for element1 in rotor_position:
35            if letter == rotor_position[element1]:
36                res += element1
37                break
38    return res
3924
n40def enigma_encrypt(plugboard_position : list, rotor_position : dict):n25def enigma_encrypt(plugboard_position: list, rotor_position: dict):
41    """Returns a decorator for any function with single argument str that encrypts its argument with plugboard and rotor"""26    """Returns a decorator for any function with single argument str that encrypts its argument with plugboard and rotor"""
42    def decorator(func):27    def decorator(func):
n43        def decorated_func(message : str):n28        def decorated_func(message: str):
44            message = plugboard(message, plugboard_position)29            message = plugboard(message, plugboard_position)
45            message = rotor(message, rotor_position)30            message = rotor(message, rotor_position)
46            return func(message)31            return func(message)
47        return decorated_func32        return decorated_func
48    return decorator33    return decorator
4934
nn35 
50def enigma_decrypt(plugboard_position : list, rotor_position : dict):36def enigma_decrypt(plugboard_position: list, rotor_position: dict):
51    """Returns a decorator for any function with single argument str that decrypts its argument with decrypt_rotor and plugboard"""37    """Returns a decorator for any function with single argument str that decrypts its argument with decrypt_rotor and plugboard"""
nn38    rotor_position = {rotor_position.get(key): key  for key in rotor_position}
52    def decorator(func):39    def decorator(func):
t53        def decorated_func(message : str):t40        def decorated_func(message: str):
54            message = decrypt_rotor(message, rotor_position)41            message = rotor(message, rotor_position)
55            message = plugboard(message, plugboard_position)42            message = plugboard(message, plugboard_position)
56            return func(message)43            return func(message)
57        return decorated_func44        return decorated_func
58    return decorator45    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op