1def plugboard(text, couples_of_letters):
2 new_string = ''
3 for letter in text:
4 for (x, y) in couples_of_letters:
5 if letter == x:
6 letter = y
7 elif letter == y:
8 letter = x
9 new_string += letter
10
11 return new_string
12
13
14def rotor(text, symbol_dictionary):
15 new_string = ''
16 for letter in text:
17 letter = symbol_dictionary[letter]
18 new_string += letter
19
20 return new_string
21
22
23def enigma_encrypt(plugboard_position=[], rotor_position={}):
24 def decorator(func):
25 def encrypt(text):
26 new_string = plugboard(text, plugboard_position)
27 new_string = rotor(new_string, rotor_position)
28 func(new_string)
29 return encrypt
30 return decorator
31
32
33def enigma_decrypt(plugboard_position=[], rotor_position={}):
34 def decorator_dec(func):
35 def decrypt(text):
36 new_string = rotor(text, {v: k for k, v in rotor_position.items()})
37 new_string = plugboard(new_string, plugboard_position)
38 func(new_string)
39 return decrypt
40 return decorator_dec
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 36, in decrypt
new_string = rotor(text, {v: k for k, v in rotor_position.items()})
File "/tmp/solution.py", line 17, in rotor
letter = symbol_dictionary[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 36, in decrypt
new_string = rotor(text, {v: k for k, v in rotor_position.items()})
File "/tmp/solution.py", line 17, in rotor
letter = symbol_dictionary[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 encrypt
new_string = rotor(new_string, rotor_position)
File "/tmp/solution.py", line 17, in rotor
letter = symbol_dictionary[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 17, in rotor
letter = symbol_dictionary[letter]
KeyError: ' '
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (errors=4)
f | 1 | def plugboard(text, couples_of_letters): | f | 1 | def plugboard(text, couples_of_letters): |
2 | new_string = '' | 2 | new_string = '' | ||
3 | for letter in text: | 3 | for letter in text: | ||
4 | for (x, y) in couples_of_letters: | 4 | for (x, y) in couples_of_letters: | ||
5 | if letter == x: | 5 | if letter == x: | ||
6 | letter = y | 6 | letter = y | ||
7 | elif letter == y: | 7 | elif letter == y: | ||
8 | letter = x | 8 | letter = x | ||
9 | new_string += letter | 9 | new_string += letter | ||
10 | 10 | ||||
11 | return new_string | 11 | return new_string | ||
12 | 12 | ||||
13 | 13 | ||||
14 | def rotor(text, symbol_dictionary): | 14 | def rotor(text, symbol_dictionary): | ||
15 | new_string = '' | 15 | new_string = '' | ||
16 | for letter in text: | 16 | for letter in text: | ||
17 | letter = symbol_dictionary[letter] | 17 | letter = symbol_dictionary[letter] | ||
18 | new_string += letter | 18 | new_string += letter | ||
19 | 19 | ||||
20 | return new_string | 20 | return new_string | ||
21 | 21 | ||||
22 | 22 | ||||
23 | def enigma_encrypt(plugboard_position=[], rotor_position={}): | 23 | def enigma_encrypt(plugboard_position=[], rotor_position={}): | ||
24 | def decorator(func): | 24 | def decorator(func): | ||
25 | def encrypt(text): | 25 | def encrypt(text): | ||
26 | new_string = plugboard(text, plugboard_position) | 26 | new_string = plugboard(text, plugboard_position) | ||
27 | new_string = rotor(new_string, rotor_position) | 27 | new_string = rotor(new_string, rotor_position) | ||
28 | func(new_string) | 28 | func(new_string) | ||
29 | return encrypt | 29 | return encrypt | ||
30 | return decorator | 30 | return decorator | ||
31 | 31 | ||||
32 | 32 | ||||
33 | def enigma_decrypt(plugboard_position=[], rotor_position={}): | 33 | def enigma_decrypt(plugboard_position=[], rotor_position={}): | ||
34 | def decorator_dec(func): | 34 | def decorator_dec(func): | ||
35 | def decrypt(text): | 35 | def decrypt(text): | ||
36 | new_string = rotor(text, {v: k for k, v in rotor_position.items()}) | 36 | new_string = rotor(text, {v: k for k, v in rotor_position.items()}) | ||
37 | new_string = plugboard(new_string, plugboard_position) | 37 | new_string = plugboard(new_string, plugboard_position) | ||
38 | func(new_string) | 38 | func(new_string) | ||
39 | return decrypt | 39 | return decrypt | ||
40 | return decorator_dec | 40 | return decorator_dec | ||
41 | 41 | ||||
t | 42 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | t | ||
43 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||||
44 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||||
45 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||||
46 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||||
47 | 'm': 'r', 'c': 'k'} | ||||
48 | 42 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def plugboard(text, couples_of_letters): | f | 1 | def plugboard(text, couples_of_letters): |
2 | new_string = '' | 2 | new_string = '' | ||
3 | for letter in text: | 3 | for letter in text: | ||
4 | for (x, y) in couples_of_letters: | 4 | for (x, y) in couples_of_letters: | ||
5 | if letter == x: | 5 | if letter == x: | ||
6 | letter = y | 6 | letter = y | ||
7 | elif letter == y: | 7 | elif letter == y: | ||
8 | letter = x | 8 | letter = x | ||
9 | new_string += letter | 9 | new_string += letter | ||
10 | 10 | ||||
11 | return new_string | 11 | return new_string | ||
12 | 12 | ||||
13 | 13 | ||||
14 | def rotor(text, symbol_dictionary): | 14 | def rotor(text, symbol_dictionary): | ||
15 | new_string = '' | 15 | new_string = '' | ||
16 | for letter in text: | 16 | for letter in text: | ||
n | 17 | for key in symbol_dictionary: | n | ||
18 | if letter == key: | ||||
19 | letter = symbol_dictionary[key] | 17 | letter = symbol_dictionary[letter] | ||
20 | break | ||||
21 | new_string += letter | 18 | new_string += letter | ||
22 | 19 | ||||
23 | return new_string | 20 | return new_string | ||
24 | 21 | ||||
25 | 22 | ||||
n | 26 | def enigma_encrypt(plugboard_position = [], rotor_position = {}): | n | 23 | def enigma_encrypt(plugboard_position=[], rotor_position={}): |
27 | def decorator(func): | 24 | def decorator(func): | ||
28 | def encrypt(text): | 25 | def encrypt(text): | ||
29 | new_string = plugboard(text, plugboard_position) | 26 | new_string = plugboard(text, plugboard_position) | ||
30 | new_string = rotor(new_string, rotor_position) | 27 | new_string = rotor(new_string, rotor_position) | ||
31 | func(new_string) | 28 | func(new_string) | ||
32 | return encrypt | 29 | return encrypt | ||
33 | return decorator | 30 | return decorator | ||
34 | 31 | ||||
35 | 32 | ||||
n | 36 | def enigma_decrypt(plugboard_position = [], rotor_position = {}): | n | 33 | def enigma_decrypt(plugboard_position=[], rotor_position={}): |
37 | def decorator_dec(func): | 34 | def decorator_dec(func): | ||
38 | def decrypt(text): | 35 | def decrypt(text): | ||
39 | new_string = rotor(text, {v: k for k, v in rotor_position.items()}) | 36 | new_string = rotor(text, {v: k for k, v in rotor_position.items()}) | ||
40 | new_string = plugboard(new_string, plugboard_position) | 37 | new_string = plugboard(new_string, plugboard_position) | ||
41 | func(new_string) | 38 | func(new_string) | ||
42 | return decrypt | 39 | return decrypt | ||
43 | return decorator_dec | 40 | return decorator_dec | ||
44 | 41 | ||||
t | t | 42 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | ||
43 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||||
44 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||||
45 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||||
46 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||||
47 | 'm': 'r', 'c': 'k'} | ||||
48 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def plugboard(text, couples_of_letters): | f | 1 | def plugboard(text, couples_of_letters): |
n | 2 | newString = '' | n | 2 | new_string = '' |
3 | for letter in text: | 3 | for letter in text: | ||
4 | for (x, y) in couples_of_letters: | 4 | for (x, y) in couples_of_letters: | ||
5 | if letter == x: | 5 | if letter == x: | ||
6 | letter = y | 6 | letter = y | ||
7 | elif letter == y: | 7 | elif letter == y: | ||
8 | letter = x | 8 | letter = x | ||
n | 9 | newString += letter | n | 9 | new_string += letter |
10 | 10 | ||||
n | 11 | return newString | n | 11 | return new_string |
12 | 12 | ||||
13 | 13 | ||||
14 | def rotor(text, symbol_dictionary): | 14 | def rotor(text, symbol_dictionary): | ||
n | 15 | newString = '' | n | 15 | new_string = '' |
16 | for letter in text: | 16 | for letter in text: | ||
17 | for key in symbol_dictionary: | 17 | for key in symbol_dictionary: | ||
18 | if letter == key: | 18 | if letter == key: | ||
19 | letter = symbol_dictionary[key] | 19 | letter = symbol_dictionary[key] | ||
20 | break | 20 | break | ||
n | 21 | newString += letter | n | 21 | new_string += letter |
22 | 22 | ||||
n | 23 | return newString | n | 23 | return new_string |
24 | 24 | ||||
25 | 25 | ||||
26 | def enigma_encrypt(plugboard_position = [], rotor_position = {}): | 26 | def enigma_encrypt(plugboard_position = [], rotor_position = {}): | ||
27 | def decorator(func): | 27 | def decorator(func): | ||
28 | def encrypt(text): | 28 | def encrypt(text): | ||
n | 29 | newString = plugboard(text, plugboard_position) | n | 29 | new_string = plugboard(text, plugboard_position) |
30 | newString = rotor(newString, rotor_position) | 30 | new_string = rotor(new_string, rotor_position) | ||
31 | func(newString) | 31 | func(new_string) | ||
32 | return encrypt | 32 | return encrypt | ||
33 | return decorator | 33 | return decorator | ||
34 | 34 | ||||
35 | 35 | ||||
36 | def enigma_decrypt(plugboard_position = [], rotor_position = {}): | 36 | def enigma_decrypt(plugboard_position = [], rotor_position = {}): | ||
37 | def decorator_dec(func): | 37 | def decorator_dec(func): | ||
38 | def decrypt(text): | 38 | def decrypt(text): | ||
t | 39 | newString = rotor(text, {v: k for k, v in rotor_position.items()}) | t | 39 | new_string = rotor(text, {v: k for k, v in rotor_position.items()}) |
40 | newString = plugboard(newString, plugboard_position) | 40 | new_string = plugboard(new_string, plugboard_position) | ||
41 | func(newString) | 41 | func(new_string) | ||
42 | return decrypt | 42 | return decrypt | ||
43 | return decorator_dec | 43 | return decorator_dec | ||
44 | 44 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|