1def plugboard(text, set):
2 result = ""
3 dictionary = {' ':' '}
4
5 for i in set:
6 i = list(i)
7 dictionary[i[0]] = i[1]
8 dictionary[i[1]] = i[0]
9
10 for letter in text:
11 result += dictionary.get(letter, letter)
12
13 return result
14
15def rotor(text, dict):
16 result = ""
17
18 for letter in text:
19 result += dict[letter]
20
21 return result
22
23
24def enigma_encrypt(plugboard_position, rotor_position):
25 def decorator(function):
26 def wrapper(text):
27 text = rotor(plugboard(text, plugboard_position), rotor_position)
28
29 return function(text)
30
31 return wrapper
32
33 return decorator
34
35
36
37def enigma_decrypt(plugboard_position, rotor_position):
38 def decorator(function):
39 def wrapper(text):
40 def reverse_rotor(dictionary):
41 result = dict()
42 for key in dictionary.keys():
43 result[dictionary[key]] = key
44
45 return result
46 text = plugboard(rotor(text, reverse_rotor(rotor_position)), plugboard_position)
47 return function(text)
48 return wrapper
49 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 46, in wrapper
text = plugboard(rotor(text, reverse_rotor(rotor_position)), plugboard_position)
File "/tmp/solution.py", line 19, in rotor
result += dict[letter]
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 46, in wrapper
text = plugboard(rotor(text, reverse_rotor(rotor_position)), plugboard_position)
File "/tmp/solution.py", line 19, in rotor
result += dict[letter]
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 27, in wrapper
text = rotor(plugboard(text, plugboard_position), rotor_position)
File "/tmp/solution.py", line 19, in rotor
result += dict[letter]
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 19, in rotor
result += dict[letter]
KeyError: ' '
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (errors=4)
f | 1 | def plugboard(text, set): | f | 1 | def plugboard(text, set): |
2 | result = "" | 2 | result = "" | ||
3 | dictionary = {' ':' '} | 3 | dictionary = {' ':' '} | ||
4 | 4 | ||||
5 | for i in set: | 5 | for i in set: | ||
6 | i = list(i) | 6 | i = list(i) | ||
7 | dictionary[i[0]] = i[1] | 7 | dictionary[i[0]] = i[1] | ||
8 | dictionary[i[1]] = i[0] | 8 | dictionary[i[1]] = i[0] | ||
9 | 9 | ||||
10 | for letter in text: | 10 | for letter in text: | ||
11 | result += dictionary.get(letter, letter) | 11 | result += dictionary.get(letter, letter) | ||
12 | 12 | ||||
13 | return result | 13 | return result | ||
14 | 14 | ||||
15 | def rotor(text, dict): | 15 | def rotor(text, dict): | ||
16 | result = "" | 16 | result = "" | ||
17 | 17 | ||||
18 | for letter in text: | 18 | for letter in text: | ||
19 | result += dict[letter] | 19 | result += dict[letter] | ||
20 | 20 | ||||
21 | return result | 21 | return result | ||
22 | 22 | ||||
23 | 23 | ||||
24 | def enigma_encrypt(plugboard_position, rotor_position): | 24 | def enigma_encrypt(plugboard_position, rotor_position): | ||
25 | def decorator(function): | 25 | def decorator(function): | ||
26 | def wrapper(text): | 26 | def wrapper(text): | ||
27 | text = rotor(plugboard(text, plugboard_position), rotor_position) | 27 | text = rotor(plugboard(text, plugboard_position), rotor_position) | ||
28 | 28 | ||||
29 | return function(text) | 29 | return function(text) | ||
30 | 30 | ||||
31 | return wrapper | 31 | return wrapper | ||
32 | 32 | ||||
33 | return decorator | 33 | return decorator | ||
34 | 34 | ||||
n | 35 | def reverse_rotor(dictionary): | n | ||
36 | result = dict() | ||||
37 | for key in dictionary.keys(): | ||||
38 | result[dictionary[key]] = key | ||||
39 | 35 | ||||
n | 40 | return result | n | ||
41 | 36 | ||||
42 | def enigma_decrypt(plugboard_position, rotor_position): | 37 | def enigma_decrypt(plugboard_position, rotor_position): | ||
43 | def decorator(function): | 38 | def decorator(function): | ||
44 | def wrapper(text): | 39 | def wrapper(text): | ||
t | t | 40 | def reverse_rotor(dictionary): | ||
41 | result = dict() | ||||
42 | for key in dictionary.keys(): | ||||
43 | result[dictionary[key]] = key | ||||
44 | |||||
45 | return result | ||||
45 | text = plugboard(rotor(text, reverse_rotor(rotor_position)), plugboard_position) | 46 | text = plugboard(rotor(text, reverse_rotor(rotor_position)), plugboard_position) | ||
46 | return function(text) | 47 | return function(text) | ||
47 | return wrapper | 48 | return wrapper | ||
48 | return decorator | 49 | return decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
01.11.2023 12:56
01.11.2023 12:56
01.11.2023 12:55
01.11.2023 12:58
01.11.2023 12:58