1def plugboard_for_letter(letter, plugboard_position):
2 for pair in plugboard_position:
3 if letter in pair:
4 for pair_item in pair:
5 if pair_item != letter:
6 return pair_item
7 return letter
8
9
10def plugboard(string, plugboard_position):
11 return "".join([plugboard_for_letter(l, plugboard_position) for l in string])
12
13
14def rotor(string, rotor_position):
15 return "".join([rotor_position[letter] for letter in string])
16
17
18def enigma_encrypt(plugboard_position, rotor_position):
19 def decorator(function):
20 def encrypted_function(string):
21 return function(rotor(plugboard(string, plugboard_position), rotor_position))
22 return encrypted_function
23 return decorator
24
25
26def rotor_decryptor_letter(letter, rotor_position):
27 for key, value in rotor_position.items():
28 if value == letter:
29 return key
30
31
32def rotor_decryptor(string, rotor_position):
33 return "".join([rotor_decryptor_letter(l, rotor_position) for l in string])
34
35
36def enigma_decrypt(plugboard_position, rotor_position):
37 def decorator(function):
38 def decrypted_function(string):
39 return function(plugboard(rotor_decryptor(string, rotor_position), plugboard_position))
40 return decrypted_function
41 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 39, in decrypted_function
return function(plugboard(rotor_decryptor(string, rotor_position), plugboard_position))
File "/tmp/solution.py", line 33, in rotor_decryptor
return "".join([rotor_decryptor_letter(l, rotor_position) for l in string])
TypeError: sequence item 1: expected str instance, NoneType found
======================================================================
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 39, in decrypted_function
return function(plugboard(rotor_decryptor(string, rotor_position), plugboard_position))
File "/tmp/solution.py", line 33, in rotor_decryptor
return "".join([rotor_decryptor_letter(l, rotor_position) for l in string])
TypeError: sequence item 3: expected str instance, NoneType found
======================================================================
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 21, in encrypted_function
return function(rotor(plugboard(string, plugboard_position), rotor_position))
File "/tmp/solution.py", line 15, in rotor
return "".join([rotor_position[letter] for letter in string])
File "/tmp/solution.py", line 15, in <listcomp>
return "".join([rotor_position[letter] for letter in string])
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 15, in rotor
return "".join([rotor_position[letter] for letter in string])
File "/tmp/solution.py", line 15, in <listcomp>
return "".join([rotor_position[letter] for letter in string])
KeyError: ' '
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (errors=4)
f | 1 | def plugboard_for_letter(letter, plugboard_position): | f | 1 | def plugboard_for_letter(letter, plugboard_position): |
2 | for pair in plugboard_position: | 2 | for pair in plugboard_position: | ||
3 | if letter in pair: | 3 | if letter in pair: | ||
4 | for pair_item in pair: | 4 | for pair_item in pair: | ||
5 | if pair_item != letter: | 5 | if pair_item != letter: | ||
6 | return pair_item | 6 | return pair_item | ||
7 | return letter | 7 | return letter | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def plugboard(string, plugboard_position): | 10 | def plugboard(string, plugboard_position): | ||
11 | return "".join([plugboard_for_letter(l, plugboard_position) for l in string]) | 11 | return "".join([plugboard_for_letter(l, plugboard_position) for l in string]) | ||
12 | 12 | ||||
13 | 13 | ||||
14 | def rotor(string, rotor_position): | 14 | def rotor(string, rotor_position): | ||
15 | return "".join([rotor_position[letter] for letter in string]) | 15 | return "".join([rotor_position[letter] for letter in string]) | ||
16 | 16 | ||||
17 | 17 | ||||
18 | def enigma_encrypt(plugboard_position, rotor_position): | 18 | def enigma_encrypt(plugboard_position, rotor_position): | ||
19 | def decorator(function): | 19 | def decorator(function): | ||
20 | def encrypted_function(string): | 20 | def encrypted_function(string): | ||
21 | return function(rotor(plugboard(string, plugboard_position), rotor_position)) | 21 | return function(rotor(plugboard(string, plugboard_position), rotor_position)) | ||
22 | return encrypted_function | 22 | return encrypted_function | ||
23 | return decorator | 23 | return decorator | ||
t | t | 24 | |||
25 | |||||
26 | def rotor_decryptor_letter(letter, rotor_position): | ||||
27 | for key, value in rotor_position.items(): | ||||
28 | if value == letter: | ||||
29 | return key | ||||
30 | |||||
31 | |||||
32 | def rotor_decryptor(string, rotor_position): | ||||
33 | return "".join([rotor_decryptor_letter(l, rotor_position) for l in string]) | ||||
34 | |||||
35 | |||||
36 | def enigma_decrypt(plugboard_position, rotor_position): | ||||
37 | def decorator(function): | ||||
38 | def decrypted_function(string): | ||||
39 | return function(plugboard(rotor_decryptor(string, rotor_position), plugboard_position)) | ||||
40 | return decrypted_function | ||||
41 | return decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
31.10.2023 15:23