Домашни > Енигма > Решения > Решението на Ивайло Кънчев

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

6 точки общо

5 успешни теста
4 неуспешни теста
Код

 1def plugboard(msg: str, plugboard_position: list):
 2    swaps = {}
 3
 4    for entry in plugboard_position:
 5        pair = list(entry)
 6        swaps[pair[0]] = pair[1]
 7        swaps[pair[1]] = pair[0]
 8
 9    edited_msg = ''
10    for ch in msg:
11        letter = swaps.get(ch)
12        edited_msg += ch if letter is None else letter
13
14    return edited_msg
15
16def rotor(msg: str, rotor_position: dict):
17    return ''.join(rotor_position[ch] for ch in msg)
18
19def enigma_encrypt(plugboard_position: list, rotor_position: dict):
20    def decorator (func):
21        def encrypt_message(msg):
22            msg = plugboard(msg, plugboard_position)
23            msg = rotor(msg, rotor_position)
24            return func(msg)
25        return encrypt_message
26    return decorator
27
28def enigma_decrypt(plugboard_position: list, rotor_position: dict):
29    def decorator (func):
30        def decrypt_message(msg):
31            rev_rotor_position = {}
32            for entry in rotor_position:
33                rev_rotor_position[rotor_position[entry]] = entry
34
35            msg = rotor(msg, rev_rotor_position)
36            msg = plugboard(msg, plugboard_position)
37            return func(msg)
38        return decrypt_message
39    return decorator

E.EE....E
======================================================================
ERROR: test_full_letter_set (test.TestCombination)
Test decrypting an encrypted text against itself.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 139, in test_full_letter_set
self.assertEqual(combined('i love python'), 'i love python')
File "/tmp/solution.py", line 35, in decrypt_message
msg = rotor(msg, rev_rotor_position)
File "/tmp/solution.py", line 17, in rotor
return ''.join(rotor_position[ch] for ch in msg)
File "/tmp/solution.py", line 17, in <genexpr>
return ''.join(rotor_position[ch] for ch in msg)
KeyError: ' '

