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

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

10 точки общо

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

 1def plugboard(text_to_be_converted, list_of_paired_letters):
 2    letters_translation_dict = {}
 3    for first_letter, second_letter in list_of_paired_letters:
 4        letters_translation_dict[first_letter] = second_letter
 5        letters_translation_dict[second_letter] = first_letter
 6
 7    # Make a translation table from the dict with pairs and use it to replace the letters in the original string
 8    translation_table = text_to_be_converted.maketrans(letters_translation_dict)
 9    converted_text = text_to_be_converted.translate(translation_table)
10    return converted_text
11
12
13def rotor(text_to_be_converted, alphabet_conversion_dict):
14    translate_table = text_to_be_converted.maketrans(alphabet_conversion_dict)
15    converted_text = text_to_be_converted.translate(translate_table)
16    return converted_text
17
18
19def enigma_encrypt(plugboard_pos, rotor_pos):
20    def decorate_func(f):
21        def encrypt_text_wrapper(text):
22            text = plugboard(text, plugboard_pos)
23            text = rotor(text, rotor_pos)
24            return f(text)
25        return encrypt_text_wrapper
26    return decorate_func
27
28
29def enigma_decrypt(plugboard_pos, rotor_pos):
30    def decorate_func(f):
31        def decrypt_text_wrapper(text):
32            reversed_rotor_pos = dict(zip(rotor_pos.values(), rotor_pos.keys()))
33            text = rotor(text, reversed_rotor_pos)
34            text = plugboard(text, plugboard_pos)
35            return f(text)
36        return decrypt_text_wrapper
37    return decorate_func

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

OK

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

f1def plugboard(text_to_be_converted, list_of_paired_letters):f1def plugboard(text_to_be_converted, list_of_paired_letters):
2    letters_translation_dict = {}2    letters_translation_dict = {}
n3    for pair in list_of_paired_letters:n3    for first_letter, second_letter in list_of_paired_letters:
4        list_pair = list(pair)
5        first_letter, second_letter = list_pair[0], list_pair[1]
6        letters_translation_dict[first_letter] = second_letter4        letters_translation_dict[first_letter] = second_letter
7        letters_translation_dict[second_letter] = first_letter5        letters_translation_dict[second_letter] = first_letter
86
n9    # Make a translation tabel from the dict with pairs and use it to replace the letters in the original stringn7    # Make a translation table from the dict with pairs and use it to replace the letters in the original string
10    translation_tabel = text_to_be_converted.maketrans(letters_translation_dict)8    translation_table = text_to_be_converted.maketrans(letters_translation_dict)
11    converted_text = text_to_be_converted.translate(translation_tabel)9    converted_text = text_to_be_converted.translate(translation_table)
12    return converted_text10    return converted_text
1311
1412
15def rotor(text_to_be_converted, alphabet_conversion_dict):13def rotor(text_to_be_converted, alphabet_conversion_dict):
n16    translate_tabel = text_to_be_converted.maketrans(alphabet_conversion_dict)n14    translate_table = text_to_be_converted.maketrans(alphabet_conversion_dict)
17    converted_text = text_to_be_converted.translate(translate_tabel)15    converted_text = text_to_be_converted.translate(translate_table)
18    return converted_text16    return converted_text
1917
2018
21def enigma_encrypt(plugboard_pos, rotor_pos):19def enigma_encrypt(plugboard_pos, rotor_pos):
22    def decorate_func(f):20    def decorate_func(f):
23        def encrypt_text_wrapper(text):21        def encrypt_text_wrapper(text):
24            text = plugboard(text, plugboard_pos)22            text = plugboard(text, plugboard_pos)
25            text = rotor(text, rotor_pos)23            text = rotor(text, rotor_pos)
26            return f(text)24            return f(text)
27        return encrypt_text_wrapper25        return encrypt_text_wrapper
28    return decorate_func26    return decorate_func
2927
3028
31def enigma_decrypt(plugboard_pos, rotor_pos):29def enigma_decrypt(plugboard_pos, rotor_pos):
32    def decorate_func(f):30    def decorate_func(f):
33        def decrypt_text_wrapper(text):31        def decrypt_text_wrapper(text):
34            reversed_rotor_pos = dict(zip(rotor_pos.values(), rotor_pos.keys()))32            reversed_rotor_pos = dict(zip(rotor_pos.values(), rotor_pos.keys()))
35            text = rotor(text, reversed_rotor_pos)33            text = rotor(text, reversed_rotor_pos)
36            text = plugboard(text, plugboard_pos)34            text = plugboard(text, plugboard_pos)
37            return f(text)35            return f(text)
38        return decrypt_text_wrapper36        return decrypt_text_wrapper
39    return decorate_func37    return decorate_func
t40 t
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op