Домашни > Енигма > Решения > Решението на Веселина Велкова

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

10 точки общо

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

 1def translation(letter, coupled_letters):
 2    """ Finding the coupled letter of the input letter, if such exists. """
 3
 4    for (first, second) in coupled_letters:
 5        if letter == first:
 6            return second
 7        elif letter == second:
 8            return first
 9    return letter
10
11
12def plugboard(sentence, coupled_letters):
13    """ Plugboard usage. """
14
15    list_sentence = [translation(char, coupled_letters) for char in sentence]
16    return "".join(list_sentence)
17
18
19def rotor(sentence, dict_alphabet):
20    """ Rotor usage. """
21
22    new_sentence = "".join(map(lambda letter: dict_alphabet[letter] if letter.isalpha() else letter, sentence))
23    return new_sentence
24
25
26def enigma_encrypt(plugboard_position, rotor_position):
27    def decorator_encrypt(func):
28        def encrypt(sentence):
29
30            sentence = plugboard(sentence, plugboard_position)
31            sentence = rotor(sentence, rotor_position)
32
33            return func(sentence)
34        return encrypt
35    return decorator_encrypt
36
37
38def enigma_decrypt(plugboard_position, rotor_position):
39    def decorator_decrypt(func):
40        def decrypt(sentence):
41
42            new_rotor_position = dict(zip(rotor_position.values(), rotor_position.keys()))
43            sentence = rotor(sentence, new_rotor_position)
44            sentence = plugboard(sentence, plugboard_position)
45
46            return func(sentence)
47        return decrypt
48    return decorator_decrypt

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

OK

Дискусия
Виктор Бечев
31.10.2023 15:35

Отвъд дребните забележки - чисто решение. :)
История

f1def translation(letter, coupled_letters):f1def translation(letter, coupled_letters):
2    """ Finding the coupled letter of the input letter, if such exists. """2    """ Finding the coupled letter of the input letter, if such exists. """
33
4    for (first, second) in coupled_letters:4    for (first, second) in coupled_letters:
5        if letter == first:5        if letter == first:
6            return second6            return second
7        elif letter == second:7        elif letter == second:
8            return first8            return first
9    return letter9    return letter
1010
1111
12def plugboard(sentence, coupled_letters):12def plugboard(sentence, coupled_letters):
13    """ Plugboard usage. """13    """ Plugboard usage. """
1414
15    list_sentence = [translation(char, coupled_letters) for char in sentence]15    list_sentence = [translation(char, coupled_letters) for char in sentence]
16    return "".join(list_sentence)16    return "".join(list_sentence)
1717
1818
19def rotor(sentence, dict_alphabet):19def rotor(sentence, dict_alphabet):
20    """ Rotor usage. """20    """ Rotor usage. """
2121
22    new_sentence = "".join(map(lambda letter: dict_alphabet[letter] if letter.isalpha() else letter, sentence))22    new_sentence = "".join(map(lambda letter: dict_alphabet[letter] if letter.isalpha() else letter, sentence))
23    return new_sentence23    return new_sentence
2424
2525
n26def enigma_encrypt(*, plugboard_position, rotor_position):n26def enigma_encrypt(plugboard_position, rotor_position):
27    def decorator_encrypt(func):27    def decorator_encrypt(func):
28        def encrypt(sentence):28        def encrypt(sentence):
2929
30            sentence = plugboard(sentence, plugboard_position)30            sentence = plugboard(sentence, plugboard_position)
31            sentence = rotor(sentence, rotor_position)31            sentence = rotor(sentence, rotor_position)
3232
33            return func(sentence)33            return func(sentence)
34        return encrypt34        return encrypt
35    return decorator_encrypt35    return decorator_encrypt
3636
3737
t38def enigma_decrypt(*, plugboard_position, rotor_position):t38def enigma_decrypt(plugboard_position, rotor_position):
39    def decorator_decrypt(func):39    def decorator_decrypt(func):
40        def decrypt(sentence):40        def decrypt(sentence):
4141
42            new_rotor_position = dict(zip(rotor_position.values(), rotor_position.keys()))42            new_rotor_position = dict(zip(rotor_position.values(), rotor_position.keys()))
43            sentence = rotor(sentence, new_rotor_position)43            sentence = rotor(sentence, new_rotor_position)
44            sentence = plugboard(sentence, plugboard_position)44            sentence = plugboard(sentence, plugboard_position)
4545
46            return func(sentence)46            return func(sentence)
47        return decrypt47        return decrypt
48    return decorator_decrypt48    return decorator_decrypt
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op