1def plugboard(text, plugboard_position):
2 def match(letter):
3 for x, y in plugboard_position:
4 if letter == x:
5 return y
6 if letter == y:
7 return x
8 return letter
9 return ''.join([match(letter) for letter in text])
10
11def rotor(text, rotor_position):
12 return ''.join([rotor_position.get(letter, letter) for letter in text])
13
14def enigma_encrypt(plugboard_position, rotor_position):
15 def encryptor(func):
16 def encrypt_func(text):
17 encrypted = rotor(plugboard(text, plugboard_position), rotor_position)
18 return func(encrypted)
19 return encrypt_func
20 return encryptor
21
22def enigma_decrypt(plugboard_position, rotor_position):
23 def decryptor(func):
24 def decrypt_func(text):
25 reverse_rotor = {value: key for key, value in rotor_position.items()}
26 decrypted = plugboard(rotor(text, reverse_rotor), plugboard_position)
27 return func(decrypted)
28 return decrypt_func
29 return decryptor
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
| f | 1 | def plugboard(text, plugboard_position): | f | 1 | def plugboard(text, plugboard_position): |
| 2 | def match(letter): | 2 | def match(letter): | ||
| 3 | for x, y in plugboard_position: | 3 | for x, y in plugboard_position: | ||
| 4 | if letter == x: | 4 | if letter == x: | ||
| 5 | return y | 5 | return y | ||
| 6 | if letter == y: | 6 | if letter == y: | ||
| 7 | return x | 7 | return x | ||
| 8 | return letter | 8 | return letter | ||
| 9 | return ''.join([match(letter) for letter in text]) | 9 | return ''.join([match(letter) for letter in text]) | ||
| 10 | 10 | ||||
| 11 | def rotor(text, rotor_position): | 11 | def rotor(text, rotor_position): | ||
| t | 12 | return ''.join([value if letter == key else '' for letter in text for key, value in rotor_position.items()]) | t | 12 | return ''.join([rotor_position.get(letter, letter) for letter in text]) |
| 13 | 13 | ||||
| 14 | def enigma_encrypt(plugboard_position, rotor_position): | 14 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 15 | def encryptor(func): | 15 | def encryptor(func): | ||
| 16 | def encrypt_func(text): | 16 | def encrypt_func(text): | ||
| 17 | encrypted = rotor(plugboard(text, plugboard_position), rotor_position) | 17 | encrypted = rotor(plugboard(text, plugboard_position), rotor_position) | ||
| 18 | return func(encrypted) | 18 | return func(encrypted) | ||
| 19 | return encrypt_func | 19 | return encrypt_func | ||
| 20 | return encryptor | 20 | return encryptor | ||
| 21 | 21 | ||||
| 22 | def enigma_decrypt(plugboard_position, rotor_position): | 22 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 23 | def decryptor(func): | 23 | def decryptor(func): | ||
| 24 | def decrypt_func(text): | 24 | def decrypt_func(text): | ||
| 25 | reverse_rotor = {value: key for key, value in rotor_position.items()} | 25 | reverse_rotor = {value: key for key, value in rotor_position.items()} | ||
| 26 | decrypted = plugboard(rotor(text, reverse_rotor), plugboard_position) | 26 | decrypted = plugboard(rotor(text, reverse_rotor), plugboard_position) | ||
| 27 | return func(decrypted) | 27 | return func(decrypted) | ||
| 28 | return decrypt_func | 28 | return decrypt_func | ||
| 29 | return decryptor | 29 | return decryptor |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | def plugboard(text, plugboard_position): | f | 1 | def plugboard(text, plugboard_position): |
| 2 | def match(letter): | 2 | def match(letter): | ||
| 3 | for x, y in plugboard_position: | 3 | for x, y in plugboard_position: | ||
| 4 | if letter == x: | 4 | if letter == x: | ||
| 5 | return y | 5 | return y | ||
| 6 | if letter == y: | 6 | if letter == y: | ||
| 7 | return x | 7 | return x | ||
| 8 | return letter | 8 | return letter | ||
| 9 | return ''.join([match(letter) for letter in text]) | 9 | return ''.join([match(letter) for letter in text]) | ||
| 10 | 10 | ||||
| 11 | def rotor(text, rotor_position): | 11 | def rotor(text, rotor_position): | ||
| t | 12 | return ''.join([value if letter == key else '' for letter in text for key in rotor_position for value in rotor_position[key]]) | t | 12 | return ''.join([value if letter == key else '' for letter in text for key, value in rotor_position.items()]) |
| 13 | 13 | ||||
| 14 | def enigma_encrypt(plugboard_position, rotor_position): | 14 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 15 | def encryptor(func): | 15 | def encryptor(func): | ||
| 16 | def encrypt_func(text): | 16 | def encrypt_func(text): | ||
| 17 | encrypted = rotor(plugboard(text, plugboard_position), rotor_position) | 17 | encrypted = rotor(plugboard(text, plugboard_position), rotor_position) | ||
| 18 | return func(encrypted) | 18 | return func(encrypted) | ||
| 19 | return encrypt_func | 19 | return encrypt_func | ||
| 20 | return encryptor | 20 | return encryptor | ||
| 21 | 21 | ||||
| 22 | def enigma_decrypt(plugboard_position, rotor_position): | 22 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 23 | def decryptor(func): | 23 | def decryptor(func): | ||
| 24 | def decrypt_func(text): | 24 | def decrypt_func(text): | ||
| 25 | reverse_rotor = {value: key for key, value in rotor_position.items()} | 25 | reverse_rotor = {value: key for key, value in rotor_position.items()} | ||
| 26 | decrypted = plugboard(rotor(text, reverse_rotor), plugboard_position) | 26 | decrypted = plugboard(rotor(text, reverse_rotor), plugboard_position) | ||
| 27 | return func(decrypted) | 27 | return func(decrypted) | ||
| 28 | return decrypt_func | 28 | return decrypt_func | ||
| 29 | return decryptor | 29 | return decryptor |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | def plugboard(text, plugboard_position): | f | 1 | def plugboard(text, plugboard_position): |
| 2 | def match(letter): | 2 | def match(letter): | ||
| 3 | for x, y in plugboard_position: | 3 | for x, y in plugboard_position: | ||
| 4 | if letter == x: | 4 | if letter == x: | ||
| 5 | return y | 5 | return y | ||
| 6 | if letter == y: | 6 | if letter == y: | ||
| 7 | return x | 7 | return x | ||
| 8 | return letter | 8 | return letter | ||
| t | 9 | return ''.join([match(x) for x in text]) | t | 9 | return ''.join([match(letter) for letter in text]) |
| 10 | 10 | ||||
| 11 | def rotor(text, rotor_position): | 11 | def rotor(text, rotor_position): | ||
| 12 | return ''.join([value if letter == key else '' for letter in text for key in rotor_position for value in rotor_position[key]]) | 12 | return ''.join([value if letter == key else '' for letter in text for key in rotor_position for value in rotor_position[key]]) | ||
| 13 | 13 | ||||
| 14 | def enigma_encrypt(plugboard_position, rotor_position): | 14 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 15 | def encryptor(func): | 15 | def encryptor(func): | ||
| 16 | def encrypt_func(text): | 16 | def encrypt_func(text): | ||
| 17 | encrypted = rotor(plugboard(text, plugboard_position), rotor_position) | 17 | encrypted = rotor(plugboard(text, plugboard_position), rotor_position) | ||
| 18 | return func(encrypted) | 18 | return func(encrypted) | ||
| 19 | return encrypt_func | 19 | return encrypt_func | ||
| 20 | return encryptor | 20 | return encryptor | ||
| 21 | 21 | ||||
| 22 | def enigma_decrypt(plugboard_position, rotor_position): | 22 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 23 | def decryptor(func): | 23 | def decryptor(func): | ||
| 24 | def decrypt_func(text): | 24 | def decrypt_func(text): | ||
| 25 | reverse_rotor = {value: key for key, value in rotor_position.items()} | 25 | reverse_rotor = {value: key for key, value in rotor_position.items()} | ||
| 26 | decrypted = plugboard(rotor(text, reverse_rotor), plugboard_position) | 26 | decrypted = plugboard(rotor(text, reverse_rotor), plugboard_position) | ||
| 27 | return func(decrypted) | 27 | return func(decrypted) | ||
| 28 | return decrypt_func | 28 | return decrypt_func | ||
| 29 | return decryptor | 29 | return decryptor |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||