Домашни > Енигма > Решения > Решението на Светлозар Стефанов

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

10 точки общо

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

 1def plugboard(word, plugboard_position):
 2    changed_word = ""
 3    for letter in word:
 4        for letter_pair in plugboard_position:
 5            if letter in letter_pair:
 6                letter = list(letter_pair - {letter})[0]
 7        changed_word += letter
 8    return changed_word
 9
10def rotor(word, rotor_position):
11    changed_word = ""
12    for letter in word:
13        if letter in rotor_position:
14            letter = rotor_position[letter]
15        changed_word += letter
16    return changed_word
17
18def enigma_encrypt(plugboard_position, rotor_position):
19    def encrypt_function(func):
20        def encrypted_call(word):
21            word = plugboard(word, plugboard_position)
22            word = rotor(word, rotor_position)
23            return func(word)
24        return encrypted_call
25    return encrypt_function
26
27def enigma_decrypt(plugboard_position, rotor_position):
28    rotor_position = {v:k for k, v in rotor_position.items()}
29    def decrypt_function(func):
30        def decrypted_call(word):
31            word = rotor(word, rotor_position)
32            word = plugboard(word, plugboard_position)
33            return func(word)
34        return decrypted_call
35    return decrypt_function

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

OK

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

f1def plugboard(word, plugboard_position):f1def plugboard(word, plugboard_position):
2    changed_word = ""2    changed_word = ""
3    for letter in word:3    for letter in word:
4        for letter_pair in plugboard_position:4        for letter_pair in plugboard_position:
5            if letter in letter_pair:5            if letter in letter_pair:
6                letter = list(letter_pair - {letter})[0]6                letter = list(letter_pair - {letter})[0]
7        changed_word += letter7        changed_word += letter
8    return changed_word8    return changed_word
99
10def rotor(word, rotor_position):10def rotor(word, rotor_position):
11    changed_word = ""11    changed_word = ""
12    for letter in word:12    for letter in word:
tt13        if letter in rotor_position:
13        letter = rotor_position[letter]14            letter = rotor_position[letter]
14        changed_word += letter15        changed_word += letter
15    return changed_word16    return changed_word
1617
17def enigma_encrypt(plugboard_position, rotor_position):18def enigma_encrypt(plugboard_position, rotor_position):
18    def encrypt_function(func):19    def encrypt_function(func):
19        def encrypted_call(word):20        def encrypted_call(word):
20            word = plugboard(word, plugboard_position)21            word = plugboard(word, plugboard_position)
21            word = rotor(word, rotor_position)22            word = rotor(word, rotor_position)
22            return func(word)23            return func(word)
23        return encrypted_call24        return encrypted_call
24    return encrypt_function25    return encrypt_function
2526
26def enigma_decrypt(plugboard_position, rotor_position):27def enigma_decrypt(plugboard_position, rotor_position):
27    rotor_position = {v:k for k, v in rotor_position.items()}28    rotor_position = {v:k for k, v in rotor_position.items()}
28    def decrypt_function(func):29    def decrypt_function(func):
29        def decrypted_call(word):30        def decrypted_call(word):
30            word = rotor(word, rotor_position)31            word = rotor(word, rotor_position)
31            word = plugboard(word, plugboard_position)32            word = plugboard(word, plugboard_position)
32            return func(word)33            return func(word)
33        return decrypted_call34        return decrypted_call
34    return decrypt_function35    return decrypt_function
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op