1def plugboard(str_to_convert, list_with_sets):
2 list_of_lists = [[first, second] for [first, second] in list_with_sets]
3 list_of_str = list(str_to_convert)
4 for i, character in enumerate(list_of_str):
5 for pair_in_list in list_of_lists:
6 if character == pair_in_list[0]:
7 list_of_str[i] = pair_in_list[1]
8 elif character == pair_in_list[1]:
9 list_of_str[i] = pair_in_list[0]
10
11 return ''.join(list_of_str)
12
13
14def rotor(str_to_convert, dict_for_convertion):
15 list_of_str = list(str_to_convert)
16 for i,character in enumerate(list_of_str):
17 list_of_str[i] = dict_for_convertion[character]
18
19 return ''.join(list_of_str)
20
21
22def enigma_encrypt(plugboard_position=[], rotor_position={}):
23 def decorator(func):
24 def encrypt(str_to_encrypt):
25 str_to_encrypt = plugboard(str_to_encrypt, plugboard_position)
26 str_to_encrypt = rotor(str_to_encrypt, rotor_position)
27 return func(str_to_encrypt)
28 return encrypt
29 return decorator
30
31
32def enigma_decrypt(plugboard_position=[], rotor_position={}):
33 def decorator(func):
34 def decrypt(str_to_decrypt):
35 new_rotor_position = dict((value, key) for key, value in rotor_position.items())
36 str_to_decrypt = rotor(str_to_decrypt, new_rotor_position)
37 str_to_decrypt = plugboard(str_to_decrypt, plugboard_position)
38 return func(str_to_decrypt)
39 return decrypt
40 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 36, in decrypt
str_to_decrypt = rotor(str_to_decrypt, new_rotor_position)
File "/tmp/solution.py", line 17, in rotor
list_of_str[i] = dict_for_convertion[character]
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
str_to_decrypt = rotor(str_to_decrypt, new_rotor_position)
File "/tmp/solution.py", line 17, in rotor
list_of_str[i] = dict_for_convertion[character]
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 26, in encrypt
str_to_encrypt = rotor(str_to_encrypt, rotor_position)
File "/tmp/solution.py", line 17, in rotor
list_of_str[i] = dict_for_convertion[character]
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 17, in rotor
list_of_str[i] = dict_for_convertion[character]
KeyError: ' '
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (errors=4)
f | 1 | def plugboard(str_to_convert, list_with_sets): | f | 1 | def plugboard(str_to_convert, list_with_sets): |
2 | list_of_lists = [[first, second] for [first, second] in list_with_sets] | 2 | list_of_lists = [[first, second] for [first, second] in list_with_sets] | ||
3 | list_of_str = list(str_to_convert) | 3 | list_of_str = list(str_to_convert) | ||
n | 4 | for i,character in enumerate(list_of_str): | n | 4 | for i, character in enumerate(list_of_str): |
5 | for pair_in_list in list_of_lists: | 5 | for pair_in_list in list_of_lists: | ||
6 | if character == pair_in_list[0]: | 6 | if character == pair_in_list[0]: | ||
7 | list_of_str[i] = pair_in_list[1] | 7 | list_of_str[i] = pair_in_list[1] | ||
8 | elif character == pair_in_list[1]: | 8 | elif character == pair_in_list[1]: | ||
9 | list_of_str[i] = pair_in_list[0] | 9 | list_of_str[i] = pair_in_list[0] | ||
10 | 10 | ||||
11 | return ''.join(list_of_str) | 11 | return ''.join(list_of_str) | ||
12 | 12 | ||||
13 | 13 | ||||
14 | def rotor(str_to_convert, dict_for_convertion): | 14 | def rotor(str_to_convert, dict_for_convertion): | ||
15 | list_of_str = list(str_to_convert) | 15 | list_of_str = list(str_to_convert) | ||
16 | for i,character in enumerate(list_of_str): | 16 | for i,character in enumerate(list_of_str): | ||
17 | list_of_str[i] = dict_for_convertion[character] | 17 | list_of_str[i] = dict_for_convertion[character] | ||
18 | 18 | ||||
19 | return ''.join(list_of_str) | 19 | return ''.join(list_of_str) | ||
20 | 20 | ||||
21 | 21 | ||||
n | 22 | def enigma_encrypt(plugboard_position = [], rotor_position = {}): | n | 22 | def enigma_encrypt(plugboard_position=[], rotor_position={}): |
23 | def decorator(func): | 23 | def decorator(func): | ||
24 | def encrypt(str_to_encrypt): | 24 | def encrypt(str_to_encrypt): | ||
25 | str_to_encrypt = plugboard(str_to_encrypt, plugboard_position) | 25 | str_to_encrypt = plugboard(str_to_encrypt, plugboard_position) | ||
26 | str_to_encrypt = rotor(str_to_encrypt, rotor_position) | 26 | str_to_encrypt = rotor(str_to_encrypt, rotor_position) | ||
27 | return func(str_to_encrypt) | 27 | return func(str_to_encrypt) | ||
28 | return encrypt | 28 | return encrypt | ||
29 | return decorator | 29 | return decorator | ||
30 | 30 | ||||
31 | 31 | ||||
n | 32 | def enigma_decrypt(plugboard_position = [], rotor_position = {}): | n | 32 | def enigma_decrypt(plugboard_position=[], rotor_position={}): |
33 | def decorator(func): | 33 | def decorator(func): | ||
34 | def decrypt(str_to_decrypt): | 34 | def decrypt(str_to_decrypt): | ||
35 | new_rotor_position = dict((value, key) for key, value in rotor_position.items()) | 35 | new_rotor_position = dict((value, key) for key, value in rotor_position.items()) | ||
t | 36 | str_to_decrypt = rotor(str_to_decrypt,new_rotor_position) | t | 36 | str_to_decrypt = rotor(str_to_decrypt, new_rotor_position) |
37 | str_to_decrypt = plugboard(str_to_decrypt, plugboard_position) | 37 | str_to_decrypt = plugboard(str_to_decrypt, plugboard_position) | ||
38 | return func(str_to_decrypt) | 38 | return func(str_to_decrypt) | ||
39 | return decrypt | 39 | return decrypt | ||
40 | return decorator | 40 | return decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|