1def convert_to_dict(plugboard_position):
2 dictionary = {}
3 for i in plugboard_position:
4 for j in i:
5 dictionary[j] = (i - {j}).pop()
6 return dictionary
7
8
9def convert_to_dict_decorator(func):
10 def wrapper(text, plugboard_position):
11 return func(text, convert_to_dict(plugboard_position))
12 return wrapper
13
14
15def encrypt_text_with_dict(text, dictionary):
16 return "".join(map(lambda key: dictionary.get(key, key), text))
17
18
19def reverse_dict(dictionary):
20 return {value: key for key, value in dictionary.items()}
21
22
23def reverse_dict_decorator(func):
24 def wrapper(text, dictionary):
25 return func(text, reverse_dict(dictionary))
26 return wrapper
27
28
29@convert_to_dict_decorator
30def plugboard(text, plugboard_position):
31 return encrypt_text_with_dict(text, plugboard_position)
32
33
34def rotor(text, rotor_position):
35 return encrypt_text_with_dict(text, rotor_position)
36
37
38@reverse_dict_decorator
39def rotor_decrypt(text, rotor_position):
40 return encrypt_text_with_dict(text, rotor_position)
41
42
43def enigma_encrypt(plugboard_position, rotor_position):
44 def decorator(func):
45 def wrapper(text):
46 encrypted_text = rotor(
47 plugboard(text, plugboard_position), rotor_position)
48 return func(encrypted_text)
49 return wrapper
50 return decorator
51
52
53def enigma_decrypt(plugboard_position, rotor_position):
54 def decorator(func):
55 def wrapper(text):
56 decrypted_text = plugboard(rotor_decrypt(
57 text, rotor_position), plugboard_position)
58 return func(decrypted_text)
59 return wrapper
60 return decorator
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
| f | 1 | def convert_to_dict(plugboard_position): | f | 1 | def convert_to_dict(plugboard_position): |
| 2 | dictionary = {} | 2 | dictionary = {} | ||
| 3 | for i in plugboard_position: | 3 | for i in plugboard_position: | ||
| 4 | for j in i: | 4 | for j in i: | ||
| 5 | dictionary[j] = (i - {j}).pop() | 5 | dictionary[j] = (i - {j}).pop() | ||
| 6 | return dictionary | 6 | return dictionary | ||
| 7 | 7 | ||||
| 8 | 8 | ||||
| 9 | def convert_to_dict_decorator(func): | 9 | def convert_to_dict_decorator(func): | ||
| 10 | def wrapper(text, plugboard_position): | 10 | def wrapper(text, plugboard_position): | ||
| 11 | return func(text, convert_to_dict(plugboard_position)) | 11 | return func(text, convert_to_dict(plugboard_position)) | ||
| 12 | return wrapper | 12 | return wrapper | ||
| 13 | 13 | ||||
| 14 | 14 | ||||
| 15 | def encrypt_text_with_dict(text, dictionary): | 15 | def encrypt_text_with_dict(text, dictionary): | ||
| n | 16 | return "".join(map(lambda key: dictionary[key] | n | 16 | return "".join(map(lambda key: dictionary.get(key, key), text)) |
| 17 | if key in dictionary | ||||
| 18 | else key, | ||||
| 19 | text)) | ||||
| 20 | 17 | ||||
| 21 | 18 | ||||
| 22 | def reverse_dict(dictionary): | 19 | def reverse_dict(dictionary): | ||
| 23 | return {value: key for key, value in dictionary.items()} | 20 | return {value: key for key, value in dictionary.items()} | ||
| 24 | 21 | ||||
| 25 | 22 | ||||
| 26 | def reverse_dict_decorator(func): | 23 | def reverse_dict_decorator(func): | ||
| 27 | def wrapper(text, dictionary): | 24 | def wrapper(text, dictionary): | ||
| 28 | return func(text, reverse_dict(dictionary)) | 25 | return func(text, reverse_dict(dictionary)) | ||
| 29 | return wrapper | 26 | return wrapper | ||
| 30 | 27 | ||||
| 31 | 28 | ||||
| 32 | @convert_to_dict_decorator | 29 | @convert_to_dict_decorator | ||
| 33 | def plugboard(text, plugboard_position): | 30 | def plugboard(text, plugboard_position): | ||
| 34 | return encrypt_text_with_dict(text, plugboard_position) | 31 | return encrypt_text_with_dict(text, plugboard_position) | ||
| 35 | 32 | ||||
| 36 | 33 | ||||
| 37 | def rotor(text, rotor_position): | 34 | def rotor(text, rotor_position): | ||
| 38 | return encrypt_text_with_dict(text, rotor_position) | 35 | return encrypt_text_with_dict(text, rotor_position) | ||
| 39 | 36 | ||||
| 40 | 37 | ||||
| 41 | @reverse_dict_decorator | 38 | @reverse_dict_decorator | ||
| 42 | def rotor_decrypt(text, rotor_position): | 39 | def rotor_decrypt(text, rotor_position): | ||
| n | 43 | return encrypt_text_with_dict(text,rotor_position) | n | 40 | return encrypt_text_with_dict(text, rotor_position) |
| 44 | 41 | ||||
| 45 | 42 | ||||
| 46 | def enigma_encrypt(plugboard_position, rotor_position): | 43 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 47 | def decorator(func): | 44 | def decorator(func): | ||
| 48 | def wrapper(text): | 45 | def wrapper(text): | ||
| n | n | 46 | encrypted_text = rotor( | ||
| 49 | encrypted_text = rotor(plugboard(text, plugboard_position), rotor_position) | 47 | plugboard(text, plugboard_position), rotor_position) | ||
| 50 | return func(encrypted_text) | 48 | return func(encrypted_text) | ||
| 51 | return wrapper | 49 | return wrapper | ||
| 52 | return decorator | 50 | return decorator | ||
| 53 | 51 | ||||
| 54 | 52 | ||||
| 55 | def enigma_decrypt(plugboard_position, rotor_position): | 53 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 56 | def decorator(func): | 54 | def decorator(func): | ||
| 57 | def wrapper(text): | 55 | def wrapper(text): | ||
| t | 58 | decrypted_text = plugboard(rotor_decrypt(text, rotor_position),plugboard_position) | t | 56 | decrypted_text = plugboard(rotor_decrypt( |
| 57 | text, rotor_position), plugboard_position) | ||||
| 59 | return func(decrypted_text) | 58 | return func(decrypted_text) | ||
| 60 | return wrapper | 59 | return wrapper | ||
| 61 | return decorator | 60 | return decorator |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | def convert_to_dict(plugboard_position): | f | 1 | def convert_to_dict(plugboard_position): |
| 2 | dictionary = {} | 2 | dictionary = {} | ||
| 3 | for i in plugboard_position: | 3 | for i in plugboard_position: | ||
| 4 | for j in i: | 4 | for j in i: | ||
| 5 | dictionary[j] = (i - {j}).pop() | 5 | dictionary[j] = (i - {j}).pop() | ||
| 6 | return dictionary | 6 | return dictionary | ||
| 7 | 7 | ||||
| n | n | 8 | |||
| 8 | def convert_to_dict_decorator(func): | 9 | def convert_to_dict_decorator(func): | ||
| 9 | def wrapper(text, plugboard_position): | 10 | def wrapper(text, plugboard_position): | ||
| 10 | return func(text, convert_to_dict(plugboard_position)) | 11 | return func(text, convert_to_dict(plugboard_position)) | ||
| 11 | return wrapper | 12 | return wrapper | ||
| n | n | 13 | |||
| 12 | 14 | ||||
| 13 | def encrypt_text_with_dict(text, dictionary): | 15 | def encrypt_text_with_dict(text, dictionary): | ||
| 14 | return "".join(map(lambda key: dictionary[key] | 16 | return "".join(map(lambda key: dictionary[key] | ||
| 15 | if key in dictionary | 17 | if key in dictionary | ||
| 16 | else key, | 18 | else key, | ||
| 17 | text)) | 19 | text)) | ||
| 18 | 20 | ||||
| n | n | 21 | |||
| 19 | def reverse_dict(dictionary): | 22 | def reverse_dict(dictionary): | ||
| 20 | return {value: key for key, value in dictionary.items()} | 23 | return {value: key for key, value in dictionary.items()} | ||
| n | n | 24 | |||
| 21 | 25 | ||||
| 22 | def reverse_dict_decorator(func): | 26 | def reverse_dict_decorator(func): | ||
| 23 | def wrapper(text, dictionary): | 27 | def wrapper(text, dictionary): | ||
| 24 | return func(text, reverse_dict(dictionary)) | 28 | return func(text, reverse_dict(dictionary)) | ||
| 25 | return wrapper | 29 | return wrapper | ||
| 26 | 30 | ||||
| n | n | 31 | |||
| 27 | @convert_to_dict_decorator | 32 | @convert_to_dict_decorator | ||
| 28 | def plugboard(text, plugboard_position): | 33 | def plugboard(text, plugboard_position): | ||
| 29 | return encrypt_text_with_dict(text, plugboard_position) | 34 | return encrypt_text_with_dict(text, plugboard_position) | ||
| 30 | 35 | ||||
| n | n | 36 | |||
| 31 | def rotor(text, rotor_position): | 37 | def rotor(text, rotor_position): | ||
| 32 | return encrypt_text_with_dict(text, rotor_position) | 38 | return encrypt_text_with_dict(text, rotor_position) | ||
| n | n | 39 | |||
| 33 | 40 | ||||
| 34 | @reverse_dict_decorator | 41 | @reverse_dict_decorator | ||
| 35 | def rotor_decrypt(text, rotor_position): | 42 | def rotor_decrypt(text, rotor_position): | ||
| 36 | return encrypt_text_with_dict(text,rotor_position) | 43 | return encrypt_text_with_dict(text,rotor_position) | ||
| n | n | 44 | |||
| 37 | 45 | ||||
| 38 | def enigma_encrypt(plugboard_position, rotor_position): | 46 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 39 | def decorator(func): | 47 | def decorator(func): | ||
| 40 | def wrapper(text): | 48 | def wrapper(text): | ||
| 41 | encrypted_text = rotor(plugboard(text, plugboard_position), rotor_position) | 49 | encrypted_text = rotor(plugboard(text, plugboard_position), rotor_position) | ||
| 42 | return func(encrypted_text) | 50 | return func(encrypted_text) | ||
| 43 | return wrapper | 51 | return wrapper | ||
| 44 | return decorator | 52 | return decorator | ||
| 45 | 53 | ||||
| t | t | 54 | |||
| 46 | def enigma_decrypt(plugboard_position, rotor_position): | 55 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 47 | def decorator(func): | 56 | def decorator(func): | ||
| 48 | def wrapper(text): | 57 | def wrapper(text): | ||
| 49 | decrypted_text = plugboard(rotor_decrypt(text, rotor_position),plugboard_position) | 58 | decrypted_text = plugboard(rotor_decrypt(text, rotor_position),plugboard_position) | ||
| 50 | return func(decrypted_text) | 59 | return func(decrypted_text) | ||
| 51 | return wrapper | 60 | return wrapper | ||
| 52 | return decorator | 61 | return decorator |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
31.10.2023 09:08