1def plugboard(input_string, plugboard_sets):
2
3 convert_rules = {}
4 for single_set in plugboard_sets:
5 a, b = list(single_set) # a, b = single_set works as well :)
6 convert_rules[a] = b
7 convert_rules[b] = a
8
9 result = ""
10 for symbol in input_string:
11 result += convert_rules.get(symbol, symbol)
12
13 return result
14
15
16def rotor(input_string, conversion_dict):
17 result = ""
18 for symbol in input_string:
19 result += conversion_dict[symbol]
20
21 return result
22
23
24def enigma_encrypt(plugboard_position, rotor_position):
25 def decorator(func):
26 def encrypt(text):
27 encrypted_text = plugboard(text, plugboard_position)
28 encrypted_text = rotor(encrypted_text, rotor_position)
29 return func(encrypted_text)
30 return encrypt
31 return decorator
32
33
34def enigma_decrypt(plugboard_position, rotor_position):
35 def decorator(func):
36 def decrypt(text):
37
38 reversed_pos = {}
39 for a, b in rotor_position.items():
40 reversed_pos[b] = a
41
42 decrypted_text = ""
43 for symbol in text:
44 decrypted_text += reversed_pos.get(symbol, symbol)
45 decrypted_text = plugboard(decrypted_text, plugboard_position)
46 return func(decrypted_text)
47 return decrypt
48 return decorator
E..E....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 decrypt
return func(decrypted_text)
File "/tmp/solution.py", line 28, in encrypt
encrypted_text = rotor(encrypted_text, rotor_position)
File "/tmp/solution.py", line 19, in rotor
result += conversion_dict[symbol]
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 28, in encrypt
encrypted_text = rotor(encrypted_text, rotor_position)
File "/tmp/solution.py", line 19, in rotor
result += conversion_dict[symbol]
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 += conversion_dict[symbol]
KeyError: ' '
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (errors=3)
| f | 1 | def plugboard(input_string, plugboard_sets): | f | 1 | def plugboard(input_string, plugboard_sets): |
| 2 | 2 | ||||
| 3 | convert_rules = {} | 3 | convert_rules = {} | ||
| 4 | for single_set in plugboard_sets: | 4 | for single_set in plugboard_sets: | ||
| 5 | a, b = list(single_set) # a, b = single_set works as well :) | 5 | a, b = list(single_set) # a, b = single_set works as well :) | ||
| 6 | convert_rules[a] = b | 6 | convert_rules[a] = b | ||
| 7 | convert_rules[b] = a | 7 | convert_rules[b] = a | ||
| 8 | 8 | ||||
| 9 | result = "" | 9 | result = "" | ||
| 10 | for symbol in input_string: | 10 | for symbol in input_string: | ||
| 11 | result += convert_rules.get(symbol, symbol) | 11 | result += convert_rules.get(symbol, symbol) | ||
| 12 | 12 | ||||
| 13 | return result | 13 | return result | ||
| 14 | 14 | ||||
| 15 | 15 | ||||
| 16 | def rotor(input_string, conversion_dict): | 16 | def rotor(input_string, conversion_dict): | ||
| 17 | result = "" | 17 | result = "" | ||
| 18 | for symbol in input_string: | 18 | for symbol in input_string: | ||
| 19 | result += conversion_dict[symbol] | 19 | result += conversion_dict[symbol] | ||
| 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(func): | 25 | def decorator(func): | ||
| 26 | def encrypt(text): | 26 | def encrypt(text): | ||
| 27 | encrypted_text = plugboard(text, plugboard_position) | 27 | encrypted_text = plugboard(text, plugboard_position) | ||
| 28 | encrypted_text = rotor(encrypted_text, rotor_position) | 28 | encrypted_text = rotor(encrypted_text, rotor_position) | ||
| 29 | return func(encrypted_text) | 29 | return func(encrypted_text) | ||
| 30 | return encrypt | 30 | return encrypt | ||
| 31 | return decorator | 31 | return decorator | ||
| 32 | 32 | ||||
| 33 | 33 | ||||
| 34 | def enigma_decrypt(plugboard_position, rotor_position): | 34 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 35 | def decorator(func): | 35 | def decorator(func): | ||
| 36 | def decrypt(text): | 36 | def decrypt(text): | ||
| 37 | 37 | ||||
| 38 | reversed_pos = {} | 38 | reversed_pos = {} | ||
| 39 | for a, b in rotor_position.items(): | 39 | for a, b in rotor_position.items(): | ||
| 40 | reversed_pos[b] = a | 40 | reversed_pos[b] = a | ||
| 41 | 41 | ||||
| 42 | decrypted_text = "" | 42 | decrypted_text = "" | ||
| 43 | for symbol in text: | 43 | for symbol in text: | ||
| 44 | decrypted_text += reversed_pos.get(symbol, symbol) | 44 | decrypted_text += reversed_pos.get(symbol, symbol) | ||
| 45 | decrypted_text = plugboard(decrypted_text, plugboard_position) | 45 | decrypted_text = plugboard(decrypted_text, plugboard_position) | ||
| 46 | return func(decrypted_text) | 46 | return func(decrypted_text) | ||
| 47 | return decrypt | 47 | return decrypt | ||
| 48 | return decorator | 48 | return decorator | ||
| t | 49 | t | |||
| 50 | |||||
| 51 | def enigma_decrypt(plugboard_position, rotor_position): | ||||
| 52 | def decorator(func): | ||||
| 53 | def decrypt(text): | ||||
| 54 | |||||
| 55 | reversed_pos = {} | ||||
| 56 | for a, b in rotor_position.items(): | ||||
| 57 | reversed_pos[b] = a | ||||
| 58 | |||||
| 59 | decrypted_text = "" | ||||
| 60 | for symbol in text: | ||||
| 61 | decrypted_text += reversed_pos.get(symbol, symbol) | ||||
| 62 | decrypted_text = plugboard(decrypted_text, plugboard_position) | ||||
| 63 | return func(decrypted_text) | ||||
| 64 | return decrypt | ||||
| 65 | return decorator |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| n | 1 | n | |||
| 2 | def plugboard(input_string, plugboard_sets): | 1 | def plugboard(input_string, plugboard_sets): | ||
| 3 | 2 | ||||
| 4 | convert_rules = {} | 3 | convert_rules = {} | ||
| 5 | for single_set in plugboard_sets: | 4 | for single_set in plugboard_sets: | ||
| 6 | a, b = list(single_set) # a, b = single_set works as well :) | 5 | a, b = list(single_set) # a, b = single_set works as well :) | ||
| 7 | convert_rules[a] = b | 6 | convert_rules[a] = b | ||
| 8 | convert_rules[b] = a | 7 | convert_rules[b] = a | ||
| 9 | 8 | ||||
| 10 | result = "" | 9 | result = "" | ||
| 11 | for symbol in input_string: | 10 | for symbol in input_string: | ||
| n | 12 | if symbol in convert_rules: | n | ||
| 13 | result += convert_rules[symbol] | 11 | result += convert_rules.get(symbol, symbol) | ||
| 14 | else: | ||||
| 15 | result += symbol | ||||
| 16 | 12 | ||||
| 17 | return result | 13 | return result | ||
| 18 | 14 | ||||
| 19 | 15 | ||||
| 20 | def rotor(input_string, conversion_dict): | 16 | def rotor(input_string, conversion_dict): | ||
| 21 | result = "" | 17 | result = "" | ||
| 22 | for symbol in input_string: | 18 | for symbol in input_string: | ||
| 23 | result += conversion_dict[symbol] | 19 | result += conversion_dict[symbol] | ||
| 24 | 20 | ||||
| 25 | return result | 21 | return result | ||
| 26 | 22 | ||||
| 27 | 23 | ||||
| 28 | def enigma_encrypt(plugboard_position, rotor_position): | 24 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 29 | def decorator(func): | 25 | def decorator(func): | ||
| 30 | def encrypt(text): | 26 | def encrypt(text): | ||
| 31 | encrypted_text = plugboard(text, plugboard_position) | 27 | encrypted_text = plugboard(text, plugboard_position) | ||
| 32 | encrypted_text = rotor(encrypted_text, rotor_position) | 28 | encrypted_text = rotor(encrypted_text, rotor_position) | ||
| 33 | return func(encrypted_text) | 29 | return func(encrypted_text) | ||
| 34 | return encrypt | 30 | return encrypt | ||
| 35 | return decorator | 31 | return decorator | ||
| 36 | 32 | ||||
| 37 | 33 | ||||
| 38 | def enigma_decrypt(plugboard_position, rotor_position): | 34 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 39 | def decorator(func): | 35 | def decorator(func): | ||
| 40 | def decrypt(text): | 36 | def decrypt(text): | ||
| 41 | 37 | ||||
| 42 | reversed_pos = {} | 38 | reversed_pos = {} | ||
| 43 | for a, b in rotor_position.items(): | 39 | for a, b in rotor_position.items(): | ||
| 44 | reversed_pos[b] = a | 40 | reversed_pos[b] = a | ||
| 45 | 41 | ||||
| 46 | decrypted_text = "" | 42 | decrypted_text = "" | ||
| 47 | for symbol in text: | 43 | for symbol in text: | ||
| 48 | decrypted_text += reversed_pos.get(symbol, symbol) | 44 | decrypted_text += reversed_pos.get(symbol, symbol) | ||
| 49 | decrypted_text = plugboard(decrypted_text, plugboard_position) | 45 | decrypted_text = plugboard(decrypted_text, plugboard_position) | ||
| 50 | return func(decrypted_text) | 46 | return func(decrypted_text) | ||
| 51 | return decrypt | 47 | return decrypt | ||
| 52 | return decorator | 48 | return decorator | ||
| 53 | 49 | ||||
| t | t | 50 | |||
| 51 | def enigma_decrypt(plugboard_position, rotor_position): | ||||
| 52 | def decorator(func): | ||||
| 53 | def decrypt(text): | ||||
| 54 | |||||
| 55 | reversed_pos = {} | ||||
| 56 | for a, b in rotor_position.items(): | ||||
| 57 | reversed_pos[b] = a | ||||
| 58 | |||||
| 59 | decrypted_text = "" | ||||
| 60 | for symbol in text: | ||||
| 61 | decrypted_text += reversed_pos.get(symbol, symbol) | ||||
| 62 | decrypted_text = plugboard(decrypted_text, plugboard_position) | ||||
| 63 | return func(decrypted_text) | ||||
| 64 | return decrypt | ||||
| 65 | return decorator |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | f | 1 | ||
| 2 | def plugboard(input_string, plugboard_sets): | 2 | def plugboard(input_string, plugboard_sets): | ||
| 3 | 3 | ||||
| 4 | convert_rules = {} | 4 | convert_rules = {} | ||
| 5 | for single_set in plugboard_sets: | 5 | for single_set in plugboard_sets: | ||
| 6 | a, b = list(single_set) # a, b = single_set works as well :) | 6 | a, b = list(single_set) # a, b = single_set works as well :) | ||
| 7 | convert_rules[a] = b | 7 | convert_rules[a] = b | ||
| 8 | convert_rules[b] = a | 8 | convert_rules[b] = a | ||
| 9 | 9 | ||||
| 10 | result = "" | 10 | result = "" | ||
| 11 | for symbol in input_string: | 11 | for symbol in input_string: | ||
| 12 | if symbol in convert_rules: | 12 | if symbol in convert_rules: | ||
| 13 | result += convert_rules[symbol] | 13 | result += convert_rules[symbol] | ||
| 14 | else: | 14 | else: | ||
| 15 | result += symbol | 15 | result += symbol | ||
| 16 | 16 | ||||
| 17 | return result | 17 | return result | ||
| 18 | 18 | ||||
| 19 | 19 | ||||
| 20 | def rotor(input_string, conversion_dict): | 20 | def rotor(input_string, conversion_dict): | ||
| 21 | result = "" | 21 | result = "" | ||
| 22 | for symbol in input_string: | 22 | for symbol in input_string: | ||
| 23 | result += conversion_dict[symbol] | 23 | result += conversion_dict[symbol] | ||
| 24 | 24 | ||||
| 25 | return result | 25 | return result | ||
| 26 | 26 | ||||
| n | n | 27 | |||
| 27 | def enigma_encrypt(plugboard_position, rotor_position): | 28 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 28 | def decorator(func): | 29 | def decorator(func): | ||
| 29 | def encrypt(text): | 30 | def encrypt(text): | ||
| 30 | encrypted_text = plugboard(text, plugboard_position) | 31 | encrypted_text = plugboard(text, plugboard_position) | ||
| 31 | encrypted_text = rotor(encrypted_text, rotor_position) | 32 | encrypted_text = rotor(encrypted_text, rotor_position) | ||
| 32 | return func(encrypted_text) | 33 | return func(encrypted_text) | ||
| 33 | return encrypt | 34 | return encrypt | ||
| 34 | return decorator | 35 | return decorator | ||
| 35 | 36 | ||||
| 36 | 37 | ||||
| 37 | def enigma_decrypt(plugboard_position, rotor_position): | 38 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 38 | def decorator(func): | 39 | def decorator(func): | ||
| 39 | def decrypt(text): | 40 | def decrypt(text): | ||
| 40 | 41 | ||||
| 41 | reversed_pos = {} | 42 | reversed_pos = {} | ||
| 42 | for a, b in rotor_position.items(): | 43 | for a, b in rotor_position.items(): | ||
| 43 | reversed_pos[b] = a | 44 | reversed_pos[b] = a | ||
| 44 | 45 | ||||
| t | 45 | decrypted_text="" | t | 46 | decrypted_text = "" |
| 46 | for symbol in text: | 47 | for symbol in text: | ||
| 47 | decrypted_text += reversed_pos.get(symbol, symbol) | 48 | decrypted_text += reversed_pos.get(symbol, symbol) | ||
| 48 | decrypted_text = plugboard(decrypted_text, plugboard_position) | 49 | decrypted_text = plugboard(decrypted_text, plugboard_position) | ||
| 49 | return func(decrypted_text) | 50 | return func(decrypted_text) | ||
| 50 | return decrypt | 51 | return decrypt | ||
| 51 | return decorator | 52 | return decorator | ||
| 52 | 53 |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||