1"""Solution NOT based on the fact that there are only lowercase letters"""
2
3
4def replace_text(text, dictionary):
5 return "".join([dictionary.get(char, char) for char in text])
6
7
8def invert_dictionary(dictionary):
9 return {value: key for key, value in dictionary.items()}
10
11
12def plugboard(message, plugboard_position):
13 plugboard_dictionary = dict(plugboard_position)
14 plugboard_dictionary |= invert_dictionary(plugboard_dictionary)
15 return replace_text(message, plugboard_dictionary)
16
17
18def rotor(message, rotor_position):
19 return replace_text(message, rotor_position)
20
21
22def enigma_encrypt(plugboard_position, rotor_position):
23 def decorator(function):
24 def wrapper(text):
25 text_plugboard = plugboard(text, plugboard_position)
26 text_rotor = rotor(text_plugboard, rotor_position)
27 return function(text_rotor)
28
29 return wrapper
30
31 return decorator
32
33
34def enigma_decrypt(plugboard_position, rotor_position):
35 def decorator(function):
36 def wrapper(text):
37 inverted_rotor_position = invert_dictionary(rotor_position)
38 text_rotor = rotor(text, inverted_rotor_position)
39 text_plugboard = plugboard(text_rotor, plugboard_position)
40 return function(text_plugboard)
41
42 return wrapper
43
44 return decorator
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
| f | 1 | """Solution NOT based on the fact that there are only lowercase letters""" | f | 1 | """Solution NOT based on the fact that there are only lowercase letters""" |
| 2 | 2 | ||||
| 3 | 3 | ||||
| 4 | def replace_text(text, dictionary): | 4 | def replace_text(text, dictionary): | ||
| t | 5 | return "".join( | t | 5 | return "".join([dictionary.get(char, char) for char in text]) |
| 6 | [ | ||||
| 7 | dictionary[old_char] if old_char in dictionary.keys() else old_char | ||||
| 8 | for old_char in text | ||||
| 9 | ] | ||||
| 10 | ) | ||||
| 11 | 6 | ||||
| 12 | 7 | ||||
| 13 | def invert_dictionary(dictionary): | 8 | def invert_dictionary(dictionary): | ||
| 14 | return {value: key for key, value in dictionary.items()} | 9 | return {value: key for key, value in dictionary.items()} | ||
| 15 | 10 | ||||
| 16 | 11 | ||||
| 17 | def plugboard(message, plugboard_position): | 12 | def plugboard(message, plugboard_position): | ||
| 18 | plugboard_dictionary = dict(plugboard_position) | 13 | plugboard_dictionary = dict(plugboard_position) | ||
| 19 | plugboard_dictionary |= invert_dictionary(plugboard_dictionary) | 14 | plugboard_dictionary |= invert_dictionary(plugboard_dictionary) | ||
| 20 | return replace_text(message, plugboard_dictionary) | 15 | return replace_text(message, plugboard_dictionary) | ||
| 21 | 16 | ||||
| 22 | 17 | ||||
| 23 | def rotor(message, rotor_position): | 18 | def rotor(message, rotor_position): | ||
| 24 | return replace_text(message, rotor_position) | 19 | return replace_text(message, rotor_position) | ||
| 25 | 20 | ||||
| 26 | 21 | ||||
| 27 | def enigma_encrypt(plugboard_position, rotor_position): | 22 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 28 | def decorator(function): | 23 | def decorator(function): | ||
| 29 | def wrapper(text): | 24 | def wrapper(text): | ||
| 30 | text_plugboard = plugboard(text, plugboard_position) | 25 | text_plugboard = plugboard(text, plugboard_position) | ||
| 31 | text_rotor = rotor(text_plugboard, rotor_position) | 26 | text_rotor = rotor(text_plugboard, rotor_position) | ||
| 32 | return function(text_rotor) | 27 | return function(text_rotor) | ||
| 33 | 28 | ||||
| 34 | return wrapper | 29 | return wrapper | ||
| 35 | 30 | ||||
| 36 | return decorator | 31 | return decorator | ||
| 37 | 32 | ||||
| 38 | 33 | ||||
| 39 | def enigma_decrypt(plugboard_position, rotor_position): | 34 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 40 | def decorator(function): | 35 | def decorator(function): | ||
| 41 | def wrapper(text): | 36 | def wrapper(text): | ||
| 42 | inverted_rotor_position = invert_dictionary(rotor_position) | 37 | inverted_rotor_position = invert_dictionary(rotor_position) | ||
| 43 | text_rotor = rotor(text, inverted_rotor_position) | 38 | text_rotor = rotor(text, inverted_rotor_position) | ||
| 44 | text_plugboard = plugboard(text_rotor, plugboard_position) | 39 | text_plugboard = plugboard(text_rotor, plugboard_position) | ||
| 45 | return function(text_plugboard) | 40 | return function(text_plugboard) | ||
| 46 | 41 | ||||
| 47 | return wrapper | 42 | return wrapper | ||
| 48 | 43 | ||||
| 49 | return decorator | 44 | return decorator |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| n | 1 | """Solution based on the fact that there are only lowercase letters""" | n | 1 | """Solution NOT based on the fact that there are only lowercase letters""" |
| 2 | 2 | ||||
| 3 | 3 | ||||
| n | 4 | def replace_with_upper_case(text, old_char, new_char): | n | 4 | def replace_text(text, dictionary): |
| 5 | return text.replace(old_char, new_char.capitalize()) | 5 | return "".join( | ||
| 6 | [ | ||||
| 7 | dictionary[old_char] if old_char in dictionary.keys() else old_char | ||||
| 8 | for old_char in text | ||||
| 9 | ] | ||||
| 10 | ) | ||||
| 11 | |||||
| 12 | |||||
| 13 | def invert_dictionary(dictionary): | ||||
| 14 | return {value: key for key, value in dictionary.items()} | ||||
| 6 | 15 | ||||
| 7 | 16 | ||||
| 8 | def plugboard(message, plugboard_position): | 17 | def plugboard(message, plugboard_position): | ||
| n | 9 | output = message | n | 18 | plugboard_dictionary = dict(plugboard_position) |
| 10 | 19 | plugboard_dictionary |= invert_dictionary(plugboard_dictionary) | |||
| 11 | for left_char, right_char in plugboard_position: | 20 | return replace_text(message, plugboard_dictionary) | ||
| 12 | output = replace_with_upper_case(output, left_char, right_char) | ||||
| 13 | output = replace_with_upper_case(output, right_char, left_char) | ||||
| 14 | |||||
| 15 | return output.lower() | ||||
| 16 | 21 | ||||
| 17 | 22 | ||||
| 18 | def rotor(message, rotor_position): | 23 | def rotor(message, rotor_position): | ||
| n | 19 | output = message | n | 24 | return replace_text(message, rotor_position) |
| 20 | |||||
| 21 | for key, value in rotor_position.items(): | ||||
| 22 | output = replace_with_upper_case(output, key, value) | ||||
| 23 | |||||
| 24 | return output.lower() | ||||
| 25 | 25 | ||||
| 26 | 26 | ||||
| 27 | def enigma_encrypt(plugboard_position, rotor_position): | 27 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 28 | def decorator(function): | 28 | def decorator(function): | ||
| 29 | def wrapper(text): | 29 | def wrapper(text): | ||
| 30 | text_plugboard = plugboard(text, plugboard_position) | 30 | text_plugboard = plugboard(text, plugboard_position) | ||
| 31 | text_rotor = rotor(text_plugboard, rotor_position) | 31 | text_rotor = rotor(text_plugboard, rotor_position) | ||
| 32 | return function(text_rotor) | 32 | return function(text_rotor) | ||
| 33 | 33 | ||||
| 34 | return wrapper | 34 | return wrapper | ||
| 35 | 35 | ||||
| 36 | return decorator | 36 | return decorator | ||
| 37 | 37 | ||||
| 38 | 38 | ||||
| t | 39 | def invert_dictionary(dictionary): | t | ||
| 40 | return {value: key for key, value in dictionary.items()} | ||||
| 41 | |||||
| 42 | |||||
| 43 | def enigma_decrypt(plugboard_position, rotor_position): | 39 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 44 | def decorator(function): | 40 | def decorator(function): | ||
| 45 | def wrapper(text): | 41 | def wrapper(text): | ||
| 46 | inverted_rotor_position = invert_dictionary(rotor_position) | 42 | inverted_rotor_position = invert_dictionary(rotor_position) | ||
| 47 | text_rotor = rotor(text, inverted_rotor_position) | 43 | text_rotor = rotor(text, inverted_rotor_position) | ||
| 48 | text_plugboard = plugboard(text_rotor, plugboard_position) | 44 | text_plugboard = plugboard(text_rotor, plugboard_position) | ||
| 49 | return function(text_plugboard) | 45 | return function(text_plugboard) | ||
| 50 | 46 | ||||
| 51 | return wrapper | 47 | return wrapper | ||
| 52 | 48 | ||||
| 53 | return decorator | 49 | return decorator |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||