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

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

7 точки общо

6 успешни теста
3 неуспешни теста
Код

 1def plugboard(input_string, plugboard_sets):
 2
 3    convert_rules = {}
 4    for single_set in plugboard_sets:
 5        a, b = list(single_set) # a, b = single_set works as well :)
 6        convert_rules[a] = b
 7        convert_rules[b] = a
 8
 9    result = ""
10    for symbol in input_string:
11        result += convert_rules.get(symbol, symbol)
12
13    return result
14
15
16def rotor(input_string, conversion_dict):
17    result = ""
18    for symbol in input_string:
19        result += conversion_dict[symbol]
20
21    return result
22
23
24def enigma_encrypt(plugboard_position, rotor_position):
25    def decorator(func):
26        def encrypt(text):
27            encrypted_text = plugboard(text, plugboard_position)
28            encrypted_text = rotor(encrypted_text, rotor_position)
29            return func(encrypted_text)
30        return encrypt
31    return decorator
32
33
34def enigma_decrypt(plugboard_position, rotor_position):
35    def decorator(func):
36        def decrypt(text):
37            
38            reversed_pos = {}
39            for a, b in rotor_position.items():
40                reversed_pos[b] = a
41
42            decrypted_text = ""
43            for symbol in text:
44                decrypted_text += reversed_pos.get(symbol, symbol)
45            decrypted_text = plugboard(decrypted_text, plugboard_position)
46            return func(decrypted_text)
47        return decrypt
48    return decorator

E..E....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 46, in decrypt
return func(decrypted_text)
File "/tmp/solution.py", line 28, in encrypt
encrypted_text = rotor(encrypted_text, rotor_position)
File "/tmp/solution.py", line 19, in rotor
result += conversion_dict[symbol]
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 28, in encrypt
encrypted_text = rotor(encrypted_text, rotor_position)
File "/tmp/solution.py", line 19, in rotor
result += conversion_dict[symbol]
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 19, in rotor
result += conversion_dict[symbol]
KeyError: ' '

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

FAILED (errors=3)

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

