Домашни > Енигма > Решения > Решението на Илиян Георгиев

Резултати
10 точки от тестове
0 точки от учител

10 точки общо

9 успешни теста
0 неуспешни теста
Код

 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

Дискусия
История

f1f1
22
n3def plugboard(uncoded : str, plugboard_pos : list):n3def 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))
55
66
n7def rotor(uncoded : str, from_to_dict : dict):n7def 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)
99
1010
n11def enigma_encrypt(plugboard_position : list, rotor_position : dict):n11def enigma_encrypt(plugboard_position: list, rotor_position: dict):
12    def encrypting_decorator(func):12    def encrypting_decorator(func):
n13        def encrypt_and_run_func(str):n13        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_func16        return encrypt_and_run_func
19    return encrypting_decorator17    return encrypting_decorator
2018
2119
n22def enigma_decrypt(plugboard_position : list, rotor_position : dict):n20def enigma_decrypt(plugboard_position: list, rotor_position: dict):
23    def encrypting_decorator(func):21    def encrypting_decorator(func):
n24        def decrypt_and_run_func(str):n22        def decrypt_and_run_func(text):
25            reverse_rotor_position = rotate_rotor_positions(rotor_position)23            reverse_rotor_position = rotate_rotor_positions(rotor_position)
n26            str = plugboard(n24            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_func26        return decrypt_and_run_func
31    return encrypting_decorator27    return encrypting_decorator
3228
3329
n34def encode_with_dict(uncoded : str, from_to_dict : dict):n30def encode_with_dict(uncoded: str, from_to_dict: dict):
35    encoded = ''31    encoded = ''
3632
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 += char37            encoded += char
42    return encoded38    return encoded
4339
4440
n45def convert_to_dictionary(plugboard_pos):n41def convert_to_dictionary(plugboard_pos: list):
46    from_to_dictionary = {}42    from_to_dictionary = {}
4743
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_dictionary48    return from_to_dictionary
5349
5450
t55def rotate_rotor_positions(rotor_positions : dict):t51def rotate_rotor_positions(rotor_positions: dict):
56    new_rotation = {}52    new_rotation = {}
5753
58    for key, value in rotor_positions.items():54    for key, value in rotor_positions.items():
59        new_rotation[value] = key55        new_rotation[value] = key
60    return new_rotation56    return new_rotation
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1f1
22
3def plugboard(uncoded : str, plugboard_pos : list):3def 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))
55
66
7def rotor(uncoded : str, from_to_dict : dict):7def 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)
99
1010
11def enigma_encrypt(plugboard_position : list, rotor_position : dict):11def 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):
n14            str = rotor(plugboard(str, plugboard_position), rotor_position)n14            str = rotor(
15                plugboard(str, plugboard_position),
16                rotor_position)
15            return func(str)17            return func(str)
16        return encrypt_and_run_func18        return encrypt_and_run_func
17    return encrypting_decorator19    return encrypting_decorator
1820
1921
20def enigma_decrypt(plugboard_position : list, rotor_position : dict):22def 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_func30        return decrypt_and_run_func
29    return encrypting_decorator31    return encrypting_decorator
3032
3133
32def encode_with_dict(uncoded : str, from_to_dict : dict):34def encode_with_dict(uncoded : str, from_to_dict : dict):
33    encoded = ''35    encoded = ''
nn36 
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 += char41            encoded += char
39    return encoded42    return encoded
4043
4144
42def convert_to_dictionary(plugboard_pos):45def convert_to_dictionary(plugboard_pos):
43    from_to_dictionary = {}46    from_to_dictionary = {}
4447
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_dictionary52    return from_to_dictionary
5053
5154
52def rotate_rotor_positions(rotor_positions : dict):55def rotate_rotor_positions(rotor_positions : dict):
n53 n
54    new_rotation = {}56    new_rotation = {}
5557
56    for key, value in rotor_positions.items():58    for key, value in rotor_positions.items():
57        new_rotation[value] = key59        new_rotation[value] = key
t58 t
59    return new_rotation60    return new_rotation
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1t1
22
3def plugboard(uncoded : str, plugboard_pos : list):3def 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))
55
66
7def rotor(uncoded : str, from_to_dict : dict):7def 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)
99
1010
11def enigma_encrypt(plugboard_position : list, rotor_position : dict):11def 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_func16        return encrypt_and_run_func
17    return encrypting_decorator17    return encrypting_decorator
1818
1919
20def enigma_decrypt(plugboard_position : list, rotor_position : dict):20def 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_func28        return decrypt_and_run_func
29    return encrypting_decorator29    return encrypting_decorator
3030
3131
32def encode_with_dict(uncoded : str, from_to_dict : dict):32def 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 += char38            encoded += char
39    return encoded39    return encoded
4040
4141
42def convert_to_dictionary(plugboard_pos):42def convert_to_dictionary(plugboard_pos):
43    from_to_dictionary = {}43    from_to_dictionary = {}
4444
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_dictionary49    return from_to_dictionary
5050
5151
52def rotate_rotor_positions(rotor_positions : dict):52def rotate_rotor_positions(rotor_positions : dict):
5353
54    new_rotation = {}54    new_rotation = {}
5555
56    for key, value in rotor_positions.items():56    for key, value in rotor_positions.items():
57        new_rotation[value] = key57        new_rotation[value] = key
5858
59    return new_rotation59    return new_rotation
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op