Домашни > Енигма > Решения > Решението на Мария Кукова

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

10 точки общо

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

 1def change_letter(letter, converting_list):
 2    for single_set in converting_list:
 3        if letter in single_set:
 4            return "".join(single_set.difference({letter}))
 5    return letter
 6
 7
 8def plugboard(text_to_convert, converting_set):
 9    converted_text = [change_letter(letter, converting_set) for letter in text_to_convert]
10    return "".join(converted_text)
11
12
13def rotor(text_to_convert, converting_dict):
14    converted_text = ""
15    for symbol in text_to_convert:
16        converted_text += converting_dict.get(symbol, ' ')
17    return converted_text
18
19
20def enigma_encrypt(plugboard_position, rotor_position):
21    def decorator(func):
22        def wrapped_func(text_to_encrypt):
23            return func(rotor(plugboard(text_to_encrypt, plugboard_position), rotor_position))
24        return wrapped_func
25    return decorator
26
27
28def enigma_decrypt(plugboard_position, rotor_position):
29    def decorator(func):
30        def wrapped_func(text_to_decrypt):
31            reverse_rotor_position = {v: k for k, v in rotor_position.items()}
32            return func(plugboard(rotor(text_to_decrypt, reverse_rotor_position), plugboard_position))
33        return wrapped_func
34    return decorator

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

OK

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

f1def change_letter(letter, converting_list):f1def change_letter(letter, converting_list):
2    for single_set in converting_list:2    for single_set in converting_list:
3        if letter in single_set:3        if letter in single_set:
4            return "".join(single_set.difference({letter}))4            return "".join(single_set.difference({letter}))
n5 n
6    return letter5    return letter
76
87
9def plugboard(text_to_convert, converting_set):8def plugboard(text_to_convert, converting_set):
10    converted_text = [change_letter(letter, converting_set) for letter in text_to_convert]9    converted_text = [change_letter(letter, converting_set) for letter in text_to_convert]
n11 n
12    return "".join(converted_text)10    return "".join(converted_text)
1311
1412
15def rotor(text_to_convert, converting_dict):13def rotor(text_to_convert, converting_dict):
16    converted_text = ""14    converted_text = ""
17    for symbol in text_to_convert:15    for symbol in text_to_convert:
n18        if symbol in converting_dict:n
19            converted_text += converting_dict[symbol]16        converted_text += converting_dict.get(symbol, ' ')
20        else:
21            converted_text += symbol
22 
23    return converted_text
24 
25 
26def rotor_decrypt(text_to_convert, converting_dict):
27    converted_text = ""
28    reverse_dict = {v: k for k, v in converting_dict.items()}
29    for symbol in text_to_convert:
30        if symbol in reverse_dict:
31            converted_text += reverse_dict[symbol]
32        else:
33            converted_text += symbol
34 
35    return converted_text17    return converted_text
3618
3719
38def enigma_encrypt(plugboard_position, rotor_position):20def enigma_encrypt(plugboard_position, rotor_position):
39    def decorator(func):21    def decorator(func):
40        def wrapped_func(text_to_encrypt):22        def wrapped_func(text_to_encrypt):
41            return func(rotor(plugboard(text_to_encrypt, plugboard_position), rotor_position))23            return func(rotor(plugboard(text_to_encrypt, plugboard_position), rotor_position))
n42 n
43        return wrapped_func24        return wrapped_func
n44 n
45    return decorator25    return decorator
4626
4727
48def enigma_decrypt(plugboard_position, rotor_position):28def enigma_decrypt(plugboard_position, rotor_position):
49    def decorator(func):29    def decorator(func):
50        def wrapped_func(text_to_decrypt):30        def wrapped_func(text_to_decrypt):
nn31            reverse_rotor_position = {v: k for k, v in rotor_position.items()}
51            return func(plugboard(rotor_decrypt(text_to_decrypt, rotor_position), plugboard_position))32            return func(plugboard(rotor(text_to_decrypt, reverse_rotor_position), plugboard_position))
52 
53        return wrapped_func33        return wrapped_func
t54 t
55    return decorator34    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op