Домашни > Енигма > Решения > Решението на Теодор Костадинов

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

10 точки общо

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

 1"""Solution NOT based on the fact that there are only lowercase letters"""
 2
 3
 4def replace_text(text, dictionary):
 5    return "".join([dictionary.get(char, char) for char in text])
 6
 7
 8def invert_dictionary(dictionary):
 9    return {value: key for key, value in dictionary.items()}
10
11
12def plugboard(message, plugboard_position):
13    plugboard_dictionary = dict(plugboard_position)
14    plugboard_dictionary |= invert_dictionary(plugboard_dictionary)
15    return replace_text(message, plugboard_dictionary)
16
17
18def rotor(message, rotor_position):
19    return replace_text(message, rotor_position)
20
21
22def enigma_encrypt(plugboard_position, rotor_position):
23    def decorator(function):
24        def wrapper(text):
25            text_plugboard = plugboard(text, plugboard_position)
26            text_rotor = rotor(text_plugboard, rotor_position)
27            return function(text_rotor)
28
29        return wrapper
30
31    return decorator
32
33
34def enigma_decrypt(plugboard_position, rotor_position):
35    def decorator(function):
36        def wrapper(text):
37            inverted_rotor_position = invert_dictionary(rotor_position)
38            text_rotor = rotor(text, inverted_rotor_position)
39            text_plugboard = plugboard(text_rotor, plugboard_position)
40            return function(text_plugboard)
41
42        return wrapper
43
44    return decorator

.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s

OK

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

f1"""Solution NOT based on the fact that there are only lowercase letters"""f1"""Solution NOT based on the fact that there are only lowercase letters"""
22
33
4def replace_text(text, dictionary):4def replace_text(text, dictionary):
t5    return "".join(t5    return "".join([dictionary.get(char, char) for char in text])
6        [
7            dictionary[old_char] if old_char in dictionary.keys() else old_char
8            for old_char in text
9        ]
10    )
116
127
13def invert_dictionary(dictionary):8def invert_dictionary(dictionary):
14    return {value: key for key, value in dictionary.items()}9    return {value: key for key, value in dictionary.items()}
1510
1611
17def plugboard(message, plugboard_position):12def plugboard(message, plugboard_position):
18    plugboard_dictionary = dict(plugboard_position)13    plugboard_dictionary = dict(plugboard_position)
19    plugboard_dictionary |= invert_dictionary(plugboard_dictionary)14    plugboard_dictionary |= invert_dictionary(plugboard_dictionary)
20    return replace_text(message, plugboard_dictionary)15    return replace_text(message, plugboard_dictionary)
2116
2217
23def rotor(message, rotor_position):18def rotor(message, rotor_position):
24    return replace_text(message, rotor_position)19    return replace_text(message, rotor_position)
2520
2621
27def enigma_encrypt(plugboard_position, rotor_position):22def enigma_encrypt(plugboard_position, rotor_position):
28    def decorator(function):23    def decorator(function):
29        def wrapper(text):24        def wrapper(text):
30            text_plugboard = plugboard(text, plugboard_position)25            text_plugboard = plugboard(text, plugboard_position)
31            text_rotor = rotor(text_plugboard, rotor_position)26            text_rotor = rotor(text_plugboard, rotor_position)
32            return function(text_rotor)27            return function(text_rotor)
3328
34        return wrapper29        return wrapper
3530
36    return decorator31    return decorator
3732
3833
39def enigma_decrypt(plugboard_position, rotor_position):34def enigma_decrypt(plugboard_position, rotor_position):
40    def decorator(function):35    def decorator(function):
41        def wrapper(text):36        def wrapper(text):
42            inverted_rotor_position = invert_dictionary(rotor_position)37            inverted_rotor_position = invert_dictionary(rotor_position)
43            text_rotor = rotor(text, inverted_rotor_position)38            text_rotor = rotor(text, inverted_rotor_position)
44            text_plugboard = plugboard(text_rotor, plugboard_position)39            text_plugboard = plugboard(text_rotor, plugboard_position)
45            return function(text_plugboard)40            return function(text_plugboard)
4641
47        return wrapper42        return wrapper
4843
49    return decorator44    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1"""Solution based on the fact that there are only lowercase letters"""n1"""Solution NOT based on the fact that there are only lowercase letters"""
22
33
n4def replace_with_upper_case(text, old_char, new_char):n4def replace_text(text, dictionary):
5    return text.replace(old_char, new_char.capitalize())5    return "".join(
6        [
7            dictionary[old_char] if old_char in dictionary.keys() else old_char
8            for old_char in text
9        ]
10    )
11 
12 
13def invert_dictionary(dictionary):
14    return {value: key for key, value in dictionary.items()}
615
716
8def plugboard(message, plugboard_position):17def plugboard(message, plugboard_position):
n9    output = messagen18    plugboard_dictionary = dict(plugboard_position)
10 19    plugboard_dictionary |= invert_dictionary(plugboard_dictionary)
11    for left_char, right_char in plugboard_position:20    return replace_text(message, plugboard_dictionary)
12        output = replace_with_upper_case(output, left_char, right_char)
13        output = replace_with_upper_case(output, right_char, left_char)
14 
15    return output.lower()
1621
1722
18def rotor(message, rotor_position):23def rotor(message, rotor_position):
n19    output = messagen24    return replace_text(message, rotor_position)
20 
21    for key, value in rotor_position.items():
22        output = replace_with_upper_case(output, key, value)
23 
24    return output.lower()
2525
2626
27def enigma_encrypt(plugboard_position, rotor_position):27def enigma_encrypt(plugboard_position, rotor_position):
28    def decorator(function):28    def decorator(function):
29        def wrapper(text):29        def wrapper(text):
30            text_plugboard = plugboard(text, plugboard_position)30            text_plugboard = plugboard(text, plugboard_position)
31            text_rotor = rotor(text_plugboard, rotor_position)31            text_rotor = rotor(text_plugboard, rotor_position)
32            return function(text_rotor)32            return function(text_rotor)
3333
34        return wrapper34        return wrapper
3535
36    return decorator36    return decorator
3737
3838
t39def invert_dictionary(dictionary):t
40    return {value: key for key, value in dictionary.items()}
41 
42 
43def enigma_decrypt(plugboard_position, rotor_position):39def enigma_decrypt(plugboard_position, rotor_position):
44    def decorator(function):40    def decorator(function):
45        def wrapper(text):41        def wrapper(text):
46            inverted_rotor_position = invert_dictionary(rotor_position)42            inverted_rotor_position = invert_dictionary(rotor_position)
47            text_rotor = rotor(text, inverted_rotor_position)43            text_rotor = rotor(text, inverted_rotor_position)
48            text_plugboard = plugboard(text_rotor, plugboard_position)44            text_plugboard = plugboard(text_rotor, plugboard_position)
49            return function(text_plugboard)45            return function(text_plugboard)
5046
51        return wrapper47        return wrapper
5248
53    return decorator49    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op