1def plugboard(text, plugboard_position):
2 output = ''
3 for char in text:
4 for flip in plugboard_position:
5 if char in flip:
6 output += (flip - {char}).pop()
7 break
8 else:
9 output += char
10 return output
11
12
13def rotor(text, rotor_position):
14 rotor_position[' '] = ' '
15 return ''.join(map(lambda char: rotor_position[char] or char, text))
16
17
18def enigma_encrypt(plugboard_position, rotor_position):
19 def decorator(f):
20 def decorated(text):
21 flipped = plugboard(text, plugboard_position)
22 rotated = rotor(flipped, rotor_position)
23 return f(rotated)
24 return decorated
25 return decorator
26
27
28def enigma_decrypt(plugboard_position, rotor_position):
29 rotor_position = {
30 value: key for key, value in rotor_position.items()
31 }
32 def decorator(f):
33 def decorated(text):
34 rotated = rotor(text, rotor_position)
35 flipped = plugboard(rotated, plugboard_position)
36 return f(flipped)
37 return decorated
38 return decorator
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
t | 1 | def plugboard(text, plugboard_position): | t | 1 | def plugboard(text, plugboard_position): |
2 | output = '' | 2 | output = '' | ||
3 | for char in text: | 3 | for char in text: | ||
4 | for flip in plugboard_position: | 4 | for flip in plugboard_position: | ||
5 | if char in flip: | 5 | if char in flip: | ||
6 | output += (flip - {char}).pop() | 6 | output += (flip - {char}).pop() | ||
7 | break | 7 | break | ||
8 | else: | 8 | else: | ||
9 | output += char | 9 | output += char | ||
10 | return output | 10 | return output | ||
11 | 11 | ||||
12 | 12 | ||||
13 | def rotor(text, rotor_position): | 13 | def rotor(text, rotor_position): | ||
14 | rotor_position[' '] = ' ' | 14 | rotor_position[' '] = ' ' | ||
15 | return ''.join(map(lambda char: rotor_position[char] or char, text)) | 15 | return ''.join(map(lambda char: rotor_position[char] or char, text)) | ||
16 | 16 | ||||
17 | 17 | ||||
18 | def enigma_encrypt(plugboard_position, rotor_position): | 18 | def enigma_encrypt(plugboard_position, rotor_position): | ||
19 | def decorator(f): | 19 | def decorator(f): | ||
20 | def decorated(text): | 20 | def decorated(text): | ||
21 | flipped = plugboard(text, plugboard_position) | 21 | flipped = plugboard(text, plugboard_position) | ||
22 | rotated = rotor(flipped, rotor_position) | 22 | rotated = rotor(flipped, rotor_position) | ||
23 | return f(rotated) | 23 | return f(rotated) | ||
24 | return decorated | 24 | return decorated | ||
25 | return decorator | 25 | return decorator | ||
26 | 26 | ||||
27 | 27 | ||||
28 | def enigma_decrypt(plugboard_position, rotor_position): | 28 | def enigma_decrypt(plugboard_position, rotor_position): | ||
29 | rotor_position = { | 29 | rotor_position = { | ||
30 | value: key for key, value in rotor_position.items() | 30 | value: key for key, value in rotor_position.items() | ||
31 | } | 31 | } | ||
32 | def decorator(f): | 32 | def decorator(f): | ||
33 | def decorated(text): | 33 | def decorated(text): | ||
34 | rotated = rotor(text, rotor_position) | 34 | rotated = rotor(text, rotor_position) | ||
35 | flipped = plugboard(rotated, plugboard_position) | 35 | flipped = plugboard(rotated, plugboard_position) | ||
36 | return f(flipped) | 36 | return f(flipped) | ||
37 | return decorated | 37 | return decorated | ||
38 | return decorator | 38 | return decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | def plugboard(text, plugboard_position): | t | 1 | def plugboard(text, plugboard_position): |
2 | output = '' | 2 | output = '' | ||
3 | for char in text: | 3 | for char in text: | ||
4 | for flip in plugboard_position: | 4 | for flip in plugboard_position: | ||
5 | if char in flip: | 5 | if char in flip: | ||
6 | output += (flip - {char}).pop() | 6 | output += (flip - {char}).pop() | ||
7 | break | 7 | break | ||
8 | else: | 8 | else: | ||
9 | output += char | 9 | output += char | ||
10 | return output | 10 | return output | ||
11 | 11 | ||||
12 | 12 | ||||
13 | def rotor(text, rotor_position): | 13 | def rotor(text, rotor_position): | ||
14 | rotor_position[' '] = ' ' | 14 | rotor_position[' '] = ' ' | ||
15 | return ''.join(map(lambda char: rotor_position[char] or char, text)) | 15 | return ''.join(map(lambda char: rotor_position[char] or char, text)) | ||
16 | 16 | ||||
17 | 17 | ||||
18 | def enigma_encrypt(plugboard_position, rotor_position): | 18 | def enigma_encrypt(plugboard_position, rotor_position): | ||
19 | def decorator(f): | 19 | def decorator(f): | ||
20 | def decorated(text): | 20 | def decorated(text): | ||
21 | flipped = plugboard(text, plugboard_position) | 21 | flipped = plugboard(text, plugboard_position) | ||
22 | rotated = rotor(flipped, rotor_position) | 22 | rotated = rotor(flipped, rotor_position) | ||
23 | return f(rotated) | 23 | return f(rotated) | ||
24 | return decorated | 24 | return decorated | ||
25 | return decorator | 25 | return decorator | ||
26 | 26 | ||||
27 | 27 | ||||
28 | def enigma_decrypt(plugboard_position, rotor_position): | 28 | def enigma_decrypt(plugboard_position, rotor_position): | ||
29 | rotor_position = { | 29 | rotor_position = { | ||
30 | value: key for key, value in rotor_position.items() | 30 | value: key for key, value in rotor_position.items() | ||
31 | } | 31 | } | ||
32 | def decorator(f): | 32 | def decorator(f): | ||
33 | def decorated(text): | 33 | def decorated(text): | ||
34 | rotated = rotor(text, rotor_position) | 34 | rotated = rotor(text, rotor_position) | ||
35 | flipped = plugboard(rotated, plugboard_position) | 35 | flipped = plugboard(rotated, plugboard_position) | ||
36 | return f(flipped) | 36 | return f(flipped) | ||
37 | return decorated | 37 | return decorated | ||
38 | return decorator | 38 | return decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|