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

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

10 точки общо

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

 1def plugboard(text, plugboard_position):
 2    def match(letter):
 3        for x, y in plugboard_position:
 4            if letter == x:
 5                return y
 6            if letter == y:
 7                return x
 8        return letter
 9    return ''.join([match(letter) for letter in text])
10
11def rotor(text, rotor_position):
12    return ''.join([rotor_position.get(letter, letter) for letter in text])
13
14def enigma_encrypt(plugboard_position, rotor_position):
15    def encryptor(func):
16        def encrypt_func(text):
17            encrypted = rotor(plugboard(text, plugboard_position), rotor_position)
18            return func(encrypted)
19        return encrypt_func
20    return encryptor
21
22def enigma_decrypt(plugboard_position, rotor_position):
23    def decryptor(func):
24        def decrypt_func(text):
25            reverse_rotor = {value: key for key, value in rotor_position.items()}
26            decrypted = plugboard(rotor(text, reverse_rotor), plugboard_position)
27            return func(decrypted)
28        return decrypt_func
29    return decryptor

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

OK

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

f1def plugboard(text, plugboard_position):f1def plugboard(text, plugboard_position):
2    def match(letter):2    def match(letter):
3        for x, y in plugboard_position:3        for x, y in plugboard_position:
4            if letter == x:4            if letter == x:
5                return y5                return y
6            if letter == y:6            if letter == y:
7                return x7                return x
8        return letter8        return letter
9    return ''.join([match(letter) for letter in text])9    return ''.join([match(letter) for letter in text])
1010
11def rotor(text, rotor_position):11def rotor(text, rotor_position):
t12    return ''.join([value if letter == key else '' for letter in text for key, value in rotor_position.items()])t12    return ''.join([rotor_position.get(letter, letter) for letter in text])
1313
14def enigma_encrypt(plugboard_position, rotor_position):14def enigma_encrypt(plugboard_position, rotor_position):
15    def encryptor(func):15    def encryptor(func):
16        def encrypt_func(text):16        def encrypt_func(text):
17            encrypted = rotor(plugboard(text, plugboard_position), rotor_position)17            encrypted = rotor(plugboard(text, plugboard_position), rotor_position)
18            return func(encrypted)18            return func(encrypted)
19        return encrypt_func19        return encrypt_func
20    return encryptor20    return encryptor
2121
22def enigma_decrypt(plugboard_position, rotor_position):22def enigma_decrypt(plugboard_position, rotor_position):
23    def decryptor(func):23    def decryptor(func):
24        def decrypt_func(text):24        def decrypt_func(text):
25            reverse_rotor = {value: key for key, value in rotor_position.items()}25            reverse_rotor = {value: key for key, value in rotor_position.items()}
26            decrypted = plugboard(rotor(text, reverse_rotor), plugboard_position)26            decrypted = plugboard(rotor(text, reverse_rotor), plugboard_position)
27            return func(decrypted)27            return func(decrypted)
28        return decrypt_func28        return decrypt_func
29    return decryptor29    return decryptor
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def plugboard(text, plugboard_position):f1def plugboard(text, plugboard_position):
2    def match(letter):2    def match(letter):
3        for x, y in plugboard_position:3        for x, y in plugboard_position:
4            if letter == x:4            if letter == x:
5                return y5                return y
6            if letter == y:6            if letter == y:
7                return x7                return x
8        return letter8        return letter
9    return ''.join([match(letter) for letter in text])9    return ''.join([match(letter) for letter in text])
1010
11def rotor(text, rotor_position):11def rotor(text, rotor_position):
t12    return ''.join([value if letter == key else '' for letter in text for key in rotor_position for value in rotor_position[key]])t12    return ''.join([value if letter == key else '' for letter in text for key, value in rotor_position.items()])
1313
14def enigma_encrypt(plugboard_position, rotor_position):14def enigma_encrypt(plugboard_position, rotor_position):
15    def encryptor(func):15    def encryptor(func):
16        def encrypt_func(text):16        def encrypt_func(text):
17            encrypted = rotor(plugboard(text, plugboard_position), rotor_position)17            encrypted = rotor(plugboard(text, plugboard_position), rotor_position)
18            return func(encrypted)18            return func(encrypted)
19        return encrypt_func19        return encrypt_func
20    return encryptor20    return encryptor
2121
22def enigma_decrypt(plugboard_position, rotor_position):22def enigma_decrypt(plugboard_position, rotor_position):
23    def decryptor(func):23    def decryptor(func):
24        def decrypt_func(text):24        def decrypt_func(text):
25            reverse_rotor = {value: key for key, value in rotor_position.items()}25            reverse_rotor = {value: key for key, value in rotor_position.items()}
26            decrypted = plugboard(rotor(text, reverse_rotor), plugboard_position)26            decrypted = plugboard(rotor(text, reverse_rotor), plugboard_position)
27            return func(decrypted)27            return func(decrypted)
28        return decrypt_func28        return decrypt_func
29    return decryptor29    return decryptor
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def plugboard(text, plugboard_position):f1def plugboard(text, plugboard_position):
2    def match(letter):2    def match(letter):
3        for x, y in plugboard_position:3        for x, y in plugboard_position:
4            if letter == x:4            if letter == x:
5                return y5                return y
6            if letter == y:6            if letter == y:
7                return x7                return x
8        return letter8        return letter
t9    return ''.join([match(x) for x in text])t9    return ''.join([match(letter) for letter in text])
1010
11def rotor(text, rotor_position):11def rotor(text, rotor_position):
12    return ''.join([value if letter == key else '' for letter in text for key in rotor_position for value in rotor_position[key]])12    return ''.join([value if letter == key else '' for letter in text for key in rotor_position for value in rotor_position[key]])
1313
14def enigma_encrypt(plugboard_position, rotor_position):14def enigma_encrypt(plugboard_position, rotor_position):
15    def encryptor(func):15    def encryptor(func):
16        def encrypt_func(text):16        def encrypt_func(text):
17            encrypted = rotor(plugboard(text, plugboard_position), rotor_position)17            encrypted = rotor(plugboard(text, plugboard_position), rotor_position)
18            return func(encrypted)18            return func(encrypted)
19        return encrypt_func19        return encrypt_func
20    return encryptor20    return encryptor
2121
22def enigma_decrypt(plugboard_position, rotor_position):22def enigma_decrypt(plugboard_position, rotor_position):
23    def decryptor(func):23    def decryptor(func):
24        def decrypt_func(text):24        def decrypt_func(text):
25            reverse_rotor = {value: key for key, value in rotor_position.items()}25            reverse_rotor = {value: key for key, value in rotor_position.items()}
26            decrypted = plugboard(rotor(text, reverse_rotor), plugboard_position)26            decrypted = plugboard(rotor(text, reverse_rotor), plugboard_position)
27            return func(decrypted)27            return func(decrypted)
28        return decrypt_func28        return decrypt_func
29    return decryptor29    return decryptor
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op