======================================================================
ERROR: test_full_letter_set (test.TestDecryptor)
Test the decryptor function with all letters in the rotor.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 112, in test_full_letter_set
self.assertEqual(decrypted('mlx fuver cbakn jad guoyq aixb mlx pzhw sat'),
File "/tmp/solution.py", line 35, in decrypt_message
msg = rotor(msg, rev_rotor_position)
File "/tmp/solution.py", line 17, in rotor
return ''.join(rotor_position[ch] for ch in msg)
File "/tmp/solution.py", line 17, in <genexpr>
return ''.join(rotor_position[ch] for ch in msg)
KeyError: ' '

======================================================================
ERROR: test_full_letter_set (test.TestEncryptor)
Test the encryptor function with all letters in the rotor.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 89, in test_full_letter_set
self.assertEqual(encrypted('the quick brown fox jumps over the lazy dog'),
File "/tmp/solution.py", line 23, in encrypt_message
msg = rotor(msg, rotor_position)
File "/tmp/solution.py", line 17, in rotor
return ''.join(rotor_position[ch] for ch in msg)
File "/tmp/solution.py", line 17, in <genexpr>
return ''.join(rotor_position[ch] for ch in msg)
KeyError: ' '

======================================================================
ERROR: test_normal_case (test.TestRotor)
Test the rotor function with normally expected input.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 67, in test_normal_case
self.assertEqual(rotor('this is a test input', self.ROTOR_POSITION), 'kbjo jo c kdok jylqk')
File "/tmp/solution.py", line 17, in rotor
return ''.join(rotor_position[ch] for ch in msg)
File "/tmp/solution.py", line 17, in <genexpr>
return ''.join(rotor_position[ch] for ch in msg)
KeyError: ' '

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

FAILED (errors=4)

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

f1def plugboard(msg: str, plugboard_position: list):f1def plugboard(msg: str, plugboard_position: list):
2    swaps = {}2    swaps = {}
33
4    for entry in plugboard_position:4    for entry in plugboard_position:
5        pair = list(entry)5        pair = list(entry)
6        swaps[pair[0]] = pair[1]6        swaps[pair[0]] = pair[1]
7        swaps[pair[1]] = pair[0]7        swaps[pair[1]] = pair[0]
88
n9    alpha = [chr(i) for i in range(ord('a'), ord('z')+1)]n9    edited_msg = ''
10    for ch in alpha:10    for ch in msg:
11        if ch not in swaps:11        letter = swaps.get(ch)
12            swaps[ch] = ch12        edited_msg += ch if letter is None else letter
1313
t14    return ''.join(swaps[ch] for ch in msg)t14    return edited_msg
1515
16def rotor(msg: str, rotor_position: dict):16def rotor(msg: str, rotor_position: dict):
17    return ''.join(rotor_position[ch] for ch in msg)17    return ''.join(rotor_position[ch] for ch in msg)
1818
19def enigma_encrypt(plugboard_position: list, rotor_position: dict):19def enigma_encrypt(plugboard_position: list, rotor_position: dict):
20    def decorator (func):20    def decorator (func):
21        def encrypt_message(msg):21        def encrypt_message(msg):
22            msg = plugboard(msg, plugboard_position)22            msg = plugboard(msg, plugboard_position)
23            msg = rotor(msg, rotor_position)23            msg = rotor(msg, rotor_position)
24            return func(msg)24            return func(msg)
25        return encrypt_message25        return encrypt_message
26    return decorator26    return decorator
2727
28def enigma_decrypt(plugboard_position: list, rotor_position: dict):28def enigma_decrypt(plugboard_position: list, rotor_position: dict):
29    def decorator (func):29    def decorator (func):
30        def decrypt_message(msg):30        def decrypt_message(msg):
31            rev_rotor_position = {}31            rev_rotor_position = {}
32            for entry in rotor_position:32            for entry in rotor_position:
33                rev_rotor_position[rotor_position[entry]] = entry33                rev_rotor_position[rotor_position[entry]] = entry
3434
35            msg = rotor(msg, rev_rotor_position)35            msg = rotor(msg, rev_rotor_position)
36            msg = plugboard(msg, plugboard_position)36            msg = plugboard(msg, plugboard_position)
37            return func(msg)37            return func(msg)
38        return decrypt_message38        return decrypt_message
39    return decorator39    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def plugboard(msg: str, plugboard_position: list):f1def plugboard(msg: str, plugboard_position: list):
nn2    swaps = {}
3 
2    for entry in plugboard_position:4    for entry in plugboard_position:
3        pair = list(entry)5        pair = list(entry)
n4        msg = msg.replace(pair[0], '#')n6        swaps[pair[0]] = pair[1]
5        msg = msg.replace(pair[1], pair[0])7        swaps[pair[1]] = pair[0]
6        msg = msg.replace('#', pair[1])8 
7    return msg9    alpha = [chr(i) for i in range(ord('a'), ord('z')+1)]
10    for ch in alpha:
11        if ch not in swaps:
12            swaps[ch] = ch
13 
14    return ''.join(swaps[ch] for ch in msg)
815
9def rotor(msg: str, rotor_position: dict):16def rotor(msg: str, rotor_position: dict):
n10    result = ""n17    return ''.join(rotor_position[ch] for ch in msg)
11    for i in range(len(msg)):
12        result += rotor_position[msg[i]]
13    return result
1418
15def enigma_encrypt(plugboard_position: list, rotor_position: dict):19def enigma_encrypt(plugboard_position: list, rotor_position: dict):
16    def decorator (func):20    def decorator (func):
17        def encrypt_message(msg):21        def encrypt_message(msg):
18            msg = plugboard(msg, plugboard_position)22            msg = plugboard(msg, plugboard_position)
19            msg = rotor(msg, rotor_position)23            msg = rotor(msg, rotor_position)
20            return func(msg)24            return func(msg)
21        return encrypt_message25        return encrypt_message
22    return decorator26    return decorator
2327
24def enigma_decrypt(plugboard_position: list, rotor_position: dict):28def enigma_decrypt(plugboard_position: list, rotor_position: dict):
25    def decorator (func):29    def decorator (func):
26        def decrypt_message(msg):30        def decrypt_message(msg):
27            rev_rotor_position = {}31            rev_rotor_position = {}
28            for entry in rotor_position:32            for entry in rotor_position:
t29                rev_rotor_position.update({rotor_position[entry]: entry})t33                rev_rotor_position[rotor_position[entry]] = entry
3034
31            msg = rotor(msg, rev_rotor_position)35            msg = rotor(msg, rev_rotor_position)
32            msg = plugboard(msg, plugboard_position)36            msg = plugboard(msg, plugboard_position)
33            return func(msg)37            return func(msg)
34        return decrypt_message38        return decrypt_message
35    return decorator39    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op