Домашни > Енигма > Решения > Решението на Михаил Цанков

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

11 точки общо

9 успешни теста
0 неуспешни теста
Код (v2)
Скрий всички коментари

 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

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

f1def convert_to_dict(plugboard_position):f1def 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 dictionary6    return dictionary
77
88
9def convert_to_dict_decorator(func):9def 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 wrapper12    return wrapper
1313
1414
15def encrypt_text_with_dict(text, dictionary):15def encrypt_text_with_dict(text, dictionary):
n16    return "".join(map(lambda key: dictionary[keyn16    return "".join(map(lambda key: dictionary.get(key, key), text))
17                        if key in dictionary 
18                        else key, 
19                        text))
2017
2118
22def reverse_dict(dictionary):19def reverse_dict(dictionary):
23    return {value: key for key, value in dictionary.items()}20    return {value: key for key, value in dictionary.items()}
2421
2522
26def reverse_dict_decorator(func):23def 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 wrapper26    return wrapper
3027
3128
32@convert_to_dict_decorator29@convert_to_dict_decorator
33def plugboard(text, plugboard_position):30def plugboard(text, plugboard_position):
34    return encrypt_text_with_dict(text, plugboard_position)31    return encrypt_text_with_dict(text, plugboard_position)
3532
3633
37def rotor(text, rotor_position):34def rotor(text, rotor_position):
38    return encrypt_text_with_dict(text, rotor_position)35    return encrypt_text_with_dict(text, rotor_position)
3936
4037
41@reverse_dict_decorator38@reverse_dict_decorator
42def rotor_decrypt(text, rotor_position):39def rotor_decrypt(text, rotor_position):
n43    return encrypt_text_with_dict(text,rotor_position)n40    return encrypt_text_with_dict(text, rotor_position)
4441
4542
46def enigma_encrypt(plugboard_position, rotor_position):43def enigma_encrypt(plugboard_position, rotor_position):
47    def decorator(func):44    def decorator(func):
48        def wrapper(text):45        def wrapper(text):
nn46            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 wrapper49        return wrapper
52    return decorator50    return decorator
5351
5452
55def enigma_decrypt(plugboard_position, rotor_position):53def enigma_decrypt(plugboard_position, rotor_position):
56    def decorator(func):54    def decorator(func):
57        def wrapper(text):55        def wrapper(text):
t58            decrypted_text = plugboard(rotor_decrypt(text, rotor_position),plugboard_position)t56            decrypted_text = plugboard(rotor_decrypt(
57                text, rotor_position), plugboard_position)
59            return func(decrypted_text)58            return func(decrypted_text)
60        return wrapper59        return wrapper
61    return decorator60    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def convert_to_dict(plugboard_position):f1def 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 dictionary6    return dictionary
77
nn8 
8def convert_to_dict_decorator(func):9def 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 wrapper12    return wrapper
nn13 
1214
13def encrypt_text_with_dict(text, dictionary):15def 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))
1820
nn21 
19def reverse_dict(dictionary):22def reverse_dict(dictionary):
20    return {value: key for key, value in dictionary.items()}23    return {value: key for key, value in dictionary.items()}
nn24 
2125
22def reverse_dict_decorator(func):26def 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 wrapper29    return wrapper
2630
nn31 
27@convert_to_dict_decorator32@convert_to_dict_decorator
28def plugboard(text, plugboard_position):33def plugboard(text, plugboard_position):
29    return encrypt_text_with_dict(text, plugboard_position)34    return encrypt_text_with_dict(text, plugboard_position)
3035
nn36 
31def rotor(text, rotor_position):37def rotor(text, rotor_position):
32    return encrypt_text_with_dict(text, rotor_position)38    return encrypt_text_with_dict(text, rotor_position)
nn39 
3340
34@reverse_dict_decorator41@reverse_dict_decorator
35def rotor_decrypt(text, rotor_position):42def rotor_decrypt(text, rotor_position):
36    return encrypt_text_with_dict(text,rotor_position)43    return encrypt_text_with_dict(text,rotor_position)
nn44 
3745
38def enigma_encrypt(plugboard_position, rotor_position):46def 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 wrapper51        return wrapper
44    return decorator52    return decorator
4553
tt54 
46def enigma_decrypt(plugboard_position, rotor_position):55def 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 wrapper60        return wrapper
52    return decorator61    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op