1def plugboard(text, plugboard_position):
2 converted_text = ''
3 for char_text in text:
4 for plug_set in plugboard_position:
5 if char_text in plug_set:
6 converted_text += plug_set.difference({char_text}).pop()
7 break
8 else:
9 converted_text += char_text
10 return converted_text
11
12
13def rotor(text, rotor_position):
14 converted_text = ''
15 for char_text in text:
16 converted_text += rotor_position[char_text]
17 return converted_text
18
19
20def enigma_encrypt(plugboard_position, rotor_position):
21 def encrypt_decorator(func):
22 def encrypt_wrapper(text):
23 text = plugboard(text, plugboard_position)
24 text = rotor(text, rotor_position)
25
26 return func(text)
27
28 return encrypt_wrapper
29
30 return encrypt_decorator
31
32
33def enigma_decrypt(plugboard_position, rotor_position):
34 def decrypt_decorator(func):
35 def decrypt_wrapper(text):
36 text = rotor(text, {v: k for k, v in rotor_position.items()})
37 text = plugboard(text, plugboard_position)
38
39 return func(text)
40
41 return decrypt_wrapper
42
43 return decrypt_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 36, in decrypt_wrapper
text = rotor(text, {v: k for k, v in rotor_position.items()})
File "/tmp/solution.py", line 16, in rotor
converted_text += rotor_position[char_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 36, in decrypt_wrapper
text = rotor(text, {v: k for k, v in rotor_position.items()})
File "/tmp/solution.py", line 16, in rotor
converted_text += rotor_position[char_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 24, in encrypt_wrapper
text = rotor(text, rotor_position)
File "/tmp/solution.py", line 16, in rotor
converted_text += rotor_position[char_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 16, in rotor
converted_text += rotor_position[char_text]
KeyError: ' '
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (errors=4)
f | 1 | def plugboard(text, plugboard_position): | f | 1 | def plugboard(text, plugboard_position): |
2 | converted_text = '' | 2 | converted_text = '' | ||
n | 3 | for c in text: | n | 3 | for char_text in text: |
4 | for plug_set in plugboard_position: | 4 | for plug_set in plugboard_position: | ||
n | 5 | if c in plug_set: | n | 5 | if char_text in plug_set: |
6 | converted_text += plug_set.difference({c}).pop() | 6 | converted_text += plug_set.difference({char_text}).pop() | ||
7 | break | 7 | break | ||
8 | else: | 8 | else: | ||
n | 9 | converted_text += c | n | 9 | converted_text += char_text |
10 | return converted_text | 10 | return converted_text | ||
11 | 11 | ||||
12 | 12 | ||||
13 | def rotor(text, rotor_position): | 13 | def rotor(text, rotor_position): | ||
14 | converted_text = '' | 14 | converted_text = '' | ||
n | 15 | for c in text: | n | 15 | for char_text in text: |
16 | converted_text += rotor_position[c] | 16 | converted_text += rotor_position[char_text] | ||
17 | return converted_text | 17 | return converted_text | ||
18 | 18 | ||||
19 | 19 | ||||
20 | def enigma_encrypt(plugboard_position, rotor_position): | 20 | def enigma_encrypt(plugboard_position, rotor_position): | ||
21 | def encrypt_decorator(func): | 21 | def encrypt_decorator(func): | ||
22 | def encrypt_wrapper(text): | 22 | def encrypt_wrapper(text): | ||
23 | text = plugboard(text, plugboard_position) | 23 | text = plugboard(text, plugboard_position) | ||
24 | text = rotor(text, rotor_position) | 24 | text = rotor(text, rotor_position) | ||
25 | 25 | ||||
n | 26 | result = func(text) | n | 26 | return func(text) |
27 | return result | ||||
28 | 27 | ||||
29 | return encrypt_wrapper | 28 | return encrypt_wrapper | ||
30 | 29 | ||||
31 | return encrypt_decorator | 30 | return encrypt_decorator | ||
32 | 31 | ||||
33 | 32 | ||||
34 | def enigma_decrypt(plugboard_position, rotor_position): | 33 | def enigma_decrypt(plugboard_position, rotor_position): | ||
35 | def decrypt_decorator(func): | 34 | def decrypt_decorator(func): | ||
36 | def decrypt_wrapper(text): | 35 | def decrypt_wrapper(text): | ||
37 | text = rotor(text, {v: k for k, v in rotor_position.items()}) | 36 | text = rotor(text, {v: k for k, v in rotor_position.items()}) | ||
38 | text = plugboard(text, plugboard_position) | 37 | text = plugboard(text, plugboard_position) | ||
39 | 38 | ||||
t | 40 | result = func(text) | t | 39 | return func(text) |
41 | return result | ||||
42 | 40 | ||||
43 | return decrypt_wrapper | 41 | return decrypt_wrapper | ||
44 | 42 | ||||
45 | return decrypt_decorator | 43 | return decrypt_decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|