1def rotor(my_str, rotor_position):
2 for ch in my_str:
3 my_str = my_str.replace(ch, rotor_position[ch])
4 return my_str
5
6
7def plugboard(my_str, plugboard_position):
8 positions = {}
9 for curr_set in plugboard_position:
10 positions[min(curr_set)] = max(curr_set)
11 positions[max(curr_set)] = min(curr_set)
12 for ch in my_str:
13 if ch in positions:
14 my_str = my_str.replace(ch, positions[ch])
15 return my_str
16
17
18def enigma_encrypt(plugboard_position, rotor_position):
19 def enigma_encrypt_decorator(my_str):
20 my_str = plugboard(my_str, plugboard_position)
21 my_str = rotor(my_str, rotor_position)
22 return my_str
23 return enigma_encrypt_decorator
24
25
26def enigma_decrypt(plugboard_position, rotor_position):
27 rotor_pos_new = {rotor_position[key]:key for key in rotor_position.keys()}
28 def enigma_decrypt_decorator(my_str):
29 my_str = rotor(my_str, rotor_pos_new)
30 my_str = plugboard(my_str, plugboard_position)
31 return my_str
32 return enigma_decrypt_decorator
EEEE..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 138, in test_full_letter_set
combined = decryptor(encryptor(TEST_FUN))
File "/tmp/solution.py", line 20, in enigma_encrypt_decorator
my_str = plugboard(my_str, plugboard_position)
File "/tmp/solution.py", line 12, in plugboard
for ch in my_str:
TypeError: 'function' object is not iterable
======================================================================
ERROR: 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 165, in test_correct_decorator_order
encryptor(mock)('test')
File "/tmp/solution.py", line 20, in enigma_encrypt_decorator
my_str = plugboard(my_str, plugboard_position)
File "/tmp/solution.py", line 12, in plugboard
for ch in my_str:
TypeError: 'Mock' object is not iterable
======================================================================
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 111, in test_full_letter_set
decrypted = decryptor(TEST_FUN)
File "/tmp/solution.py", line 29, in enigma_decrypt_decorator
my_str = rotor(my_str, rotor_pos_new)
File "/tmp/solution.py", line 2, in rotor
for ch in my_str:
TypeError: 'function' object is not iterable
======================================================================
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 88, in test_full_letter_set
encrypted = encryptor(TEST_FUN)
File "/tmp/solution.py", line 20, in enigma_encrypt_decorator
my_str = plugboard(my_str, plugboard_position)
File "/tmp/solution.py", line 12, in plugboard
for ch in my_str:
TypeError: 'function' object is not iterable
======================================================================
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 3, in rotor
my_str = my_str.replace(ch, rotor_position[ch])
KeyError: ' '
======================================================================
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: 'ihis is z iysi inpui' != 'ihts ts z iysi tnpui'
- ihis is z iysi inpui
? ^ ^ ^
+ ihts ts z iysi tnpui
? ^ ^ ^
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (failures=1, errors=5)
n | 1 | def rotor(str, rotor_position): | n | 1 | def rotor(my_str, rotor_position): |
2 | for ch in str: | 2 | for ch in my_str: | ||
3 | str = str.replace(ch, rotor_position[ch]) | 3 | my_str = my_str.replace(ch, rotor_position[ch]) | ||
4 | return str | 4 | return my_str | ||
5 | |||||
6 | |||||
5 | def plugboard(str, plugboard_position): | 7 | def plugboard(my_str, plugboard_position): | ||
6 | positions = {} | 8 | positions = {} | ||
n | 7 | for set in plugboard_position: | n | 9 | for curr_set in plugboard_position: |
8 | positions[min(set)] = max(set) | 10 | positions[min(curr_set)] = max(curr_set) | ||
9 | positions[max(set)] = min(set) | 11 | positions[max(curr_set)] = min(curr_set) | ||
10 | for ch in str: | 12 | for ch in my_str: | ||
11 | if ch in positions: | 13 | if ch in positions: | ||
n | 12 | str = str.replace(ch, positions[ch]) | n | 14 | my_str = my_str.replace(ch, positions[ch]) |
13 | return str | 15 | return my_str | ||
16 | |||||
17 | |||||
14 | def enigma_encrypt(plugboard_position, rotor_position): | 18 | def enigma_encrypt(plugboard_position, rotor_position): | ||
n | 15 | def enigma_encrypt_decorator(str): | n | 19 | def enigma_encrypt_decorator(my_str): |
16 | str = plugboard(str, plugboard_position) | 20 | my_str = plugboard(my_str, plugboard_position) | ||
17 | str = rotor(str, rotor_position) | 21 | my_str = rotor(my_str, rotor_position) | ||
18 | return str | 22 | return my_str | ||
19 | return enigma_encrypt_decorator | 23 | return enigma_encrypt_decorator | ||
n | n | 24 | |||
25 | |||||
20 | def enigma_decrypt(plugboard_position, rotor_position): | 26 | def enigma_decrypt(plugboard_position, rotor_position): | ||
t | 21 | rotor_pos_new = {} | t | 27 | rotor_pos_new = {rotor_position[key]:key for key in rotor_position.keys()} |
22 | for key in rotor_position: | ||||
23 | rotor_pos_new[rotor_position[key]] = key | ||||
24 | def enigma_decrypt_decorator(str): | 28 | def enigma_decrypt_decorator(my_str): | ||
25 | str = rotor(str, rotor_pos_new) | 29 | my_str = rotor(my_str, rotor_pos_new) | ||
26 | str = plugboard(str, plugboard_position) | 30 | my_str = plugboard(my_str, plugboard_position) | ||
27 | return str | 31 | return my_str | ||
28 | return enigma_decrypt_decorator | 32 | return enigma_decrypt_decorator | ||
29 | 33 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|