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)
n | 1 | def plugboard(message : str, plugboard_position : list): | n | 1 | def 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: | ||
n | 5 | changed = False | n | ||
6 | for element1, element2 in plugboard_position: | 5 | for element1, element2 in plugboard_position: | ||
7 | if letter == element1: | 6 | if letter == element1: | ||
8 | res += element2 | 7 | res += element2 | ||
n | 9 | changed = True | n | ||
10 | break | 8 | break | ||
11 | elif letter == element2: | 9 | elif letter == element2: | ||
12 | res += element1 | 10 | res += element1 | ||
n | 13 | changed = True | n | ||
14 | break | 11 | break | ||
n | 15 | if not changed: | n | 12 | else: |
16 | res += letter | 13 | res += letter | ||
17 | return res | 14 | return res | ||
18 | 15 | ||||
19 | 16 | ||||
n | 20 | def rotor(message : str, rotor_position : dict): | n | 17 | def 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: | ||
n | 24 | for element1 in rotor_position: | n | 21 | res += rotor_position.get(letter) |
25 | if letter == element1: | ||||
26 | res += rotor_position[element1] | ||||
27 | break | ||||
28 | return res | 22 | return res | ||
29 | 23 | ||||
n | 30 | def 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 | ||||
39 | 24 | ||||
n | 40 | def enigma_encrypt(plugboard_position : list, rotor_position : dict): | n | 25 | def 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): | ||
n | 43 | def decorated_func(message : str): | n | 28 | 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_func | 32 | return decorated_func | ||
48 | return decorator | 33 | return decorator | ||
49 | 34 | ||||
n | n | 35 | |||
50 | def enigma_decrypt(plugboard_position : list, rotor_position : dict): | 36 | def 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""" | ||
n | n | 38 | rotor_position = {rotor_position.get(key): key for key in rotor_position} | ||
52 | def decorator(func): | 39 | def decorator(func): | ||
t | 53 | def decorated_func(message : str): | t | 40 | 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_func | 44 | return decorated_func | ||
58 | return decorator | 45 | return decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|