1def switch(letter, connection):
2 if letter not in connection:
3 return letter
4 for l in connection:
5 if l != letter:
6 return l
7 return letter
8
9
10def plugboard_one_letter(letter, plugboard_position):
11 new_letter = letter
12 for connection in plugboard_position:
13 if letter not in connection:
14 continue
15 return switch(letter, connection)
16 return letter
17
18
19def plugboard(to_encrypt, plugboard_position):
20 return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt)))
21
22
23def rotor(to_encrypt, rotor_position):
24 return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))
25
26
27def rotor_decrycpt(to_decrypt, rotor_position):
28 decrypting_rotor_pos = {}
29 for key, value in rotor_position.items():
30 decrypting_rotor_pos[value] = key
31 return rotor(to_decrypt, decrypting_rotor_pos)
32
33
34def enigma_encrypt(plugboard_position, rotor_position):
35 def decorator(func):
36 def encrypted(string):
37 return func(rotor(plugboard(string, plugboard_position), rotor_position))
38 return encrypted
39 return decorator
40
41
42def enigma_decrypt(plugboard_position, rotor_position):
43 def decorator(func):
44 def decrypted(string):
45 return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position))
46 return decrypted
47 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 45, in decrypted
return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position))
File "/tmp/solution.py", line 31, in rotor_decrycpt
return rotor(to_decrypt, decrypting_rotor_pos)
File "/tmp/solution.py", line 24, in rotor
return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))
TypeError: sequence item 1: expected str instance, NoneType found
======================================================================
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 45, in decrypted
return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position))
File "/tmp/solution.py", line 31, in rotor_decrycpt
return rotor(to_decrypt, decrypting_rotor_pos)
File "/tmp/solution.py", line 24, in rotor
return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))
TypeError: sequence item 3: expected str instance, NoneType found
======================================================================
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 37, in encrypted
return func(rotor(plugboard(string, plugboard_position), rotor_position))
File "/tmp/solution.py", line 24, in rotor
return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))
TypeError: sequence item 3: expected str instance, NoneType found
======================================================================
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 24, in rotor
return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))
TypeError: sequence item 4: expected str instance, NoneType found
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (errors=4)
| f | 1 | def switch(letter, connection): | f | 1 | def switch(letter, connection): |
| 2 | if letter not in connection: | 2 | if letter not in connection: | ||
| 3 | return letter | 3 | return letter | ||
| 4 | for l in connection: | 4 | for l in connection: | ||
| 5 | if l != letter: | 5 | if l != letter: | ||
| 6 | return l | 6 | return l | ||
| 7 | return letter | 7 | return letter | ||
| 8 | 8 | ||||
| 9 | 9 | ||||
| 10 | def plugboard_one_letter(letter, plugboard_position): | 10 | def plugboard_one_letter(letter, plugboard_position): | ||
| 11 | new_letter = letter | 11 | new_letter = letter | ||
| 12 | for connection in plugboard_position: | 12 | for connection in plugboard_position: | ||
| 13 | if letter not in connection: | 13 | if letter not in connection: | ||
| 14 | continue | 14 | continue | ||
| 15 | return switch(letter, connection) | 15 | return switch(letter, connection) | ||
| 16 | return letter | 16 | return letter | ||
| 17 | 17 | ||||
| 18 | 18 | ||||
| 19 | def plugboard(to_encrypt, plugboard_position): | 19 | def plugboard(to_encrypt, plugboard_position): | ||
| 20 | return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt))) | 20 | return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt))) | ||
| 21 | 21 | ||||
| 22 | 22 | ||||
| 23 | def rotor(to_encrypt, rotor_position): | 23 | def rotor(to_encrypt, rotor_position): | ||
| 24 | return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt))) | 24 | return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt))) | ||
| 25 | 25 | ||||
| 26 | 26 | ||||
| 27 | def rotor_decrycpt(to_decrypt, rotor_position): | 27 | def rotor_decrycpt(to_decrypt, rotor_position): | ||
| 28 | decrypting_rotor_pos = {} | 28 | decrypting_rotor_pos = {} | ||
| 29 | for key, value in rotor_position.items(): | 29 | for key, value in rotor_position.items(): | ||
| 30 | decrypting_rotor_pos[value] = key | 30 | decrypting_rotor_pos[value] = key | ||
| 31 | return rotor(to_decrypt, decrypting_rotor_pos) | 31 | return rotor(to_decrypt, decrypting_rotor_pos) | ||
| 32 | 32 | ||||
| 33 | 33 | ||||
| 34 | def enigma_encrypt(plugboard_position, rotor_position): | 34 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 35 | def decorator(func): | 35 | def decorator(func): | ||
| 36 | def encrypted(string): | 36 | def encrypted(string): | ||
| 37 | return func(rotor(plugboard(string, plugboard_position), rotor_position)) | 37 | return func(rotor(plugboard(string, plugboard_position), rotor_position)) | ||
| 38 | return encrypted | 38 | return encrypted | ||
| 39 | return decorator | 39 | return decorator | ||
| 40 | 40 | ||||
| 41 | 41 | ||||
| 42 | def enigma_decrypt(plugboard_position, rotor_position): | 42 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 43 | def decorator(func): | 43 | def decorator(func): | ||
| 44 | def decrypted(string): | 44 | def decrypted(string): | ||
| 45 | return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position)) | 45 | return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position)) | ||
| 46 | return decrypted | 46 | return decrypted | ||
| 47 | return decorator | 47 | return decorator | ||
| t | 48 | t | |||
| 49 | |||||
| 50 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | ||||
| 51 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||||
| 52 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||||
| 53 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||||
| 54 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||||
| 55 | 'm': 'r', 'c': 'k'} |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | def switch(letter, connection): | f | 1 | def switch(letter, connection): |
| 2 | if letter not in connection: | 2 | if letter not in connection: | ||
| 3 | return letter | 3 | return letter | ||
| 4 | for l in connection: | 4 | for l in connection: | ||
| 5 | if l != letter: | 5 | if l != letter: | ||
| 6 | return l | 6 | return l | ||
| 7 | return letter | 7 | return letter | ||
| 8 | 8 | ||||
| 9 | 9 | ||||
| 10 | def plugboard_one_letter(letter, plugboard_position): | 10 | def plugboard_one_letter(letter, plugboard_position): | ||
| 11 | new_letter = letter | 11 | new_letter = letter | ||
| 12 | for connection in plugboard_position: | 12 | for connection in plugboard_position: | ||
| 13 | if letter not in connection: | 13 | if letter not in connection: | ||
| 14 | continue | 14 | continue | ||
| 15 | return switch(letter, connection) | 15 | return switch(letter, connection) | ||
| 16 | return letter | 16 | return letter | ||
| 17 | 17 | ||||
| 18 | 18 | ||||
| 19 | def plugboard(to_encrypt, plugboard_position): | 19 | def plugboard(to_encrypt, plugboard_position): | ||
| 20 | return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt))) | 20 | return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt))) | ||
| 21 | 21 | ||||
| 22 | 22 | ||||
| 23 | def rotor(to_encrypt, rotor_position): | 23 | def rotor(to_encrypt, rotor_position): | ||
| 24 | return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt))) | 24 | return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt))) | ||
| 25 | 25 | ||||
| 26 | 26 | ||||
| 27 | def rotor_decrycpt(to_decrypt, rotor_position): | 27 | def rotor_decrycpt(to_decrypt, rotor_position): | ||
| 28 | decrypting_rotor_pos = {} | 28 | decrypting_rotor_pos = {} | ||
| n | 29 | for kvp in rotor_position.items(): | n | 29 | for key, value in rotor_position.items(): |
| 30 | decrypting_rotor_pos[kvp[1]] = kvp[0] | 30 | decrypting_rotor_pos[value] = key | ||
| 31 | return rotor(to_decrypt, decrypting_rotor_pos) | 31 | return rotor(to_decrypt, decrypting_rotor_pos) | ||
| 32 | 32 | ||||
| 33 | 33 | ||||
| 34 | def enigma_encrypt(plugboard_position, rotor_position): | 34 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 35 | def decorator(func): | 35 | def decorator(func): | ||
| 36 | def encrypted(string): | 36 | def encrypted(string): | ||
| 37 | return func(rotor(plugboard(string, plugboard_position), rotor_position)) | 37 | return func(rotor(plugboard(string, plugboard_position), rotor_position)) | ||
| 38 | return encrypted | 38 | return encrypted | ||
| 39 | return decorator | 39 | return decorator | ||
| 40 | 40 | ||||
| 41 | 41 | ||||
| 42 | def enigma_decrypt(plugboard_position, rotor_position): | 42 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 43 | def decorator(func): | 43 | def decorator(func): | ||
| 44 | def decrypted(string): | 44 | def decrypted(string): | ||
| 45 | return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position)) | 45 | return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position)) | ||
| 46 | return decrypted | 46 | return decrypted | ||
| 47 | return decorator | 47 | return decorator | ||
| t | t | 48 | |||
| 49 | |||||
| 50 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | ||||
| 51 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||||
| 52 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||||
| 53 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||||
| 54 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||||
| 55 | 'm': 'r', 'c': 'k'} |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | def switch(letter, connection): | f | 1 | def switch(letter, connection): |
| 2 | if letter not in connection: | 2 | if letter not in connection: | ||
| 3 | return letter | 3 | return letter | ||
| 4 | for l in connection: | 4 | for l in connection: | ||
| 5 | if l != letter: | 5 | if l != letter: | ||
| 6 | return l | 6 | return l | ||
| 7 | return letter | 7 | return letter | ||
| 8 | 8 | ||||
| 9 | 9 | ||||
| 10 | def plugboard_one_letter(letter, plugboard_position): | 10 | def plugboard_one_letter(letter, plugboard_position): | ||
| 11 | new_letter = letter | 11 | new_letter = letter | ||
| 12 | for connection in plugboard_position: | 12 | for connection in plugboard_position: | ||
| 13 | if letter not in connection: | 13 | if letter not in connection: | ||
| 14 | continue | 14 | continue | ||
| t | 15 | new_letter = switch(letter, connection) | t | 15 | return switch(letter, connection) |
| 16 | break | ||||
| 17 | return new_letter | 16 | return letter | ||
| 18 | 17 | ||||
| 19 | 18 | ||||
| 20 | def plugboard(to_encrypt, plugboard_position): | 19 | def plugboard(to_encrypt, plugboard_position): | ||
| 21 | return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt))) | 20 | return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt))) | ||
| 22 | 21 | ||||
| 23 | 22 | ||||
| 24 | def rotor(to_encrypt, rotor_position): | 23 | def rotor(to_encrypt, rotor_position): | ||
| 25 | return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt))) | 24 | return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt))) | ||
| 26 | 25 | ||||
| 27 | 26 | ||||
| 28 | def rotor_decrycpt(to_decrypt, rotor_position): | 27 | def rotor_decrycpt(to_decrypt, rotor_position): | ||
| 29 | decrypting_rotor_pos = {} | 28 | decrypting_rotor_pos = {} | ||
| 30 | for kvp in rotor_position.items(): | 29 | for kvp in rotor_position.items(): | ||
| 31 | decrypting_rotor_pos[kvp[1]] = kvp[0] | 30 | decrypting_rotor_pos[kvp[1]] = kvp[0] | ||
| 32 | return rotor(to_decrypt, decrypting_rotor_pos) | 31 | return rotor(to_decrypt, decrypting_rotor_pos) | ||
| 33 | 32 | ||||
| 34 | 33 | ||||
| 35 | def enigma_encrypt(plugboard_position, rotor_position): | 34 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 36 | def decorator(func): | 35 | def decorator(func): | ||
| 37 | def encrypted(string): | 36 | def encrypted(string): | ||
| 38 | return func(rotor(plugboard(string, plugboard_position), rotor_position)) | 37 | return func(rotor(plugboard(string, plugboard_position), rotor_position)) | ||
| 39 | return encrypted | 38 | return encrypted | ||
| 40 | return decorator | 39 | return decorator | ||
| 41 | 40 | ||||
| 42 | 41 | ||||
| 43 | def enigma_decrypt(plugboard_position, rotor_position): | 42 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 44 | def decorator(func): | 43 | def decorator(func): | ||
| 45 | def decrypted(string): | 44 | def decrypted(string): | ||
| 46 | return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position)) | 45 | return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position)) | ||
| 47 | return decrypted | 46 | return decrypted | ||
| 48 | return decorator | 47 | return decorator |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| n | 1 | def check_and_switch(letter, connection): | n | 1 | def switch(letter, connection): |
| 2 | if letter not in connection: | 2 | if letter not in connection: | ||
| 3 | return letter | 3 | return letter | ||
| 4 | for l in connection: | 4 | for l in connection: | ||
| 5 | if l != letter: | 5 | if l != letter: | ||
| 6 | return l | 6 | return l | ||
| 7 | return letter | 7 | return letter | ||
| 8 | 8 | ||||
| 9 | 9 | ||||
| 10 | def plugboard_one_letter(letter, plugboard_position): | 10 | def plugboard_one_letter(letter, plugboard_position): | ||
| 11 | new_letter = letter | 11 | new_letter = letter | ||
| 12 | for connection in plugboard_position: | 12 | for connection in plugboard_position: | ||
| t | t | 13 | if letter not in connection: | ||
| 14 | continue | ||||
| 13 | new_letter = check_and_switch(letter, connection) | 15 | new_letter = switch(letter, connection) | ||
| 16 | break | ||||
| 14 | return new_letter | 17 | return new_letter | ||
| 15 | 18 | ||||
| 16 | 19 | ||||
| 17 | def plugboard(to_encrypt, plugboard_position): | 20 | def plugboard(to_encrypt, plugboard_position): | ||
| 18 | return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt))) | 21 | return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt))) | ||
| 19 | 22 | ||||
| 20 | 23 | ||||
| 21 | def rotor(to_encrypt, rotor_position): | 24 | def rotor(to_encrypt, rotor_position): | ||
| 22 | return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt))) | 25 | return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt))) | ||
| 23 | 26 | ||||
| 24 | 27 | ||||
| 25 | def rotor_decrycpt(to_decrypt, rotor_position): | 28 | def rotor_decrycpt(to_decrypt, rotor_position): | ||
| 26 | decrypting_rotor_pos = {} | 29 | decrypting_rotor_pos = {} | ||
| 27 | for kvp in rotor_position.items(): | 30 | for kvp in rotor_position.items(): | ||
| 28 | decrypting_rotor_pos[kvp[1]] = kvp[0] | 31 | decrypting_rotor_pos[kvp[1]] = kvp[0] | ||
| 29 | return rotor(to_decrypt, decrypting_rotor_pos) | 32 | return rotor(to_decrypt, decrypting_rotor_pos) | ||
| 30 | 33 | ||||
| 31 | 34 | ||||
| 32 | def enigma_encrypt(plugboard_position, rotor_position): | 35 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 33 | def decorator(func): | 36 | def decorator(func): | ||
| 34 | def encrypted(string): | 37 | def encrypted(string): | ||
| 35 | return func(rotor(plugboard(string, plugboard_position), rotor_position)) | 38 | return func(rotor(plugboard(string, plugboard_position), rotor_position)) | ||
| 36 | return encrypted | 39 | return encrypted | ||
| 37 | return decorator | 40 | return decorator | ||
| 38 | 41 | ||||
| 39 | 42 | ||||
| 40 | def enigma_decrypt(plugboard_position, rotor_position): | 43 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 41 | def decorator(func): | 44 | def decorator(func): | ||
| 42 | def decrypted(string): | 45 | def decrypted(string): | ||
| 43 | return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position)) | 46 | return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position)) | ||
| 44 | return decrypted | 47 | return decrypted | ||
| 45 | return decorator | 48 | return decorator |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||