1def plugboard(text, plugboard_position):
2 for pair in plugboard_position:
3 if text in pair:
4 return next(iter(pair - {text}))
5 return text
6
7def rotor(text, rotor_position):
8 return ''.join(rotor_position[char] for char in text)
9
10def enigma_encrypt(plugboard_position, rotor_position):
11 def decorator(func):
12 def wrapper(text):
13 encrypted_text = ''
14 for char in text:
15 char = plugboard(char, plugboard_position)
16 char = rotor(char, rotor_position)
17 encrypted_text += char
18 func(encrypted_text)
19 return wrapper
20 return decorator
21
22def enigma_decrypt(plugboard_position, rotor_position):
23 def decorator(func):
24 def wrapper(text):
25 decrypted_text = ''
26 for char in text:
27 char = rotor(char, {v: k for k, v in rotor_position.items()})
28 char = plugboard(char, plugboard_position)
29 decrypted_text += char
30 func(decrypted_text)
31 return wrapper
32 return decorator
E.EE..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 27, in wrapper
char = rotor(char, {v: k for k, v in rotor_position.items()})
File "/tmp/solution.py", line 8, in rotor
return ''.join(rotor_position[char] for char in text)
File "/tmp/solution.py", line 8, in <genexpr>
return ''.join(rotor_position[char] for char in text)
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 27, in wrapper
char = rotor(char, {v: k for k, v in rotor_position.items()})
File "/tmp/solution.py", line 8, in rotor
return ''.join(rotor_position[char] for char in text)
File "/tmp/solution.py", line 8, in <genexpr>
return ''.join(rotor_position[char] for char in text)
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 16, in wrapper
char = rotor(char, rotor_position)
File "/tmp/solution.py", line 8, in rotor
return ''.join(rotor_position[char] for char in text)
File "/tmp/solution.py", line 8, in <genexpr>
return ''.join(rotor_position[char] for char in text)
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 8, in rotor
return ''.join(rotor_position[char] for char in text)
File "/tmp/solution.py", line 8, in <genexpr>
return ''.join(rotor_position[char] for char in text)
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: 'this is a test input' != 'ihts ts z iysi tnpui'
- this is a test input
+ ihts ts z iysi tnpui
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (failures=1, errors=4)