1def plugboard(text, plugboard_position):
2 '''
3 The function swaps letters in the input text according to the plugboard_position.
4
5 :param text: A string containing the text to be converted.
6 :param plugboard_position: A list of sets, each set containing two letters to be swapped.
7 :return: The converted text with letters swapped based on the plugboard_position.
8 '''
9 keys = dict(plugboard_position)
10 result = []
11 for char in text:
12 if char in keys:
13 result.append(keys[char])
14 elif char in keys.values():
15 for key, value in keys.items():
16 if value == char:
17 result.append(key)
18 else:
19 result.append(char)
20 return ''.join(result)
21
22def rotor(text, rotor_position):
23 '''
24 The function swaps letters in the input text according to the rotor_position.
25
26 :param text: A string containing the text to be converted.
27 :param rotor_position: A dictionary containing letter mappings for conversion.
28 :return: The converted text with letters swapped based on the rotor_position.
29 '''
30 text.split()
31 result = []
32 for symbol in text:
33 if symbol in rotor_position:
34 result.append(rotor_position[symbol])
35 else:
36 result.append(symbol)
37 return ''.join(result)
38
39def enigma_encrypt(plugboard_position, rotor_position):
40 '''
41 A decorator that encrypts text using plugboard and rotor configurations.
42
43 :param plugboard_position: A list of sets, each set containing two letters to be swapped.
44 :param rotor_position: A dictionary containing letter mappings for conversion.
45 :return: A decorator function that encrypts text before calling the decorated function.
46 '''
47 def decorator(func):
48 def encrypt(text):
49 text = plugboard(text, plugboard_position)
50 text = rotor(text, rotor_position)
51 return func(text)
52 return encrypt
53 return decorator
54
55def enigma_decrypt(plugboard_position, rotor_position):
56 '''
57 A decorator that decrypts text using plugboard and rotor configurations.
58
59 :param plugboard_position: A list of sets, each set containing two letters to be swapped.
60 :param rotor_position: A dictionary containing letter mappings for conversion.
61 :return: A decorator function that decrypts text before calling the decorated function.
62 '''
63 def decrypt_decorator(func):
64 def decrypt_text(text):
65 rotor_reverse = {v: k for k, v in rotor_position.items()}
66 text = rotor(text, rotor_reverse)
67 text = plugboard(text, plugboard_position)
68 return func(text)
69 return decrypt_text
70 return decrypt_decorator
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
f | 1 | def plugboard(text, plugboard_position): | f | 1 | def plugboard(text, plugboard_position): |
n | n | 2 | ''' | ||
3 | The function swaps letters in the input text according to the plugboard_position. | ||||
4 | |||||
5 | :param text: A string containing the text to be converted. | ||||
6 | :param plugboard_position: A list of sets, each set containing two letters to be swapped. | ||||
7 | :return: The converted text with letters swapped based on the plugboard_position. | ||||
8 | ''' | ||||
2 | keys = dict(plugboard_position) | 9 | keys = dict(plugboard_position) | ||
3 | result = [] | 10 | result = [] | ||
4 | for char in text: | 11 | for char in text: | ||
5 | if char in keys: | 12 | if char in keys: | ||
6 | result.append(keys[char]) | 13 | result.append(keys[char]) | ||
7 | elif char in keys.values(): | 14 | elif char in keys.values(): | ||
8 | for key, value in keys.items(): | 15 | for key, value in keys.items(): | ||
9 | if value == char: | 16 | if value == char: | ||
10 | result.append(key) | 17 | result.append(key) | ||
11 | else: | 18 | else: | ||
12 | result.append(char) | 19 | result.append(char) | ||
13 | return ''.join(result) | 20 | return ''.join(result) | ||
14 | 21 | ||||
15 | def rotor(text, rotor_position): | 22 | def rotor(text, rotor_position): | ||
n | n | 23 | ''' | ||
24 | The function swaps letters in the input text according to the rotor_position. | ||||
25 | |||||
26 | :param text: A string containing the text to be converted. | ||||
27 | :param rotor_position: A dictionary containing letter mappings for conversion. | ||||
28 | :return: The converted text with letters swapped based on the rotor_position. | ||||
29 | ''' | ||||
16 | text.split() | 30 | text.split() | ||
17 | result = [] | 31 | result = [] | ||
18 | for symbol in text: | 32 | for symbol in text: | ||
19 | if symbol in rotor_position: | 33 | if symbol in rotor_position: | ||
20 | result.append(rotor_position[symbol]) | 34 | result.append(rotor_position[symbol]) | ||
21 | else: | 35 | else: | ||
22 | result.append(symbol) | 36 | result.append(symbol) | ||
23 | return ''.join(result) | 37 | return ''.join(result) | ||
24 | 38 | ||||
n | 25 | def enigma_encrypt(plugboard_position , rotor_position): | n | 39 | def enigma_encrypt(plugboard_position, rotor_position): |
40 | ''' | ||||
41 | A decorator that encrypts text using plugboard and rotor configurations. | ||||
42 | |||||
43 | :param plugboard_position: A list of sets, each set containing two letters to be swapped. | ||||
44 | :param rotor_position: A dictionary containing letter mappings for conversion. | ||||
45 | :return: A decorator function that encrypts text before calling the decorated function. | ||||
46 | ''' | ||||
26 | def decorator(func): | 47 | def decorator(func): | ||
27 | def encrypt(text): | 48 | def encrypt(text): | ||
28 | text = plugboard(text, plugboard_position) | 49 | text = plugboard(text, plugboard_position) | ||
29 | text = rotor(text, rotor_position) | 50 | text = rotor(text, rotor_position) | ||
30 | return func(text) | 51 | return func(text) | ||
31 | return encrypt | 52 | return encrypt | ||
32 | return decorator | 53 | return decorator | ||
33 | 54 | ||||
34 | def enigma_decrypt(plugboard_position, rotor_position): | 55 | def enigma_decrypt(plugboard_position, rotor_position): | ||
t | t | 56 | ''' | ||
57 | A decorator that decrypts text using plugboard and rotor configurations. | ||||
58 | |||||
59 | :param plugboard_position: A list of sets, each set containing two letters to be swapped. | ||||
60 | :param rotor_position: A dictionary containing letter mappings for conversion. | ||||
61 | :return: A decorator function that decrypts text before calling the decorated function. | ||||
62 | ''' | ||||
35 | def decrypt_decorator(func): | 63 | def decrypt_decorator(func): | ||
36 | def decrypt_text(text): | 64 | def decrypt_text(text): | ||
37 | rotor_reverse = {v: k for k, v in rotor_position.items()} | 65 | rotor_reverse = {v: k for k, v in rotor_position.items()} | ||
38 | text = rotor(text, rotor_reverse) | 66 | text = rotor(text, rotor_reverse) | ||
39 | text = plugboard(text, plugboard_position) | 67 | text = plugboard(text, plugboard_position) | ||
40 | return func(text) | 68 | return func(text) | ||
41 | return decrypt_text | 69 | return decrypt_text | ||
42 | return decrypt_decorator | 70 | return decrypt_decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def plugboard(text, plugboard_position): | f | 1 | def plugboard(text, plugboard_position): |
2 | keys = dict(plugboard_position) | 2 | keys = dict(plugboard_position) | ||
3 | result = [] | 3 | result = [] | ||
4 | for char in text: | 4 | for char in text: | ||
5 | if char in keys: | 5 | if char in keys: | ||
6 | result.append(keys[char]) | 6 | result.append(keys[char]) | ||
7 | elif char in keys.values(): | 7 | elif char in keys.values(): | ||
8 | for key, value in keys.items(): | 8 | for key, value in keys.items(): | ||
9 | if value == char: | 9 | if value == char: | ||
10 | result.append(key) | 10 | result.append(key) | ||
11 | else: | 11 | else: | ||
12 | result.append(char) | 12 | result.append(char) | ||
13 | return ''.join(result) | 13 | return ''.join(result) | ||
14 | 14 | ||||
15 | def rotor(text, rotor_position): | 15 | def rotor(text, rotor_position): | ||
16 | text.split() | 16 | text.split() | ||
17 | result = [] | 17 | result = [] | ||
18 | for symbol in text: | 18 | for symbol in text: | ||
19 | if symbol in rotor_position: | 19 | if symbol in rotor_position: | ||
20 | result.append(rotor_position[symbol]) | 20 | result.append(rotor_position[symbol]) | ||
21 | else: | 21 | else: | ||
22 | result.append(symbol) | 22 | result.append(symbol) | ||
23 | return ''.join(result) | 23 | return ''.join(result) | ||
24 | 24 | ||||
25 | def enigma_encrypt(plugboard_position , rotor_position): | 25 | def enigma_encrypt(plugboard_position , rotor_position): | ||
26 | def decorator(func): | 26 | def decorator(func): | ||
27 | def encrypt(text): | 27 | def encrypt(text): | ||
28 | text = plugboard(text, plugboard_position) | 28 | text = plugboard(text, plugboard_position) | ||
29 | text = rotor(text, rotor_position) | 29 | text = rotor(text, rotor_position) | ||
30 | return func(text) | 30 | return func(text) | ||
31 | return encrypt | 31 | return encrypt | ||
32 | return decorator | 32 | return decorator | ||
33 | 33 | ||||
34 | def enigma_decrypt(plugboard_position, rotor_position): | 34 | def enigma_decrypt(plugboard_position, rotor_position): | ||
35 | def decrypt_decorator(func): | 35 | def decrypt_decorator(func): | ||
36 | def decrypt_text(text): | 36 | def decrypt_text(text): | ||
n | 37 | # Reverse the rotor configuration dictionary | n | ||
38 | rotor_reverse = {v: k for k, v in rotor_position.items()} | 37 | rotor_reverse = {v: k for k, v in rotor_position.items()} | ||
t | 39 | text = rotor(text, rotor_reverse) # Decrypt using the reverse rotor configuration | t | 38 | text = rotor(text, rotor_reverse) |
40 | text = plugboard(text, plugboard_position) | 39 | text = plugboard(text, plugboard_position) | ||
41 | return func(text) | 40 | return func(text) | ||
42 | return decrypt_text | 41 | return decrypt_text | ||
43 | return decrypt_decorator | 42 | return decrypt_decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def plugboard(text, plugboard_position): | f | 1 | def plugboard(text, plugboard_position): |
2 | keys = dict(plugboard_position) | 2 | keys = dict(plugboard_position) | ||
3 | result = [] | 3 | result = [] | ||
4 | for char in text: | 4 | for char in text: | ||
5 | if char in keys: | 5 | if char in keys: | ||
6 | result.append(keys[char]) | 6 | result.append(keys[char]) | ||
7 | elif char in keys.values(): | 7 | elif char in keys.values(): | ||
8 | for key, value in keys.items(): | 8 | for key, value in keys.items(): | ||
9 | if value == char: | 9 | if value == char: | ||
10 | result.append(key) | 10 | result.append(key) | ||
11 | else: | 11 | else: | ||
12 | result.append(char) | 12 | result.append(char) | ||
13 | return ''.join(result) | 13 | return ''.join(result) | ||
14 | 14 | ||||
15 | def rotor(text, rotor_position): | 15 | def rotor(text, rotor_position): | ||
16 | text.split() | 16 | text.split() | ||
17 | result = [] | 17 | result = [] | ||
18 | for symbol in text: | 18 | for symbol in text: | ||
19 | if symbol in rotor_position: | 19 | if symbol in rotor_position: | ||
20 | result.append(rotor_position[symbol]) | 20 | result.append(rotor_position[symbol]) | ||
21 | else: | 21 | else: | ||
22 | result.append(symbol) | 22 | result.append(symbol) | ||
23 | return ''.join(result) | 23 | return ''.join(result) | ||
24 | 24 | ||||
25 | def enigma_encrypt(plugboard_position , rotor_position): | 25 | def enigma_encrypt(plugboard_position , rotor_position): | ||
n | 26 | def decorator(func): | n | 26 | def decorator(func): |
27 | def encrypt(text): | 27 | def encrypt(text): | ||
28 | text = plugboard(text, plugboard_position) | 28 | text = plugboard(text, plugboard_position) | ||
29 | text = rotor(text, rotor_position) | 29 | text = rotor(text, rotor_position) | ||
30 | return func(text) | 30 | return func(text) | ||
31 | return encrypt | 31 | return encrypt | ||
32 | return decorator | 32 | return decorator | ||
33 | 33 | ||||
34 | def enigma_decrypt(plugboard_position, rotor_position): | 34 | def enigma_decrypt(plugboard_position, rotor_position): | ||
35 | def decrypt_decorator(func): | 35 | def decrypt_decorator(func): | ||
36 | def decrypt_text(text): | 36 | def decrypt_text(text): | ||
37 | # Reverse the rotor configuration dictionary | 37 | # Reverse the rotor configuration dictionary | ||
38 | rotor_reverse = {v: k for k, v in rotor_position.items()} | 38 | rotor_reverse = {v: k for k, v in rotor_position.items()} | ||
39 | text = rotor(text, rotor_reverse) # Decrypt using the reverse rotor configuration | 39 | text = rotor(text, rotor_reverse) # Decrypt using the reverse rotor configuration | ||
40 | text = plugboard(text, plugboard_position) | 40 | text = plugboard(text, plugboard_position) | ||
41 | return func(text) | 41 | return func(text) | ||
42 | return decrypt_text | 42 | return decrypt_text | ||
43 | return decrypt_decorator | 43 | return decrypt_decorator | ||
t | 44 | t | |||
45 | |||||
46 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | ||||
47 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||||
48 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||||
49 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||||
50 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||||
51 | 'm': 'r', 'c': 'k'} |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|