f1def plugboard(input_string, plugboard_sets):f1def plugboard(input_string, plugboard_sets):
22
3    convert_rules = {}3    convert_rules = {}
4    for single_set in plugboard_sets:4    for single_set in plugboard_sets:
5        a, b = list(single_set) # a, b = single_set works as well :)5        a, b = list(single_set) # a, b = single_set works as well :)
6        convert_rules[a] = b6        convert_rules[a] = b
7        convert_rules[b] = a7        convert_rules[b] = a
88
9    result = ""9    result = ""
10    for symbol in input_string:10    for symbol in input_string:
11        result += convert_rules.get(symbol, symbol)11        result += convert_rules.get(symbol, symbol)
1212
13    return result13    return result
1414
1515
16def rotor(input_string, conversion_dict):16def rotor(input_string, conversion_dict):
17    result = ""17    result = ""
18    for symbol in input_string:18    for symbol in input_string:
19        result += conversion_dict[symbol]19        result += conversion_dict[symbol]
2020
21    return result21    return result
2222
2323
24def enigma_encrypt(plugboard_position, rotor_position):24def enigma_encrypt(plugboard_position, rotor_position):
25    def decorator(func):25    def decorator(func):
26        def encrypt(text):26        def encrypt(text):
27            encrypted_text = plugboard(text, plugboard_position)27            encrypted_text = plugboard(text, plugboard_position)
28            encrypted_text = rotor(encrypted_text, rotor_position)28            encrypted_text = rotor(encrypted_text, rotor_position)
29            return func(encrypted_text)29            return func(encrypted_text)
30        return encrypt30        return encrypt
31    return decorator31    return decorator
3232
3333
34def enigma_decrypt(plugboard_position, rotor_position):34def enigma_decrypt(plugboard_position, rotor_position):
35    def decorator(func):35    def decorator(func):
36        def decrypt(text):36        def decrypt(text):
37            37            
38            reversed_pos = {}38            reversed_pos = {}
39            for a, b in rotor_position.items():39            for a, b in rotor_position.items():
40                reversed_pos[b] = a40                reversed_pos[b] = a
4141
42            decrypted_text = ""42            decrypted_text = ""
43            for symbol in text:43            for symbol in text:
44                decrypted_text += reversed_pos.get(symbol, symbol)44                decrypted_text += reversed_pos.get(symbol, symbol)
45            decrypted_text = plugboard(decrypted_text, plugboard_position)45            decrypted_text = plugboard(decrypted_text, plugboard_position)
46            return func(decrypted_text)46            return func(decrypted_text)
47        return decrypt47        return decrypt
48    return decorator48    return decorator
t49 t
50 
51def enigma_decrypt(plugboard_position, rotor_position):
52    def decorator(func):
53        def decrypt(text):
54            
55            reversed_pos = {}
56            for a, b in rotor_position.items():
57                reversed_pos[b] = a
58 
59            decrypted_text = ""
60            for symbol in text:
61                decrypted_text += reversed_pos.get(symbol, symbol)
62            decrypted_text = plugboard(decrypted_text, plugboard_position)
63            return func(decrypted_text)
64        return decrypt
65    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1 n
2def plugboard(input_string, plugboard_sets):1def plugboard(input_string, plugboard_sets):
32
4    convert_rules = {}3    convert_rules = {}
5    for single_set in plugboard_sets:4    for single_set in plugboard_sets:
6        a, b = list(single_set) # a, b = single_set works as well :)5        a, b = list(single_set) # a, b = single_set works as well :)
7        convert_rules[a] = b6        convert_rules[a] = b
8        convert_rules[b] = a7        convert_rules[b] = a
98
10    result = ""9    result = ""
11    for symbol in input_string:10    for symbol in input_string:
n12        if symbol in convert_rules:n
13            result += convert_rules[symbol]11        result += convert_rules.get(symbol, symbol)
14        else:
15            result += symbol
1612
17    return result13    return result
1814
1915
20def rotor(input_string, conversion_dict):16def rotor(input_string, conversion_dict):
21    result = ""17    result = ""
22    for symbol in input_string:18    for symbol in input_string:
23        result += conversion_dict[symbol]19        result += conversion_dict[symbol]
2420
25    return result21    return result
2622
2723
28def enigma_encrypt(plugboard_position, rotor_position):24def enigma_encrypt(plugboard_position, rotor_position):
29    def decorator(func):25    def decorator(func):
30        def encrypt(text):26        def encrypt(text):
31            encrypted_text = plugboard(text, plugboard_position)27            encrypted_text = plugboard(text, plugboard_position)
32            encrypted_text = rotor(encrypted_text, rotor_position)28            encrypted_text = rotor(encrypted_text, rotor_position)
33            return func(encrypted_text)29            return func(encrypted_text)
34        return encrypt30        return encrypt
35    return decorator31    return decorator
3632
3733
38def enigma_decrypt(plugboard_position, rotor_position):34def enigma_decrypt(plugboard_position, rotor_position):
39    def decorator(func):35    def decorator(func):
40        def decrypt(text):36        def decrypt(text):
41            37            
42            reversed_pos = {}38            reversed_pos = {}
43            for a, b in rotor_position.items():39            for a, b in rotor_position.items():
44                reversed_pos[b] = a40                reversed_pos[b] = a
4541
46            decrypted_text = ""42            decrypted_text = ""
47            for symbol in text:43            for symbol in text:
48                decrypted_text += reversed_pos.get(symbol, symbol)44                decrypted_text += reversed_pos.get(symbol, symbol)
49            decrypted_text = plugboard(decrypted_text, plugboard_position)45            decrypted_text = plugboard(decrypted_text, plugboard_position)
50            return func(decrypted_text)46            return func(decrypted_text)
51        return decrypt47        return decrypt
52    return decorator48    return decorator
5349
tt50 
51def enigma_decrypt(plugboard_position, rotor_position):
52    def decorator(func):
53        def decrypt(text):
54            
55            reversed_pos = {}
56            for a, b in rotor_position.items():
57                reversed_pos[b] = a
58 
59            decrypted_text = ""
60            for symbol in text:
61                decrypted_text += reversed_pos.get(symbol, symbol)
62            decrypted_text = plugboard(decrypted_text, plugboard_position)
63            return func(decrypted_text)
64        return decrypt
65    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1f1
2def plugboard(input_string, plugboard_sets):2def plugboard(input_string, plugboard_sets):
33
4    convert_rules = {}4    convert_rules = {}
5    for single_set in plugboard_sets:5    for single_set in plugboard_sets:
6        a, b = list(single_set) # a, b = single_set works as well :)6        a, b = list(single_set) # a, b = single_set works as well :)
7        convert_rules[a] = b7        convert_rules[a] = b
8        convert_rules[b] = a8        convert_rules[b] = a
99
10    result = ""10    result = ""
11    for symbol in input_string:11    for symbol in input_string:
12        if symbol in convert_rules:12        if symbol in convert_rules:
13            result += convert_rules[symbol]13            result += convert_rules[symbol]
14        else:14        else:
15            result += symbol15            result += symbol
1616
17    return result17    return result
1818
1919
20def rotor(input_string, conversion_dict):20def rotor(input_string, conversion_dict):
21    result = ""21    result = ""
22    for symbol in input_string:22    for symbol in input_string:
23        result += conversion_dict[symbol]23        result += conversion_dict[symbol]
2424
25    return result25    return result
2626
nn27 
27def enigma_encrypt(plugboard_position, rotor_position):28def enigma_encrypt(plugboard_position, rotor_position):
28    def decorator(func):29    def decorator(func):
29        def encrypt(text):30        def encrypt(text):
30            encrypted_text = plugboard(text, plugboard_position)31            encrypted_text = plugboard(text, plugboard_position)
31            encrypted_text = rotor(encrypted_text, rotor_position)32            encrypted_text = rotor(encrypted_text, rotor_position)
32            return func(encrypted_text)33            return func(encrypted_text)
33        return encrypt34        return encrypt
34    return decorator35    return decorator
3536
3637
37def enigma_decrypt(plugboard_position, rotor_position):38def enigma_decrypt(plugboard_position, rotor_position):
38    def decorator(func):39    def decorator(func):
39        def decrypt(text):40        def decrypt(text):
40            41            
41            reversed_pos = {}42            reversed_pos = {}
42            for a, b in rotor_position.items():43            for a, b in rotor_position.items():
43                reversed_pos[b] = a44                reversed_pos[b] = a
4445
t45            decrypted_text=""t46            decrypted_text = ""
46            for symbol in text:47            for symbol in text:
47                decrypted_text += reversed_pos.get(symbol, symbol)48                decrypted_text += reversed_pos.get(symbol, symbol)
48            decrypted_text = plugboard(decrypted_text, plugboard_position)49            decrypted_text = plugboard(decrypted_text, plugboard_position)
49            return func(decrypted_text)50            return func(decrypted_text)
50        return decrypt51        return decrypt
51    return decorator52    return decorator
5253
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op