1def plugboard(text_to_convert, list_of_sets):
2 converted_string = ''
3 for character in text_to_convert:
4 for x, y in list_of_sets:
5 if character == x:
6 character = y
7 break
8 elif character == y:
9 character = x
10 break
11 converted_string += character
12 return converted_string
13
14def rotor(text_to_convert, template_dict):
15 converted_string = ''
16 for character in text_to_convert:
17 character = template_dict.get(character)
18 converted_string += character
19 return converted_string
20
21def enigma_encrypt(plugboard_position, rotor_position):
22 def decorator(function):
23 def encrypt_text(current_string):
24 function(rotor(plugboard(current_string, plugboard_position), rotor_position))
25 return encrypt_text
26 return decorator
27
28def enigma_decrypt(plugboard_position, rotor_position):
29 def decorator(function):
30 def decrypt_text(string_to_decrypt):
31 reversed_dictionary = {value:key for key, value in rotor_position.items()}
32 function(plugboard(rotor(string_to_decrypt, reversed_dictionary), plugboard_position))
33 return decrypt_text
34 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 32, in decrypt_text
function(plugboard(rotor(string_to_decrypt, reversed_dictionary), plugboard_position))
File "/tmp/solution.py", line 18, in rotor
converted_string += character
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 32, in decrypt_text
function(plugboard(rotor(string_to_decrypt, reversed_dictionary), plugboard_position))
File "/tmp/solution.py", line 18, in rotor
converted_string += character
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 24, in encrypt_text
function(rotor(plugboard(current_string, plugboard_position), rotor_position))
File "/tmp/solution.py", line 18, in rotor
converted_string += character
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 18, in rotor
converted_string += character
TypeError: can only concatenate str (not "NoneType") to str
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (errors=4)
f | 1 | def plugboard(text_to_convert, list_of_sets): | f | 1 | def plugboard(text_to_convert, list_of_sets): |
2 | converted_string = '' | 2 | converted_string = '' | ||
3 | for character in text_to_convert: | 3 | for character in text_to_convert: | ||
n | 4 | for (x, y) in list_of_sets: | n | 4 | for x, y in list_of_sets: |
5 | if character == x: | 5 | if character == x: | ||
6 | character = y | 6 | character = y | ||
7 | break | 7 | break | ||
8 | elif character == y: | 8 | elif character == y: | ||
9 | character = x | 9 | character = x | ||
10 | break | 10 | break | ||
11 | converted_string += character | 11 | converted_string += character | ||
12 | return converted_string | 12 | return converted_string | ||
13 | 13 | ||||
14 | def rotor(text_to_convert, template_dict): | 14 | def rotor(text_to_convert, template_dict): | ||
15 | converted_string = '' | 15 | converted_string = '' | ||
16 | for character in text_to_convert: | 16 | for character in text_to_convert: | ||
n | 17 | for key, value in template_dict.items(): | n | 17 | character = template_dict.get(character) |
18 | if character == key: | ||||
19 | character = value | ||||
20 | break | ||||
21 | converted_string += character | 18 | converted_string += character | ||
22 | return converted_string | 19 | return converted_string | ||
23 | 20 | ||||
24 | def enigma_encrypt(plugboard_position, rotor_position): | 21 | def enigma_encrypt(plugboard_position, rotor_position): | ||
25 | def decorator(function): | 22 | def decorator(function): | ||
26 | def encrypt_text(current_string): | 23 | def encrypt_text(current_string): | ||
27 | function(rotor(plugboard(current_string, plugboard_position), rotor_position)) | 24 | function(rotor(plugboard(current_string, plugboard_position), rotor_position)) | ||
28 | return encrypt_text | 25 | return encrypt_text | ||
29 | return decorator | 26 | return decorator | ||
30 | 27 | ||||
31 | def enigma_decrypt(plugboard_position, rotor_position): | 28 | def enigma_decrypt(plugboard_position, rotor_position): | ||
32 | def decorator(function): | 29 | def decorator(function): | ||
33 | def decrypt_text(string_to_decrypt): | 30 | def decrypt_text(string_to_decrypt): | ||
t | 34 | reversed_dictionary = {value:key for key,value in rotor_position.items()} | t | 31 | reversed_dictionary = {value:key for key, value in rotor_position.items()} |
35 | function(plugboard(rotor(string_to_decrypt, reversed_dictionary), plugboard_position)) | 32 | function(plugboard(rotor(string_to_decrypt, reversed_dictionary), plugboard_position)) | ||
36 | return decrypt_text | 33 | return decrypt_text | ||
37 | return decorator | 34 | return decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|