1def plugboard(word, plugboard_position):
2 new_word = ""
3 for letter in word:
4 for element in plugboard_position:
5 if letter in element:
6 new_word += element.difference(letter).pop()
7 break
8 else:
9 new_word += letter
10 return new_word
11
12def rotor(word, rotor_position):
13 new_word = ""
14 for letter in word:
15 new_word += rotor_position[letter]
16 return new_word
17
18def enigma_encrypt(plugboard_position, rotor_position):
19 def wrapper(function):
20 def crypt(word):
21 return function(rotor(plugboard(word, plugboard_position), rotor_position))
22 return crypt
23 return wrapper
24
25def enigma_decrypt(plugboard_position, rotor_position):
26 def wrapper(function):
27 def crypt(word):
28 reversed_rotor_position = {value: key for key, value in rotor_position.items()}
29 return function(plugboard(rotor(word, reversed_rotor_position), plugboard_position))
30 return crypt
31 return wrapper
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 29, in crypt
return function(plugboard(rotor(word, reversed_rotor_position), plugboard_position))
File "/tmp/solution.py", line 15, in rotor
new_word += rotor_position[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 29, in crypt
return function(plugboard(rotor(word, reversed_rotor_position), plugboard_position))
File "/tmp/solution.py", line 15, in rotor
new_word += rotor_position[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 21, in crypt
return function(rotor(plugboard(word, plugboard_position), rotor_position))
File "/tmp/solution.py", line 15, in rotor
new_word += rotor_position[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 15, in rotor
new_word += rotor_position[letter]
KeyError: ' '
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (errors=4)
f | 1 | def plugboard(word, plugboard_position): | f | 1 | def plugboard(word, plugboard_position): |
2 | new_word = "" | 2 | new_word = "" | ||
3 | for letter in word: | 3 | for letter in word: | ||
n | 4 | check = False | n | ||
5 | for element in plugboard_position: | 4 | for element in plugboard_position: | ||
6 | if letter in element: | 5 | if letter in element: | ||
7 | new_word += element.difference(letter).pop() | 6 | new_word += element.difference(letter).pop() | ||
n | 8 | check = True | n | ||
9 | break | 7 | break | ||
n | 10 | if check == False: | n | 8 | else: |
11 | new_word += letter | 9 | new_word += letter | ||
12 | return new_word | 10 | return new_word | ||
13 | 11 | ||||
14 | def rotor(word, rotor_position): | 12 | def rotor(word, rotor_position): | ||
15 | new_word = "" | 13 | new_word = "" | ||
16 | for letter in word: | 14 | for letter in word: | ||
17 | new_word += rotor_position[letter] | 15 | new_word += rotor_position[letter] | ||
18 | return new_word | 16 | return new_word | ||
19 | 17 | ||||
20 | def enigma_encrypt(plugboard_position, rotor_position): | 18 | def enigma_encrypt(plugboard_position, rotor_position): | ||
21 | def wrapper(function): | 19 | def wrapper(function): | ||
22 | def crypt(word): | 20 | def crypt(word): | ||
23 | return function(rotor(plugboard(word, plugboard_position), rotor_position)) | 21 | return function(rotor(plugboard(word, plugboard_position), rotor_position)) | ||
24 | return crypt | 22 | return crypt | ||
25 | return wrapper | 23 | return wrapper | ||
26 | 24 | ||||
27 | def enigma_decrypt(plugboard_position, rotor_position): | 25 | def enigma_decrypt(plugboard_position, rotor_position): | ||
28 | def wrapper(function): | 26 | def wrapper(function): | ||
29 | def crypt(word): | 27 | def crypt(word): | ||
t | 30 | reversed_rotor_position = dict([(value, key) for key, value in rotor_position.items()]) | t | 28 | reversed_rotor_position = {value: key for key, value in rotor_position.items()} |
31 | return function(plugboard(rotor(word, reversed_rotor_position), plugboard_position)) | 29 | return function(plugboard(rotor(word, reversed_rotor_position), plugboard_position)) | ||
32 | return crypt | 30 | return crypt | ||
33 | return wrapper | 31 | return wrapper |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|