1def plugboard(string_to_encode: str, plugboard_position: list):
2 """Encode a String based on the Pairs of Chars in the List of Sets.
3
4 This Function creates a Dict from the List of Pairs and extends it with the Swapped Pairs.
5 This way it can use the rotor Function.
6 """
7
8 tuple_pairs = list([tuple(curr_set) for curr_set in plugboard_position])
9 tuple_pairs.extend([(y, x) for x, y in tuple_pairs])
10 encoding_dict = dict(tuple_pairs)
11
12 return rotor(string_to_encode, encoding_dict)
13
14
15def rotor(string_to_encode: str, rotor_position: dict):
16 """Encodes a String using the Key, Value Pairs in the Dict."""
17 return "".join([rotor_position.get(ch, ch) for ch in string_to_encode])
18
19
20def enigma_encrypt(plugboard_position: list, rotor_position: dict):
21 """Create a Decorator for a Function with the given parameters using the Functions plugboard and rotor."""
22
23 def enigma_encrypt_decorator(func):
24 """Decorate an Encrypting Function that excepts a single String, to encrypt the String before calling it."""
25
26 def decorated_func(s: str):
27 """Encrypt a String before calling a Function."""
28 return func(rotor(plugboard(s, plugboard_position), rotor_position))
29
30 return decorated_func
31
32 return enigma_encrypt_decorator
33
34
35def enigma_decrypt(plugboard_position: list, rotor_position: dict):
36 """Create a Decrypting Decorator for a Function with the given positions using the Functions plugboard and rotor."""
37 rotor_position = {y: x for x, y in rotor_position.items()}
38
39 def enigma_decrypt_decorator(func):
40 """Decorate a Function that excepts a single String, to decrypt the String before calling it."""
41
42 def decorated_func(s: str):
43 """Decrypt a String before calling a Function."""
44 return func(plugboard(rotor(s, rotor_position), plugboard_position))
45
46 return decorated_func
47
48 return enigma_decrypt_decorator
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
Георги Кунчев
25.10.2023 13:04Да, да, наистина е така. Моя грешка. Можеш да пренебрегнеш коментара ми на 8 ред.
|
Филип Филчев
25.10.2023 11:13В условието се казва, че на plugboard се подава list of sets с 2 елемента. Затова в началото го превръщам в tuples. Не разбирам първия ти коментар. Може ли пояснение?
|
Георги Кунчев
25.10.2023 10:02Не е проблем да ги типизираш. Така или иначе, ти не ги типизираш, а само ги хинтваш. Все още могат да се подават аргументи от всякакъв тип и това няма да доведе до грешка. Просто помогаш на IDE-то и другит програмисти с това.
Като цяло имаш много добро и чисто решение. Коментарите ми са само неща, които могат, според мен, да го направят една идея по-чисто, но решението е добро и така.
|
Филип Филчев
24.10.2023 23:57Типизирах си параметрите, за да може IDE-то да ми подсказва с методи. Не знам дали е добре да се прави. Ако не е ще ги махна.
|
f | 1 | def plugboard(string_to_encode: str, plugboard_position: list): | f | 1 | def plugboard(string_to_encode: str, plugboard_position: list): |
2 | """Encode a String based on the Pairs of Chars in the List of Sets. | 2 | """Encode a String based on the Pairs of Chars in the List of Sets. | ||
3 | 3 | ||||
4 | This Function creates a Dict from the List of Pairs and extends it with the Swapped Pairs. | 4 | This Function creates a Dict from the List of Pairs and extends it with the Swapped Pairs. | ||
n | 5 | This way it can use the rotor Function with the Plugboard's Position acting like a Rotor Position | n | 5 | This way it can use the rotor Function. |
6 | """ | 6 | """ | ||
7 | 7 | ||||
n | 8 | tuple_pairs = list([tuple(pair) for pair in plugboard_position]) | n | 8 | tuple_pairs = list([tuple(curr_set) for curr_set in plugboard_position]) |
9 | tuple_pairs.extend([(y, x) for x, y in tuple_pairs]) | 9 | tuple_pairs.extend([(y, x) for x, y in tuple_pairs]) | ||
10 | encoding_dict = dict(tuple_pairs) | 10 | encoding_dict = dict(tuple_pairs) | ||
11 | 11 | ||||
12 | return rotor(string_to_encode, encoding_dict) | 12 | return rotor(string_to_encode, encoding_dict) | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def rotor(string_to_encode: str, rotor_position: dict): | 15 | def rotor(string_to_encode: str, rotor_position: dict): | ||
n | 16 | """Encodes a String using the Key, Value Pairs in the dict""" | n | 16 | """Encodes a String using the Key, Value Pairs in the Dict.""" |
17 | return "".join([rotor_position[ch] if ch in rotor_position else ch for ch in string_to_encode]) | 17 | return "".join([rotor_position.get(ch, ch) for ch in string_to_encode]) | ||
18 | 18 | ||||
19 | 19 | ||||
20 | def enigma_encrypt(plugboard_position: list, rotor_position: dict): | 20 | def enigma_encrypt(plugboard_position: list, rotor_position: dict): | ||
n | 21 | """Create a Decorator for a Function with the given positions using the Functions plugboard and rotor""" | n | 21 | """Create a Decorator for a Function with the given parameters using the Functions plugboard and rotor.""" |
22 | 22 | ||||
23 | def enigma_encrypt_decorator(func): | 23 | def enigma_encrypt_decorator(func): | ||
n | 24 | """Decorate an Encrypting Function that excepts a single String, to encrypt the String before calling it""" | n | 24 | """Decorate an Encrypting Function that excepts a single String, to encrypt the String before calling it.""" |
25 | 25 | ||||
26 | def decorated_func(s: str): | 26 | def decorated_func(s: str): | ||
n | 27 | """Encrypt a String before calling a Function""" | n | 27 | """Encrypt a String before calling a Function.""" |
28 | return func(rotor(plugboard(s, plugboard_position), rotor_position)) | 28 | return func(rotor(plugboard(s, plugboard_position), rotor_position)) | ||
29 | 29 | ||||
30 | return decorated_func | 30 | return decorated_func | ||
31 | 31 | ||||
32 | return enigma_encrypt_decorator | 32 | return enigma_encrypt_decorator | ||
33 | 33 | ||||
34 | 34 | ||||
35 | def enigma_decrypt(plugboard_position: list, rotor_position: dict): | 35 | def enigma_decrypt(plugboard_position: list, rotor_position: dict): | ||
n | 36 | """Create a Decrypting Decorator for a Function with the given positions using the Functions plugboard and rotor""" | n | 36 | """Create a Decrypting Decorator for a Function with the given positions using the Functions plugboard and rotor.""" |
37 | flipped_rotor_position = dict([(y, x) for x, y in rotor_position.items()]) | 37 | rotor_position = {y: x for x, y in rotor_position.items()} | ||
38 | 38 | ||||
39 | def enigma_decrypt_decorator(func): | 39 | def enigma_decrypt_decorator(func): | ||
n | 40 | """Decorate a Function that excepts a single String, to decrypt the String before calling it""" | n | 40 | """Decorate a Function that excepts a single String, to decrypt the String before calling it.""" |
41 | 41 | ||||
42 | def decorated_func(s: str): | 42 | def decorated_func(s: str): | ||
t | 43 | """Decrypt a String before calling a Function""" | t | 43 | """Decrypt a String before calling a Function.""" |
44 | return func(plugboard(rotor(s, flipped_rotor_position), plugboard_position)) | 44 | return func(plugboard(rotor(s, rotor_position), plugboard_position)) | ||
45 | 45 | ||||
46 | return decorated_func | 46 | return decorated_func | ||
47 | 47 | ||||
48 | return enigma_decrypt_decorator | 48 | return enigma_decrypt_decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def plugboard(string_to_encode: str, plugboard_position: list): | f | 1 | def plugboard(string_to_encode: str, plugboard_position: list): |
2 | """Encode a String based on the Pairs of Chars in the List of Sets. | 2 | """Encode a String based on the Pairs of Chars in the List of Sets. | ||
3 | 3 | ||||
4 | This Function creates a Dict from the List of Pairs and extends it with the Swapped Pairs. | 4 | This Function creates a Dict from the List of Pairs and extends it with the Swapped Pairs. | ||
5 | This way it can use the rotor Function with the Plugboard's Position acting like a Rotor Position | 5 | This way it can use the rotor Function with the Plugboard's Position acting like a Rotor Position | ||
6 | """ | 6 | """ | ||
7 | 7 | ||||
8 | tuple_pairs = list([tuple(pair) for pair in plugboard_position]) | 8 | tuple_pairs = list([tuple(pair) for pair in plugboard_position]) | ||
9 | tuple_pairs.extend([(y, x) for x, y in tuple_pairs]) | 9 | tuple_pairs.extend([(y, x) for x, y in tuple_pairs]) | ||
10 | encoding_dict = dict(tuple_pairs) | 10 | encoding_dict = dict(tuple_pairs) | ||
11 | 11 | ||||
12 | return rotor(string_to_encode, encoding_dict) | 12 | return rotor(string_to_encode, encoding_dict) | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def rotor(string_to_encode: str, rotor_position: dict): | 15 | def rotor(string_to_encode: str, rotor_position: dict): | ||
16 | """Encodes a String using the Key, Value Pairs in the dict""" | 16 | """Encodes a String using the Key, Value Pairs in the dict""" | ||
17 | return "".join([rotor_position[ch] if ch in rotor_position else ch for ch in string_to_encode]) | 17 | return "".join([rotor_position[ch] if ch in rotor_position else ch for ch in string_to_encode]) | ||
18 | 18 | ||||
19 | 19 | ||||
20 | def enigma_encrypt(plugboard_position: list, rotor_position: dict): | 20 | def enigma_encrypt(plugboard_position: list, rotor_position: dict): | ||
t | 21 | """Create a Decorator for a Function with the given parameters using the Functions plugboard and rotor""" | t | 21 | """Create a Decorator for a Function with the given positions using the Functions plugboard and rotor""" |
22 | 22 | ||||
23 | def enigma_encrypt_decorator(func): | 23 | def enigma_encrypt_decorator(func): | ||
24 | """Decorate an Encrypting Function that excepts a single String, to encrypt the String before calling it""" | 24 | """Decorate an Encrypting Function that excepts a single String, to encrypt the String before calling it""" | ||
25 | 25 | ||||
26 | def decorated_func(s: str): | 26 | def decorated_func(s: str): | ||
27 | """Encrypt a String before calling a Function""" | 27 | """Encrypt a String before calling a Function""" | ||
28 | return func(rotor(plugboard(s, plugboard_position), rotor_position)) | 28 | return func(rotor(plugboard(s, plugboard_position), rotor_position)) | ||
29 | 29 | ||||
30 | return decorated_func | 30 | return decorated_func | ||
31 | 31 | ||||
32 | return enigma_encrypt_decorator | 32 | return enigma_encrypt_decorator | ||
33 | 33 | ||||
34 | 34 | ||||
35 | def enigma_decrypt(plugboard_position: list, rotor_position: dict): | 35 | def enigma_decrypt(plugboard_position: list, rotor_position: dict): | ||
36 | """Create a Decrypting Decorator for a Function with the given positions using the Functions plugboard and rotor""" | 36 | """Create a Decrypting Decorator for a Function with the given positions using the Functions plugboard and rotor""" | ||
37 | flipped_rotor_position = dict([(y, x) for x, y in rotor_position.items()]) | 37 | flipped_rotor_position = dict([(y, x) for x, y in rotor_position.items()]) | ||
38 | 38 | ||||
39 | def enigma_decrypt_decorator(func): | 39 | def enigma_decrypt_decorator(func): | ||
40 | """Decorate a Function that excepts a single String, to decrypt the String before calling it""" | 40 | """Decorate a Function that excepts a single String, to decrypt the String before calling it""" | ||
41 | 41 | ||||
42 | def decorated_func(s: str): | 42 | def decorated_func(s: str): | ||
43 | """Decrypt a String before calling a Function""" | 43 | """Decrypt a String before calling a Function""" | ||
44 | return func(plugboard(rotor(s, flipped_rotor_position), plugboard_position)) | 44 | return func(plugboard(rotor(s, flipped_rotor_position), plugboard_position)) | ||
45 | 45 | ||||
46 | return decorated_func | 46 | return decorated_func | ||
47 | 47 | ||||
48 | return enigma_decrypt_decorator | 48 | return enigma_decrypt_decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
n | 1 | def plugboard(string_to_encode: str, encoding_list: list): | n | 1 | def plugboard(string_to_encode: str, plugboard_position: list): |
2 | """Encode a String based on the Pairs of Chars in the List of Sets. | 2 | """Encode a String based on the Pairs of Chars in the List of Sets. | ||
3 | 3 | ||||
4 | This Function creates a Dict from the List of Pairs and extends it with the Swapped Pairs. | 4 | This Function creates a Dict from the List of Pairs and extends it with the Swapped Pairs. | ||
n | 5 | This way it can use the rotor Function | n | 5 | This way it can use the rotor Function with the Plugboard's Position acting like a Rotor Position |
6 | """ | 6 | """ | ||
7 | 7 | ||||
n | 8 | tuple_pairs = list([tuple(curr_set) for curr_set in encoding_list]) | n | 8 | tuple_pairs = list([tuple(pair) for pair in plugboard_position]) |
9 | tuple_pairs.extend([(y, x) for x, y in tuple_pairs]) | 9 | tuple_pairs.extend([(y, x) for x, y in tuple_pairs]) | ||
10 | encoding_dict = dict(tuple_pairs) | 10 | encoding_dict = dict(tuple_pairs) | ||
11 | 11 | ||||
12 | return rotor(string_to_encode, encoding_dict) | 12 | return rotor(string_to_encode, encoding_dict) | ||
13 | 13 | ||||
14 | 14 | ||||
n | 15 | def rotor(string_to_encode: str, encoding_dict: dict): | n | 15 | def rotor(string_to_encode: str, rotor_position: dict): |
16 | """Encodes a String using the Key, Value Pairs in the dict""" | 16 | """Encodes a String using the Key, Value Pairs in the dict""" | ||
n | 17 | return "".join([encoding_dict[ch] if ch in encoding_dict else ch for ch in string_to_encode]) | n | 17 | return "".join([rotor_position[ch] if ch in rotor_position else ch for ch in string_to_encode]) |
18 | 18 | ||||
19 | 19 | ||||
20 | def enigma_encrypt(plugboard_position: list, rotor_position: dict): | 20 | def enigma_encrypt(plugboard_position: list, rotor_position: dict): | ||
21 | """Create a Decorator for a Function with the given parameters using the Functions plugboard and rotor""" | 21 | """Create a Decorator for a Function with the given parameters using the Functions plugboard and rotor""" | ||
22 | 22 | ||||
23 | def enigma_encrypt_decorator(func): | 23 | def enigma_encrypt_decorator(func): | ||
24 | """Decorate an Encrypting Function that excepts a single String, to encrypt the String before calling it""" | 24 | """Decorate an Encrypting Function that excepts a single String, to encrypt the String before calling it""" | ||
25 | 25 | ||||
26 | def decorated_func(s: str): | 26 | def decorated_func(s: str): | ||
27 | """Encrypt a String before calling a Function""" | 27 | """Encrypt a String before calling a Function""" | ||
28 | return func(rotor(plugboard(s, plugboard_position), rotor_position)) | 28 | return func(rotor(plugboard(s, plugboard_position), rotor_position)) | ||
29 | 29 | ||||
30 | return decorated_func | 30 | return decorated_func | ||
31 | 31 | ||||
32 | return enigma_encrypt_decorator | 32 | return enigma_encrypt_decorator | ||
33 | 33 | ||||
34 | 34 | ||||
35 | def enigma_decrypt(plugboard_position: list, rotor_position: dict): | 35 | def enigma_decrypt(plugboard_position: list, rotor_position: dict): | ||
36 | """Create a Decrypting Decorator for a Function with the given positions using the Functions plugboard and rotor""" | 36 | """Create a Decrypting Decorator for a Function with the given positions using the Functions plugboard and rotor""" | ||
n | 37 | rotor_position = dict([(y, x) for x, y in rotor_position.items()]) | n | 37 | flipped_rotor_position = dict([(y, x) for x, y in rotor_position.items()]) |
38 | 38 | ||||
39 | def enigma_decrypt_decorator(func): | 39 | def enigma_decrypt_decorator(func): | ||
40 | """Decorate a Function that excepts a single String, to decrypt the String before calling it""" | 40 | """Decorate a Function that excepts a single String, to decrypt the String before calling it""" | ||
41 | 41 | ||||
42 | def decorated_func(s: str): | 42 | def decorated_func(s: str): | ||
43 | """Decrypt a String before calling a Function""" | 43 | """Decrypt a String before calling a Function""" | ||
t | 44 | return func(plugboard(rotor(s, rotor_position), plugboard_position)) | t | 44 | return func(plugboard(rotor(s, flipped_rotor_position), plugboard_position)) |
45 | 45 | ||||
46 | return decorated_func | 46 | return decorated_func | ||
47 | 47 | ||||
48 | return enigma_decrypt_decorator | 48 | return enigma_decrypt_decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
n | 1 | def plugboard(string_to_encode: str, sets: list): | n | 1 | def plugboard(string_to_encode: str, encoding_list: list): |
2 | """Encode a String based on the Pairs of Chars in the List of Sets""" | 2 | """Encode a String based on the Pairs of Chars in the List of Sets. | ||
3 | 3 | ||||
t | t | 4 | This Function creates a Dict from the List of Pairs and extends it with the Swapped Pairs. | ||
5 | This way it can use the rotor Function | ||||
6 | """ | ||||
7 | |||||
4 | tuple_pairs = list([tuple(curr_set) for curr_set in sets]) | 8 | tuple_pairs = list([tuple(curr_set) for curr_set in encoding_list]) | ||
5 | tuple_pairs.extend([(y, x) for x, y in tuple_pairs]) | 9 | tuple_pairs.extend([(y, x) for x, y in tuple_pairs]) | ||
6 | encoding_dict = dict(tuple_pairs) | 10 | encoding_dict = dict(tuple_pairs) | ||
7 | 11 | ||||
8 | return rotor(string_to_encode, encoding_dict) | 12 | return rotor(string_to_encode, encoding_dict) | ||
9 | 13 | ||||
10 | 14 | ||||
11 | def rotor(string_to_encode: str, encoding_dict: dict): | 15 | def rotor(string_to_encode: str, encoding_dict: dict): | ||
12 | """Encodes a String using the Key, Value Pairs in the dict""" | 16 | """Encodes a String using the Key, Value Pairs in the dict""" | ||
13 | return "".join([encoding_dict[ch] if ch in encoding_dict else ch for ch in string_to_encode]) | 17 | return "".join([encoding_dict[ch] if ch in encoding_dict else ch for ch in string_to_encode]) | ||
14 | 18 | ||||
15 | 19 | ||||
16 | def enigma_encrypt(plugboard_position: list, rotor_position: dict): | 20 | def enigma_encrypt(plugboard_position: list, rotor_position: dict): | ||
17 | """Create a Decorator for a Function with the given parameters using the Functions plugboard and rotor""" | 21 | """Create a Decorator for a Function with the given parameters using the Functions plugboard and rotor""" | ||
18 | 22 | ||||
19 | def enigma_encrypt_decorator(func): | 23 | def enigma_encrypt_decorator(func): | ||
20 | """Decorate an Encrypting Function that excepts a single String, to encrypt the String before calling it""" | 24 | """Decorate an Encrypting Function that excepts a single String, to encrypt the String before calling it""" | ||
21 | 25 | ||||
22 | def decorated_func(s: str): | 26 | def decorated_func(s: str): | ||
23 | """Encrypt a String before calling a Function""" | 27 | """Encrypt a String before calling a Function""" | ||
24 | return func(rotor(plugboard(s, plugboard_position), rotor_position)) | 28 | return func(rotor(plugboard(s, plugboard_position), rotor_position)) | ||
25 | 29 | ||||
26 | return decorated_func | 30 | return decorated_func | ||
27 | 31 | ||||
28 | return enigma_encrypt_decorator | 32 | return enigma_encrypt_decorator | ||
29 | 33 | ||||
30 | 34 | ||||
31 | def enigma_decrypt(plugboard_position: list, rotor_position: dict): | 35 | def enigma_decrypt(plugboard_position: list, rotor_position: dict): | ||
32 | """Create a Decrypting Decorator for a Function with the given positions using the Functions plugboard and rotor""" | 36 | """Create a Decrypting Decorator for a Function with the given positions using the Functions plugboard and rotor""" | ||
33 | rotor_position = dict([(y, x) for x, y in rotor_position.items()]) | 37 | rotor_position = dict([(y, x) for x, y in rotor_position.items()]) | ||
34 | 38 | ||||
35 | def enigma_decrypt_decorator(func): | 39 | def enigma_decrypt_decorator(func): | ||
36 | """Decorate a Function that excepts a single String, to decrypt the String before calling it""" | 40 | """Decorate a Function that excepts a single String, to decrypt the String before calling it""" | ||
37 | 41 | ||||
38 | def decorated_func(s: str): | 42 | def decorated_func(s: str): | ||
39 | """Decrypt a String before calling a Function""" | 43 | """Decrypt a String before calling a Function""" | ||
40 | return func(plugboard(rotor(s, rotor_position), plugboard_position)) | 44 | return func(plugboard(rotor(s, rotor_position), plugboard_position)) | ||
41 | 45 | ||||
42 | return decorated_func | 46 | return decorated_func | ||
43 | 47 | ||||
44 | return enigma_decrypt_decorator | 48 | return enigma_decrypt_decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def plugboard(string_to_encode: str, sets: list): | f | 1 | def plugboard(string_to_encode: str, sets: list): |
2 | """Encode a String based on the Pairs of Chars in the List of Sets""" | 2 | """Encode a String based on the Pairs of Chars in the List of Sets""" | ||
3 | 3 | ||||
4 | tuple_pairs = list([tuple(curr_set) for curr_set in sets]) | 4 | tuple_pairs = list([tuple(curr_set) for curr_set in sets]) | ||
5 | tuple_pairs.extend([(y, x) for x, y in tuple_pairs]) | 5 | tuple_pairs.extend([(y, x) for x, y in tuple_pairs]) | ||
6 | encoding_dict = dict(tuple_pairs) | 6 | encoding_dict = dict(tuple_pairs) | ||
7 | 7 | ||||
8 | return rotor(string_to_encode, encoding_dict) | 8 | return rotor(string_to_encode, encoding_dict) | ||
9 | 9 | ||||
10 | 10 | ||||
11 | def rotor(string_to_encode: str, encoding_dict: dict): | 11 | def rotor(string_to_encode: str, encoding_dict: dict): | ||
12 | """Encodes a String using the Key, Value Pairs in the dict""" | 12 | """Encodes a String using the Key, Value Pairs in the dict""" | ||
n | 13 | return "".join([encoding_dict[ch] for ch in string_to_encode]) | n | 13 | return "".join([encoding_dict[ch] if ch in encoding_dict else ch for ch in string_to_encode]) |
14 | 14 | ||||
15 | 15 | ||||
16 | def enigma_encrypt(plugboard_position: list, rotor_position: dict): | 16 | def enigma_encrypt(plugboard_position: list, rotor_position: dict): | ||
17 | """Create a Decorator for a Function with the given parameters using the Functions plugboard and rotor""" | 17 | """Create a Decorator for a Function with the given parameters using the Functions plugboard and rotor""" | ||
18 | 18 | ||||
19 | def enigma_encrypt_decorator(func): | 19 | def enigma_encrypt_decorator(func): | ||
20 | """Decorate an Encrypting Function that excepts a single String, to encrypt the String before calling it""" | 20 | """Decorate an Encrypting Function that excepts a single String, to encrypt the String before calling it""" | ||
21 | 21 | ||||
22 | def decorated_func(s: str): | 22 | def decorated_func(s: str): | ||
23 | """Encrypt a String before calling a Function""" | 23 | """Encrypt a String before calling a Function""" | ||
24 | return func(rotor(plugboard(s, plugboard_position), rotor_position)) | 24 | return func(rotor(plugboard(s, plugboard_position), rotor_position)) | ||
25 | 25 | ||||
26 | return decorated_func | 26 | return decorated_func | ||
27 | 27 | ||||
28 | return enigma_encrypt_decorator | 28 | return enigma_encrypt_decorator | ||
29 | 29 | ||||
30 | 30 | ||||
31 | def enigma_decrypt(plugboard_position: list, rotor_position: dict): | 31 | def enigma_decrypt(plugboard_position: list, rotor_position: dict): | ||
32 | """Create a Decrypting Decorator for a Function with the given positions using the Functions plugboard and rotor""" | 32 | """Create a Decrypting Decorator for a Function with the given positions using the Functions plugboard and rotor""" | ||
33 | rotor_position = dict([(y, x) for x, y in rotor_position.items()]) | 33 | rotor_position = dict([(y, x) for x, y in rotor_position.items()]) | ||
34 | 34 | ||||
35 | def enigma_decrypt_decorator(func): | 35 | def enigma_decrypt_decorator(func): | ||
36 | """Decorate a Function that excepts a single String, to decrypt the String before calling it""" | 36 | """Decorate a Function that excepts a single String, to decrypt the String before calling it""" | ||
37 | 37 | ||||
38 | def decorated_func(s: str): | 38 | def decorated_func(s: str): | ||
39 | """Decrypt a String before calling a Function""" | 39 | """Decrypt a String before calling a Function""" | ||
40 | return func(plugboard(rotor(s, rotor_position), plugboard_position)) | 40 | return func(plugboard(rotor(s, rotor_position), plugboard_position)) | ||
41 | 41 | ||||
42 | return decorated_func | 42 | return decorated_func | ||
43 | 43 | ||||
44 | return enigma_decrypt_decorator | 44 | return enigma_decrypt_decorator | ||
t | 45 | t |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|