1def plugboard(msg: str, plugboard_position: list):
2 swaps = {}
3
4 for entry in plugboard_position:
5 pair = list(entry)
6 swaps[pair[0]] = pair[1]
7 swaps[pair[1]] = pair[0]
8
9 edited_msg = ''
10 for ch in msg:
11 letter = swaps.get(ch)
12 edited_msg += ch if letter is None else letter
13
14 return edited_msg
15
16def rotor(msg: str, rotor_position: dict):
17 return ''.join(rotor_position[ch] for ch in msg)
18
19def enigma_encrypt(plugboard_position: list, rotor_position: dict):
20 def decorator (func):
21 def encrypt_message(msg):
22 msg = plugboard(msg, plugboard_position)
23 msg = rotor(msg, rotor_position)
24 return func(msg)
25 return encrypt_message
26 return decorator
27
28def enigma_decrypt(plugboard_position: list, rotor_position: dict):
29 def decorator (func):
30 def decrypt_message(msg):
31 rev_rotor_position = {}
32 for entry in rotor_position:
33 rev_rotor_position[rotor_position[entry]] = entry
34
35 msg = rotor(msg, rev_rotor_position)
36 msg = plugboard(msg, plugboard_position)
37 return func(msg)
38 return decrypt_message
39 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 35, in decrypt_message
msg = rotor(msg, rev_rotor_position)
File "/tmp/solution.py", line 17, in rotor
return ''.join(rotor_position[ch] for ch in msg)
File "/tmp/solution.py", line 17, in <genexpr>
return ''.join(rotor_position[ch] for ch in msg)
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 35, in decrypt_message
msg = rotor(msg, rev_rotor_position)
File "/tmp/solution.py", line 17, in rotor
return ''.join(rotor_position[ch] for ch in msg)
File "/tmp/solution.py", line 17, in <genexpr>
return ''.join(rotor_position[ch] for ch in msg)
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 23, in encrypt_message
msg = rotor(msg, rotor_position)
File "/tmp/solution.py", line 17, in rotor
return ''.join(rotor_position[ch] for ch in msg)
File "/tmp/solution.py", line 17, in <genexpr>
return ''.join(rotor_position[ch] for ch in msg)
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
return ''.join(rotor_position[ch] for ch in msg)
File "/tmp/solution.py", line 17, in <genexpr>
return ''.join(rotor_position[ch] for ch in msg)
KeyError: ' '
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (errors=4)
| f | 1 | def plugboard(msg: str, plugboard_position: list): | f | 1 | def plugboard(msg: str, plugboard_position: list): |
| 2 | swaps = {} | 2 | swaps = {} | ||
| 3 | 3 | ||||
| 4 | for entry in plugboard_position: | 4 | for entry in plugboard_position: | ||
| 5 | pair = list(entry) | 5 | pair = list(entry) | ||
| 6 | swaps[pair[0]] = pair[1] | 6 | swaps[pair[0]] = pair[1] | ||
| 7 | swaps[pair[1]] = pair[0] | 7 | swaps[pair[1]] = pair[0] | ||
| 8 | 8 | ||||
| n | 9 | alpha = [chr(i) for i in range(ord('a'), ord('z')+1)] | n | 9 | edited_msg = '' |
| 10 | for ch in alpha: | 10 | for ch in msg: | ||
| 11 | if ch not in swaps: | 11 | letter = swaps.get(ch) | ||
| 12 | swaps[ch] = ch | 12 | edited_msg += ch if letter is None else letter | ||
| 13 | 13 | ||||
| t | 14 | return ''.join(swaps[ch] for ch in msg) | t | 14 | return edited_msg |
| 15 | 15 | ||||
| 16 | def rotor(msg: str, rotor_position: dict): | 16 | def rotor(msg: str, rotor_position: dict): | ||
| 17 | return ''.join(rotor_position[ch] for ch in msg) | 17 | return ''.join(rotor_position[ch] for ch in msg) | ||
| 18 | 18 | ||||
| 19 | def enigma_encrypt(plugboard_position: list, rotor_position: dict): | 19 | def enigma_encrypt(plugboard_position: list, rotor_position: dict): | ||
| 20 | def decorator (func): | 20 | def decorator (func): | ||
| 21 | def encrypt_message(msg): | 21 | def encrypt_message(msg): | ||
| 22 | msg = plugboard(msg, plugboard_position) | 22 | msg = plugboard(msg, plugboard_position) | ||
| 23 | msg = rotor(msg, rotor_position) | 23 | msg = rotor(msg, rotor_position) | ||
| 24 | return func(msg) | 24 | return func(msg) | ||
| 25 | return encrypt_message | 25 | return encrypt_message | ||
| 26 | return decorator | 26 | return decorator | ||
| 27 | 27 | ||||
| 28 | def enigma_decrypt(plugboard_position: list, rotor_position: dict): | 28 | def enigma_decrypt(plugboard_position: list, rotor_position: dict): | ||
| 29 | def decorator (func): | 29 | def decorator (func): | ||
| 30 | def decrypt_message(msg): | 30 | def decrypt_message(msg): | ||
| 31 | rev_rotor_position = {} | 31 | rev_rotor_position = {} | ||
| 32 | for entry in rotor_position: | 32 | for entry in rotor_position: | ||
| 33 | rev_rotor_position[rotor_position[entry]] = entry | 33 | rev_rotor_position[rotor_position[entry]] = entry | ||
| 34 | 34 | ||||
| 35 | msg = rotor(msg, rev_rotor_position) | 35 | msg = rotor(msg, rev_rotor_position) | ||
| 36 | msg = plugboard(msg, plugboard_position) | 36 | msg = plugboard(msg, plugboard_position) | ||
| 37 | return func(msg) | 37 | return func(msg) | ||
| 38 | return decrypt_message | 38 | return decrypt_message | ||
| 39 | return decorator | 39 | return decorator |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | def plugboard(msg: str, plugboard_position: list): | f | 1 | def plugboard(msg: str, plugboard_position: list): |
| n | n | 2 | swaps = {} | ||
| 3 | |||||
| 2 | for entry in plugboard_position: | 4 | for entry in plugboard_position: | ||
| 3 | pair = list(entry) | 5 | pair = list(entry) | ||
| n | 4 | msg = msg.replace(pair[0], '#') | n | 6 | swaps[pair[0]] = pair[1] |
| 5 | msg = msg.replace(pair[1], pair[0]) | 7 | swaps[pair[1]] = pair[0] | ||
| 6 | msg = msg.replace('#', pair[1]) | 8 | |||
| 7 | return msg | 9 | alpha = [chr(i) for i in range(ord('a'), ord('z')+1)] | ||
| 10 | for ch in alpha: | ||||
| 11 | if ch not in swaps: | ||||
| 12 | swaps[ch] = ch | ||||
| 13 | |||||
| 14 | return ''.join(swaps[ch] for ch in msg) | ||||
| 8 | 15 | ||||
| 9 | def rotor(msg: str, rotor_position: dict): | 16 | def rotor(msg: str, rotor_position: dict): | ||
| n | 10 | result = "" | n | 17 | return ''.join(rotor_position[ch] for ch in msg) |
| 11 | for i in range(len(msg)): | ||||
| 12 | result += rotor_position[msg[i]] | ||||
| 13 | return result | ||||
| 14 | 18 | ||||
| 15 | def enigma_encrypt(plugboard_position: list, rotor_position: dict): | 19 | def enigma_encrypt(plugboard_position: list, rotor_position: dict): | ||
| 16 | def decorator (func): | 20 | def decorator (func): | ||
| 17 | def encrypt_message(msg): | 21 | def encrypt_message(msg): | ||
| 18 | msg = plugboard(msg, plugboard_position) | 22 | msg = plugboard(msg, plugboard_position) | ||
| 19 | msg = rotor(msg, rotor_position) | 23 | msg = rotor(msg, rotor_position) | ||
| 20 | return func(msg) | 24 | return func(msg) | ||
| 21 | return encrypt_message | 25 | return encrypt_message | ||
| 22 | return decorator | 26 | return decorator | ||
| 23 | 27 | ||||
| 24 | def enigma_decrypt(plugboard_position: list, rotor_position: dict): | 28 | def enigma_decrypt(plugboard_position: list, rotor_position: dict): | ||
| 25 | def decorator (func): | 29 | def decorator (func): | ||
| 26 | def decrypt_message(msg): | 30 | def decrypt_message(msg): | ||
| 27 | rev_rotor_position = {} | 31 | rev_rotor_position = {} | ||
| 28 | for entry in rotor_position: | 32 | for entry in rotor_position: | ||
| t | 29 | rev_rotor_position.update({rotor_position[entry]: entry}) | t | 33 | rev_rotor_position[rotor_position[entry]] = entry |
| 30 | 34 | ||||
| 31 | msg = rotor(msg, rev_rotor_position) | 35 | msg = rotor(msg, rev_rotor_position) | ||
| 32 | msg = plugboard(msg, plugboard_position) | 36 | msg = plugboard(msg, plugboard_position) | ||
| 33 | return func(msg) | 37 | return func(msg) | ||
| 34 | return decrypt_message | 38 | return decrypt_message | ||
| 35 | return decorator | 39 | return decorator |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||