1def plugboard(uncoded: str, plugboard_pos: list):
2 return encode_with_dict(uncoded, convert_to_dictionary(plugboard_pos))
3
4
5def rotor(uncoded: str, from_to_dict: dict):
6 return encode_with_dict(uncoded, from_to_dict)
7
8
9def enigma_encrypt(plugboard_position: list, rotor_position: dict):
10 def encrypting_decorator(func):
11 def encrypt_and_run_func(text):
12 text = rotor(plugboard(text, plugboard_position), rotor_position)
13 return func(text)
14 return encrypt_and_run_func
15 return encrypting_decorator
16
17
18def enigma_decrypt(plugboard_position: list, rotor_position: dict):
19 def encrypting_decorator(func):
20 def decrypt_and_run_func(text):
21 reverse_rotor_position = rotate_rotor_positions(rotor_position)
22 text = plugboard(rotor(text, reverse_rotor_position), plugboard_position)
23 return func(text)
24 return decrypt_and_run_func
25 return encrypting_decorator
26
27
28def encode_with_dict(uncoded: str, from_to_dict: dict):
29 encoded = ''
30
31 for char in uncoded:
32 if char in from_to_dict:
33 encoded += from_to_dict[char]
34 else:
35 encoded += char
36 return encoded
37
38
39def convert_to_dictionary(plugboard_pos: list):
40 from_to_dictionary = {}
41
42 for from_to_set in plugboard_pos:
43 pair = list(from_to_set)
44 from_to_dictionary[pair[0]] = pair[1]
45 from_to_dictionary[pair[1]] = pair[0]
46 return from_to_dictionary
47
48
49def rotate_rotor_positions(rotor_positions: dict):
50 new_rotation = {}
51
52 for key, value in rotor_positions.items():
53 new_rotation[value] = key
54 return new_rotation
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
f | 1 | f | 1 | ||
2 | 2 | ||||
n | 3 | def plugboard(uncoded : str, plugboard_pos : list): | n | 3 | def plugboard(uncoded: str, plugboard_pos: list): |
4 | return encode_with_dict(uncoded, convert_to_dictionary(plugboard_pos)) | 4 | return encode_with_dict(uncoded, convert_to_dictionary(plugboard_pos)) | ||
5 | 5 | ||||
6 | 6 | ||||
n | 7 | def rotor(uncoded : str, from_to_dict : dict): | n | 7 | def rotor(uncoded: str, from_to_dict: dict): |
8 | return encode_with_dict(uncoded, from_to_dict) | 8 | return encode_with_dict(uncoded, from_to_dict) | ||
9 | 9 | ||||
10 | 10 | ||||
n | 11 | def enigma_encrypt(plugboard_position : list, rotor_position : dict): | n | 11 | def enigma_encrypt(plugboard_position: list, rotor_position: dict): |
12 | def encrypting_decorator(func): | 12 | def encrypting_decorator(func): | ||
n | 13 | def encrypt_and_run_func(str): | n | 13 | def encrypt_and_run_func(text): |
14 | str = rotor( | 14 | text = rotor(plugboard(text, plugboard_position), rotor_position) | ||
15 | plugboard(str, plugboard_position), | ||||
16 | rotor_position) | ||||
17 | return func(str) | 15 | return func(text) | ||
18 | return encrypt_and_run_func | 16 | return encrypt_and_run_func | ||
19 | return encrypting_decorator | 17 | return encrypting_decorator | ||
20 | 18 | ||||
21 | 19 | ||||
n | 22 | def enigma_decrypt(plugboard_position : list, rotor_position : dict): | n | 20 | def enigma_decrypt(plugboard_position: list, rotor_position: dict): |
23 | def encrypting_decorator(func): | 21 | def encrypting_decorator(func): | ||
n | 24 | def decrypt_and_run_func(str): | n | 22 | def decrypt_and_run_func(text): |
25 | reverse_rotor_position = rotate_rotor_positions(rotor_position) | 23 | reverse_rotor_position = rotate_rotor_positions(rotor_position) | ||
n | 26 | str = plugboard( | n | 24 | text = plugboard(rotor(text, reverse_rotor_position), plugboard_position) |
27 | rotor(str, reverse_rotor_position), | ||||
28 | plugboard_position) | ||||
29 | return func(str) | 25 | return func(text) | ||
30 | return decrypt_and_run_func | 26 | return decrypt_and_run_func | ||
31 | return encrypting_decorator | 27 | return encrypting_decorator | ||
32 | 28 | ||||
33 | 29 | ||||
n | 34 | def encode_with_dict(uncoded : str, from_to_dict : dict): | n | 30 | def encode_with_dict(uncoded: str, from_to_dict: dict): |
35 | encoded = '' | 31 | encoded = '' | ||
36 | 32 | ||||
37 | for char in uncoded: | 33 | for char in uncoded: | ||
38 | if char in from_to_dict: | 34 | if char in from_to_dict: | ||
39 | encoded += from_to_dict[char] | 35 | encoded += from_to_dict[char] | ||
40 | else: | 36 | else: | ||
41 | encoded += char | 37 | encoded += char | ||
42 | return encoded | 38 | return encoded | ||
43 | 39 | ||||
44 | 40 | ||||
n | 45 | def convert_to_dictionary(plugboard_pos): | n | 41 | def convert_to_dictionary(plugboard_pos: list): |
46 | from_to_dictionary = {} | 42 | from_to_dictionary = {} | ||
47 | 43 | ||||
48 | for from_to_set in plugboard_pos: | 44 | for from_to_set in plugboard_pos: | ||
49 | pair = list(from_to_set) | 45 | pair = list(from_to_set) | ||
50 | from_to_dictionary[pair[0]] = pair[1] | 46 | from_to_dictionary[pair[0]] = pair[1] | ||
51 | from_to_dictionary[pair[1]] = pair[0] | 47 | from_to_dictionary[pair[1]] = pair[0] | ||
52 | return from_to_dictionary | 48 | return from_to_dictionary | ||
53 | 49 | ||||
54 | 50 | ||||
t | 55 | def rotate_rotor_positions(rotor_positions : dict): | t | 51 | def rotate_rotor_positions(rotor_positions: dict): |
56 | new_rotation = {} | 52 | new_rotation = {} | ||
57 | 53 | ||||
58 | for key, value in rotor_positions.items(): | 54 | for key, value in rotor_positions.items(): | ||
59 | new_rotation[value] = key | 55 | new_rotation[value] = key | ||
60 | return new_rotation | 56 | return new_rotation |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | f | 1 | ||
2 | 2 | ||||
3 | def plugboard(uncoded : str, plugboard_pos : list): | 3 | def plugboard(uncoded : str, plugboard_pos : list): | ||
4 | return encode_with_dict(uncoded, convert_to_dictionary(plugboard_pos)) | 4 | return encode_with_dict(uncoded, convert_to_dictionary(plugboard_pos)) | ||
5 | 5 | ||||
6 | 6 | ||||
7 | def rotor(uncoded : str, from_to_dict : dict): | 7 | def rotor(uncoded : str, from_to_dict : dict): | ||
8 | return encode_with_dict(uncoded, from_to_dict) | 8 | return encode_with_dict(uncoded, from_to_dict) | ||
9 | 9 | ||||
10 | 10 | ||||
11 | def enigma_encrypt(plugboard_position : list, rotor_position : dict): | 11 | def enigma_encrypt(plugboard_position : list, rotor_position : dict): | ||
12 | def encrypting_decorator(func): | 12 | def encrypting_decorator(func): | ||
13 | def encrypt_and_run_func(str): | 13 | def encrypt_and_run_func(str): | ||
n | 14 | str = rotor(plugboard(str, plugboard_position), rotor_position) | n | 14 | str = rotor( |
15 | plugboard(str, plugboard_position), | ||||
16 | rotor_position) | ||||
15 | return func(str) | 17 | return func(str) | ||
16 | return encrypt_and_run_func | 18 | return encrypt_and_run_func | ||
17 | return encrypting_decorator | 19 | return encrypting_decorator | ||
18 | 20 | ||||
19 | 21 | ||||
20 | def enigma_decrypt(plugboard_position : list, rotor_position : dict): | 22 | def enigma_decrypt(plugboard_position : list, rotor_position : dict): | ||
21 | def encrypting_decorator(func): | 23 | def encrypting_decorator(func): | ||
22 | def decrypt_and_run_func(str): | 24 | def decrypt_and_run_func(str): | ||
23 | reverse_rotor_position = rotate_rotor_positions(rotor_position) | 25 | reverse_rotor_position = rotate_rotor_positions(rotor_position) | ||
24 | str = plugboard( | 26 | str = plugboard( | ||
25 | rotor(str, reverse_rotor_position), | 27 | rotor(str, reverse_rotor_position), | ||
26 | plugboard_position) | 28 | plugboard_position) | ||
27 | return func(str) | 29 | return func(str) | ||
28 | return decrypt_and_run_func | 30 | return decrypt_and_run_func | ||
29 | return encrypting_decorator | 31 | return encrypting_decorator | ||
30 | 32 | ||||
31 | 33 | ||||
32 | def encode_with_dict(uncoded : str, from_to_dict : dict): | 34 | def encode_with_dict(uncoded : str, from_to_dict : dict): | ||
33 | encoded = '' | 35 | encoded = '' | ||
n | n | 36 | |||
34 | for char in uncoded: | 37 | for char in uncoded: | ||
35 | if char in from_to_dict: | 38 | if char in from_to_dict: | ||
36 | encoded += from_to_dict[char] | 39 | encoded += from_to_dict[char] | ||
37 | else: | 40 | else: | ||
38 | encoded += char | 41 | encoded += char | ||
39 | return encoded | 42 | return encoded | ||
40 | 43 | ||||
41 | 44 | ||||
42 | def convert_to_dictionary(plugboard_pos): | 45 | def convert_to_dictionary(plugboard_pos): | ||
43 | from_to_dictionary = {} | 46 | from_to_dictionary = {} | ||
44 | 47 | ||||
45 | for from_to_set in plugboard_pos: | 48 | for from_to_set in plugboard_pos: | ||
46 | pair = list(from_to_set) | 49 | pair = list(from_to_set) | ||
47 | from_to_dictionary[pair[0]] = pair[1] | 50 | from_to_dictionary[pair[0]] = pair[1] | ||
48 | from_to_dictionary[pair[1]] = pair[0] | 51 | from_to_dictionary[pair[1]] = pair[0] | ||
49 | return from_to_dictionary | 52 | return from_to_dictionary | ||
50 | 53 | ||||
51 | 54 | ||||
52 | def rotate_rotor_positions(rotor_positions : dict): | 55 | def rotate_rotor_positions(rotor_positions : dict): | ||
n | 53 | n | |||
54 | new_rotation = {} | 56 | new_rotation = {} | ||
55 | 57 | ||||
56 | for key, value in rotor_positions.items(): | 58 | for key, value in rotor_positions.items(): | ||
57 | new_rotation[value] = key | 59 | new_rotation[value] = key | ||
t | 58 | t | |||
59 | return new_rotation | 60 | return new_rotation |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | t | 1 | ||
2 | 2 | ||||
3 | def plugboard(uncoded : str, plugboard_pos : list): | 3 | def plugboard(uncoded : str, plugboard_pos : list): | ||
4 | return encode_with_dict(uncoded, convert_to_dictionary(plugboard_pos)) | 4 | return encode_with_dict(uncoded, convert_to_dictionary(plugboard_pos)) | ||
5 | 5 | ||||
6 | 6 | ||||
7 | def rotor(uncoded : str, from_to_dict : dict): | 7 | def rotor(uncoded : str, from_to_dict : dict): | ||
8 | return encode_with_dict(uncoded, from_to_dict) | 8 | return encode_with_dict(uncoded, from_to_dict) | ||
9 | 9 | ||||
10 | 10 | ||||
11 | def enigma_encrypt(plugboard_position : list, rotor_position : dict): | 11 | def enigma_encrypt(plugboard_position : list, rotor_position : dict): | ||
12 | def encrypting_decorator(func): | 12 | def encrypting_decorator(func): | ||
13 | def encrypt_and_run_func(str): | 13 | def encrypt_and_run_func(str): | ||
14 | str = rotor(plugboard(str, plugboard_position), rotor_position) | 14 | str = rotor(plugboard(str, plugboard_position), rotor_position) | ||
15 | return func(str) | 15 | return func(str) | ||
16 | return encrypt_and_run_func | 16 | return encrypt_and_run_func | ||
17 | return encrypting_decorator | 17 | return encrypting_decorator | ||
18 | 18 | ||||
19 | 19 | ||||
20 | def enigma_decrypt(plugboard_position : list, rotor_position : dict): | 20 | def enigma_decrypt(plugboard_position : list, rotor_position : dict): | ||
21 | def encrypting_decorator(func): | 21 | def encrypting_decorator(func): | ||
22 | def decrypt_and_run_func(str): | 22 | def decrypt_and_run_func(str): | ||
23 | reverse_rotor_position = rotate_rotor_positions(rotor_position) | 23 | reverse_rotor_position = rotate_rotor_positions(rotor_position) | ||
24 | str = plugboard( | 24 | str = plugboard( | ||
25 | rotor(str, reverse_rotor_position), | 25 | rotor(str, reverse_rotor_position), | ||
26 | plugboard_position) | 26 | plugboard_position) | ||
27 | return func(str) | 27 | return func(str) | ||
28 | return decrypt_and_run_func | 28 | return decrypt_and_run_func | ||
29 | return encrypting_decorator | 29 | return encrypting_decorator | ||
30 | 30 | ||||
31 | 31 | ||||
32 | def encode_with_dict(uncoded : str, from_to_dict : dict): | 32 | def encode_with_dict(uncoded : str, from_to_dict : dict): | ||
33 | encoded = '' | 33 | encoded = '' | ||
34 | for char in uncoded: | 34 | for char in uncoded: | ||
35 | if char in from_to_dict: | 35 | if char in from_to_dict: | ||
36 | encoded += from_to_dict[char] | 36 | encoded += from_to_dict[char] | ||
37 | else: | 37 | else: | ||
38 | encoded += char | 38 | encoded += char | ||
39 | return encoded | 39 | return encoded | ||
40 | 40 | ||||
41 | 41 | ||||
42 | def convert_to_dictionary(plugboard_pos): | 42 | def convert_to_dictionary(plugboard_pos): | ||
43 | from_to_dictionary = {} | 43 | from_to_dictionary = {} | ||
44 | 44 | ||||
45 | for from_to_set in plugboard_pos: | 45 | for from_to_set in plugboard_pos: | ||
46 | pair = list(from_to_set) | 46 | pair = list(from_to_set) | ||
47 | from_to_dictionary[pair[0]] = pair[1] | 47 | from_to_dictionary[pair[0]] = pair[1] | ||
48 | from_to_dictionary[pair[1]] = pair[0] | 48 | from_to_dictionary[pair[1]] = pair[0] | ||
49 | return from_to_dictionary | 49 | return from_to_dictionary | ||
50 | 50 | ||||
51 | 51 | ||||
52 | def rotate_rotor_positions(rotor_positions : dict): | 52 | def rotate_rotor_positions(rotor_positions : dict): | ||
53 | 53 | ||||
54 | new_rotation = {} | 54 | new_rotation = {} | ||
55 | 55 | ||||
56 | for key, value in rotor_positions.items(): | 56 | for key, value in rotor_positions.items(): | ||
57 | new_rotation[value] = key | 57 | new_rotation[value] = key | ||
58 | 58 | ||||
59 | return new_rotation | 59 | return new_rotation |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|