1def plugboard(input_str, list_input):
2 result = ''
3 for character in input_str:
4 set_found = find_set_by_key(list_input, character)
5
6 if set_found is not None:
7 result = result + set_found[1]
8 else:
9 result = result + character
10
11 return result
12
13def find_set_by_key(input_set, key_to_find):
14
15 for set_item in input_set:
16 my_list = list(set_item)
17
18 if key_to_find == my_list[0]:
19 return my_list
20
21 return None
22
23def rotor(input_str, dict_input):
24 result = ''
25 for character in input_str:
26 value_to_replace = dict_input.get(character)
27 result = result + value_to_replace
28
29 return result
30
31def enigma_encrypt(plugboard_position, rotor_position):
32 def encrypt_decorator(func):
33 def wrapper(text):
34 text = plugboard(text, plugboard_position)
35 text = rotor(text, rotor_position)
36 func(text)
37 return wrapper
38 return encrypt_decorator
39
40def enigma_decrypt(plugboard_position, rotor_position):
41 def decrypt_decorator(func):
42 def wrapper(text):
43 flipped_dict = {value: key for key, value in rotor_position.items()}
44
45 text = rotor(text, flipped_dict)
46 text = plugboard(text, plugboard_position)
47 func(text)
48 return wrapper
49 return decrypt_decorator
EFEE..F.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 45, in wrapper
text = rotor(text, flipped_dict)
File "/tmp/solution.py", line 27, in rotor
result = result + value_to_replace
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 45, in wrapper
text = rotor(text, flipped_dict)
File "/tmp/solution.py", line 27, in rotor
result = result + value_to_replace
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 35, in wrapper
text = rotor(text, rotor_position)
File "/tmp/solution.py", line 27, in rotor
result = result + value_to_replace
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 27, in rotor
result = result + value_to_replace
TypeError: can only concatenate str (not "NoneType") to str
======================================================================
FAIL: test_correct_decorator_order (test.TestDecorators)
Test whether the decorator is applying the functions in correct order.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 166, in test_correct_decorator_order
mock.assert_called_once_with('bumb')
File "/usr/lib/python3.10/unittest/mock.py", line 941, in assert_called_once_with
return self.assert_called_with(*args, **kwargs)
File "/usr/lib/python3.10/unittest/mock.py", line 929, in assert_called_with
raise AssertionError(_error_message()) from cause
AssertionError: expected call not found.
Expected: mock('bumb')
Actual: mock('bubb')
======================================================================
FAIL: test_normal_case (test.TestPlugboard)
Test the plugboard function with normally expected input.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 45, in test_normal_case
self.assertEqual(plugboard('this is a test input', plugboard_position), 'ihts ts z iysi tnpui')
AssertionError: 'thts ts z tyst tnput' != 'ihts ts z iysi tnpui'
- thts ts z tyst tnput
? ^ ^ ^ ^
+ ihts ts z iysi tnpui
? ^ ^ ^ ^
----------------------------------------------------------------------
Ran 9 tests in 0.002s
FAILED (failures=2, errors=4)
f | 1 | def plugboard(input_str, list_input): | f | 1 | def plugboard(input_str, list_input): |
n | 2 | n | |||
3 | result = '' | 2 | result = '' | ||
4 | for character in input_str: | 3 | for character in input_str: | ||
5 | set_found = find_set_by_key(list_input, character) | 4 | set_found = find_set_by_key(list_input, character) | ||
6 | 5 | ||||
n | 7 | if set_found != None: | n | 6 | if set_found is not None: |
8 | value = set_found[1] | ||||
9 | result = result+value | 7 | result = result + set_found[1] | ||
10 | else: | 8 | else: | ||
n | 11 | result = result+character | n | 9 | result = result + character |
12 | 10 | ||||
13 | return result | 11 | return result | ||
14 | 12 | ||||
15 | def find_set_by_key(input_set, key_to_find): | 13 | def find_set_by_key(input_set, key_to_find): | ||
16 | 14 | ||||
17 | for set_item in input_set: | 15 | for set_item in input_set: | ||
18 | my_list = list(set_item) | 16 | my_list = list(set_item) | ||
n | 19 | n | |||
20 | key = my_list[0] | ||||
21 | 17 | ||||
n | 22 | if(key_to_find == key): | n | 18 | if key_to_find == my_list[0]: |
23 | return my_list | 19 | return my_list | ||
24 | 20 | ||||
25 | return None | 21 | return None | ||
26 | 22 | ||||
27 | def rotor(input_str, dict_input): | 23 | def rotor(input_str, dict_input): | ||
28 | result = '' | 24 | result = '' | ||
29 | for character in input_str: | 25 | for character in input_str: | ||
30 | value_to_replace = dict_input.get(character) | 26 | value_to_replace = dict_input.get(character) | ||
31 | result = result + value_to_replace | 27 | result = result + value_to_replace | ||
32 | 28 | ||||
33 | return result | 29 | return result | ||
34 | 30 | ||||
35 | def enigma_encrypt(plugboard_position, rotor_position): | 31 | def enigma_encrypt(plugboard_position, rotor_position): | ||
36 | def encrypt_decorator(func): | 32 | def encrypt_decorator(func): | ||
37 | def wrapper(text): | 33 | def wrapper(text): | ||
38 | text = plugboard(text, plugboard_position) | 34 | text = plugboard(text, plugboard_position) | ||
39 | text = rotor(text, rotor_position) | 35 | text = rotor(text, rotor_position) | ||
40 | func(text) | 36 | func(text) | ||
41 | return wrapper | 37 | return wrapper | ||
42 | return encrypt_decorator | 38 | return encrypt_decorator | ||
43 | 39 | ||||
44 | def enigma_decrypt(plugboard_position, rotor_position): | 40 | def enigma_decrypt(plugboard_position, rotor_position): | ||
45 | def decrypt_decorator(func): | 41 | def decrypt_decorator(func): | ||
46 | def wrapper(text): | 42 | def wrapper(text): | ||
n | 47 | text = rotor(text, rotor_position) | n | 43 | flipped_dict = {value: key for key, value in rotor_position.items()} |
44 | |||||
45 | text = rotor(text, flipped_dict) | ||||
48 | text = plugboard(text, plugboard_position) | 46 | text = plugboard(text, plugboard_position) | ||
49 | func(text) | 47 | func(text) | ||
50 | return wrapper | 48 | return wrapper | ||
51 | return decrypt_decorator | 49 | return decrypt_decorator | ||
52 | 50 | ||||
t | 53 | t |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|