1def plugboard(text, plugboard_position):
2 letter_mapping = {}
3 for a, b in plugboard_position:
4 letter_mapping[a], letter_mapping[b] = b, a
5 return ''.join(letter_mapping.get(letter, letter) for letter in text)
6
7
8def rotor(text, rotor_position):
9 return ''.join(rotor_position.get(letter, letter) for letter in text)
10
11
12def reversed_rotor(rotor_position):
13 return {value: key for key, value in rotor_position.items()}
14
15
16def enigma_encrypt(plugboard_position=[], rotor_position={}):
17 def decorator(func):
18 def wrapper(text):
19 encrypted_text = plugboard(text, plugboard_position)
20 encrypted_text = rotor(encrypted_text, rotor_position)
21 return func(encrypted_text)
22 return wrapper
23 return decorator
24
25
26def enigma_decrypt(plugboard_position=[], rotor_position={}):
27 reversed_rotor_mapping = reversed_rotor(rotor_position)
28
29 def decorator(func):
30 def wrapper(text):
31 decrypted_text = rotor(text, reversed_rotor_mapping)
32 decrypted_text = plugboard(decrypted_text, plugboard_position)
33 return func(decrypted_text)
34 return wrapper
35 return decorator
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
f | 1 | def plugboard(text, plugboard_position): | f | 1 | def plugboard(text, plugboard_position): |
2 | letter_mapping = {} | 2 | letter_mapping = {} | ||
3 | for a, b in plugboard_position: | 3 | for a, b in plugboard_position: | ||
4 | letter_mapping[a], letter_mapping[b] = b, a | 4 | letter_mapping[a], letter_mapping[b] = b, a | ||
5 | return ''.join(letter_mapping.get(letter, letter) for letter in text) | 5 | return ''.join(letter_mapping.get(letter, letter) for letter in text) | ||
6 | 6 | ||||
7 | 7 | ||||
8 | def rotor(text, rotor_position): | 8 | def rotor(text, rotor_position): | ||
9 | return ''.join(rotor_position.get(letter, letter) for letter in text) | 9 | return ''.join(rotor_position.get(letter, letter) for letter in text) | ||
10 | 10 | ||||
11 | 11 | ||||
12 | def reversed_rotor(rotor_position): | 12 | def reversed_rotor(rotor_position): | ||
13 | return {value: key for key, value in rotor_position.items()} | 13 | return {value: key for key, value in rotor_position.items()} | ||
14 | 14 | ||||
15 | 15 | ||||
16 | def enigma_encrypt(plugboard_position=[], rotor_position={}): | 16 | def enigma_encrypt(plugboard_position=[], rotor_position={}): | ||
17 | def decorator(func): | 17 | def decorator(func): | ||
18 | def wrapper(text): | 18 | def wrapper(text): | ||
19 | encrypted_text = plugboard(text, plugboard_position) | 19 | encrypted_text = plugboard(text, plugboard_position) | ||
20 | encrypted_text = rotor(encrypted_text, rotor_position) | 20 | encrypted_text = rotor(encrypted_text, rotor_position) | ||
21 | return func(encrypted_text) | 21 | return func(encrypted_text) | ||
22 | return wrapper | 22 | return wrapper | ||
23 | return decorator | 23 | return decorator | ||
24 | 24 | ||||
25 | 25 | ||||
26 | def enigma_decrypt(plugboard_position=[], rotor_position={}): | 26 | def enigma_decrypt(plugboard_position=[], rotor_position={}): | ||
27 | reversed_rotor_mapping = reversed_rotor(rotor_position) | 27 | reversed_rotor_mapping = reversed_rotor(rotor_position) | ||
28 | 28 | ||||
29 | def decorator(func): | 29 | def decorator(func): | ||
30 | def wrapper(text): | 30 | def wrapper(text): | ||
31 | decrypted_text = rotor(text, reversed_rotor_mapping) | 31 | decrypted_text = rotor(text, reversed_rotor_mapping) | ||
32 | decrypted_text = plugboard(decrypted_text, plugboard_position) | 32 | decrypted_text = plugboard(decrypted_text, plugboard_position) | ||
33 | return func(decrypted_text) | 33 | return func(decrypted_text) | ||
34 | return wrapper | 34 | return wrapper | ||
35 | return decorator | 35 | return decorator | ||
t | 36 | t | |||
37 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | ||||
38 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||||
39 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||||
40 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||||
41 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||||
42 | 'm': 'r', 'c': 'k'} |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def plugboard(text, plugboard_position): | f | 1 | def plugboard(text, plugboard_position): |
2 | letter_mapping = {} | 2 | letter_mapping = {} | ||
n | 3 | for pair in plugboard_position: | n | 3 | for a, b in plugboard_position: |
4 | a, b = pair | ||||
5 | letter_mapping[a], letter_mapping[b] = b, a | 4 | letter_mapping[a], letter_mapping[b] = b, a | ||
6 | return ''.join(letter_mapping.get(letter, letter) for letter in text) | 5 | return ''.join(letter_mapping.get(letter, letter) for letter in text) | ||
7 | 6 | ||||
8 | 7 | ||||
9 | def rotor(text, rotor_position): | 8 | def rotor(text, rotor_position): | ||
10 | return ''.join(rotor_position.get(letter, letter) for letter in text) | 9 | return ''.join(rotor_position.get(letter, letter) for letter in text) | ||
11 | 10 | ||||
12 | 11 | ||||
13 | def reversed_rotor(rotor_position): | 12 | def reversed_rotor(rotor_position): | ||
14 | return {value: key for key, value in rotor_position.items()} | 13 | return {value: key for key, value in rotor_position.items()} | ||
15 | 14 | ||||
16 | 15 | ||||
n | 17 | def enigma_encrypt(*, plugboard_position, rotor_position): | n | 16 | def enigma_encrypt(plugboard_position=[], rotor_position={}): |
18 | def decorator(func): | 17 | def decorator(func): | ||
19 | def wrapper(text): | 18 | def wrapper(text): | ||
20 | encrypted_text = plugboard(text, plugboard_position) | 19 | encrypted_text = plugboard(text, plugboard_position) | ||
21 | encrypted_text = rotor(encrypted_text, rotor_position) | 20 | encrypted_text = rotor(encrypted_text, rotor_position) | ||
22 | return func(encrypted_text) | 21 | return func(encrypted_text) | ||
23 | return wrapper | 22 | return wrapper | ||
24 | return decorator | 23 | return decorator | ||
25 | 24 | ||||
26 | 25 | ||||
n | 27 | def enigma_decrypt(*, plugboard_position, rotor_position): | n | 26 | def enigma_decrypt(plugboard_position=[], rotor_position={}): |
28 | reversed_rotor_mapping = reversed_rotor(rotor_position) | 27 | reversed_rotor_mapping = reversed_rotor(rotor_position) | ||
29 | 28 | ||||
30 | def decorator(func): | 29 | def decorator(func): | ||
31 | def wrapper(text): | 30 | def wrapper(text): | ||
32 | decrypted_text = rotor(text, reversed_rotor_mapping) | 31 | decrypted_text = rotor(text, reversed_rotor_mapping) | ||
33 | decrypted_text = plugboard(decrypted_text, plugboard_position) | 32 | decrypted_text = plugboard(decrypted_text, plugboard_position) | ||
34 | return func(decrypted_text) | 33 | return func(decrypted_text) | ||
35 | return wrapper | 34 | return wrapper | ||
36 | return decorator | 35 | return decorator | ||
t | t | 36 | |||
37 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | ||||
38 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||||
39 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||||
40 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||||
41 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||||
42 | 'm': 'r', 'c': 'k'} |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|