Домашни > Енигма > Решения > Решението на Георги Джанаваров

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

7 точки общо

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

 1def plugboard(input_str, plugboard_position):
 2    new_str = ''
 3    for old_letter in input_str:
 4        plugboard_set = [containing_set for containing_set in plugboard_position if old_letter in containing_set]
 5        if plugboard_set:
 6            new_letter = [letter for letter in plugboard_set[0] if letter != old_letter][0]
 7            new_str += new_letter
 8        else:
 9            new_str += old_letter
10    return new_str
11
12def rotor(input_str, rotor_position):
13    new_str = ''
14    for letter in input_str:
15        new_str += rotor_position.get(letter,' ')
16    return new_str
17
18def enigma_encrypt(plugboard_position, rotor_position):
19    def encrypt_decorator(str_func):
20        def decorated_func(input_str):
21            str_func(rotor(plugboard(input_str, plugboard_position), rotor_position))
22        return decorated_func
23    return encrypt_decorator    
24
25def enigma_decrypt(plugboard_position, rotor_position):
26    new_rotor_position = {}
27    for key, val in rotor_position.items():
28        new_rotor_position[val] = key
29
30    def decrypt_decorator(str_func):
31        def decorated_func(input_str):
32            str_func(plugboard(rotor(input_str, new_rotor_position), plugboard_position))
33        return decorated_func
34    return decrypt_decorator    

F.FF.....
======================================================================
FAIL: 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')
AssertionError: None != 'i love python'

======================================================================
FAIL: 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'),
AssertionError: None != 'the quick brown fox jumps over the lazy dog'

======================================================================
FAIL: 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'),
AssertionError: None != 'mjg cavsk nrqfy zqd haulp qxgr mjg itob eqw'

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

FAILED (failures=3)

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

f1def plugboard(input_str, plugboard_position):f1def plugboard(input_str, plugboard_position):
2    new_str = ''2    new_str = ''
3    for old_letter in input_str:3    for old_letter in input_str:
n4        flag = 0n
5        plugboard_set = [set for set in plugboard_position if old_letter in set]4        plugboard_set = [containing_set for containing_set in plugboard_position if old_letter in containing_set]
6        if plugboard_set:5        if plugboard_set:
7            new_letter = [letter for letter in plugboard_set[0] if letter != old_letter][0]6            new_letter = [letter for letter in plugboard_set[0] if letter != old_letter][0]
8            new_str += new_letter7            new_str += new_letter
n9            flag = 1n8        else:
10        if flag == 0:
11            new_str += old_letter9            new_str += old_letter
12    return new_str10    return new_str
1311
14def rotor(input_str, rotor_position):12def rotor(input_str, rotor_position):
15    new_str = ''13    new_str = ''
16    for letter in input_str:14    for letter in input_str:
n17        if letter == ' ':n
18            new_str += letter
19            continue
20        new_str += rotor_position[letter]15        new_str += rotor_position.get(letter,' ')
21    return new_str16    return new_str
2217
23def enigma_encrypt(plugboard_position, rotor_position):18def enigma_encrypt(plugboard_position, rotor_position):
24    def encrypt_decorator(str_func):19    def encrypt_decorator(str_func):
25        def decorated_func(input_str):20        def decorated_func(input_str):
26            str_func(rotor(plugboard(input_str, plugboard_position), rotor_position))21            str_func(rotor(plugboard(input_str, plugboard_position), rotor_position))
27        return decorated_func22        return decorated_func
28    return encrypt_decorator    23    return encrypt_decorator    
2924
30def enigma_decrypt(plugboard_position, rotor_position):25def enigma_decrypt(plugboard_position, rotor_position):
31    new_rotor_position = {}26    new_rotor_position = {}
32    for key, val in rotor_position.items():27    for key, val in rotor_position.items():
33        new_rotor_position[val] = key28        new_rotor_position[val] = key
3429
35    def decrypt_decorator(str_func):30    def decrypt_decorator(str_func):
36        def decorated_func(input_str):31        def decorated_func(input_str):
37            str_func(plugboard(rotor(input_str, new_rotor_position), plugboard_position))32            str_func(plugboard(rotor(input_str, new_rotor_position), plugboard_position))
38        return decorated_func33        return decorated_func
39    return decrypt_decorator    34    return decrypt_decorator    
t40 t
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def plugboard(input_str, plugboard_position):f1def plugboard(input_str, plugboard_position):
2    new_str = ''2    new_str = ''
3    for old_letter in input_str:3    for old_letter in input_str:
4        flag = 04        flag = 0
t5        for plugboard_set in plugboard_position:t5        plugboard_set = [set for set in plugboard_position if old_letter in set]
6            if old_letter in plugboard_set:6        if plugboard_set:
7                new_letter = [letter for letter in plugboard_set if letter != old_letter][0]7            new_letter = [letter for letter in plugboard_set[0] if letter != old_letter][0]
8                new_str += new_letter8            new_str += new_letter
9                flag = 19            flag = 1
10        if flag == 0:10        if flag == 0:
11            new_str += old_letter11            new_str += old_letter
12    return new_str12    return new_str
1313
14def rotor(input_str, rotor_position):14def rotor(input_str, rotor_position):
15    new_str = ''15    new_str = ''
16    for letter in input_str:16    for letter in input_str:
17        if letter == ' ':17        if letter == ' ':
18            new_str += letter18            new_str += letter
19            continue19            continue
20        new_str += rotor_position[letter]20        new_str += rotor_position[letter]
21    return new_str21    return new_str
2222
23def enigma_encrypt(plugboard_position, rotor_position):23def enigma_encrypt(plugboard_position, rotor_position):
24    def encrypt_decorator(str_func):24    def encrypt_decorator(str_func):
25        def decorated_func(input_str):25        def decorated_func(input_str):
26            str_func(rotor(plugboard(input_str, plugboard_position), rotor_position))26            str_func(rotor(plugboard(input_str, plugboard_position), rotor_position))
27        return decorated_func27        return decorated_func
28    return encrypt_decorator    28    return encrypt_decorator    
2929
30def enigma_decrypt(plugboard_position, rotor_position):30def enigma_decrypt(plugboard_position, rotor_position):
31    new_rotor_position = {}31    new_rotor_position = {}
32    for key, val in rotor_position.items():32    for key, val in rotor_position.items():
33        new_rotor_position[val] = key33        new_rotor_position[val] = key
3434
35    def decrypt_decorator(str_func):35    def decrypt_decorator(str_func):
36        def decorated_func(input_str):36        def decorated_func(input_str):
37            str_func(plugboard(rotor(input_str, new_rotor_position), plugboard_position))37            str_func(plugboard(rotor(input_str, new_rotor_position), plugboard_position))
38        return decorated_func38        return decorated_func
39    return decrypt_decorator    39    return decrypt_decorator    
4040
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def plugboard(input_str, plugboard_position):f1def plugboard(input_str, plugboard_position):
2    new_str = ''2    new_str = ''
n3    n
4    for old_letter in input_str:3    for old_letter in input_str:
5        flag = 04        flag = 0
n6 n
7        for plugboard_set in plugboard_position:5        for plugboard_set in plugboard_position:
n8 n
9            if old_letter in plugboard_set:6            if old_letter in plugboard_set:
n10                for new_letter in plugboard_set:n7                new_letter = [letter for letter in plugboard_set if letter != old_letter][0]
11                    if new_letter != old_letter:
12                        new_str += new_letter8                new_str += new_letter
13                        flag = 19                flag = 1
14 
15        if flag == 0:10        if flag == 0:
16            new_str += old_letter11            new_str += old_letter
n17 n
18    return new_str12    return new_str
1913
20def rotor(input_str, rotor_position):14def rotor(input_str, rotor_position):
21    new_str = ''15    new_str = ''
n22 n
23    for letter in input_str:16    for letter in input_str:
nn17        if letter == ' ':
18            new_str += letter
19            continue
24        new_str += rotor_position[letter]20        new_str += rotor_position[letter]
n25 n
26    return new_str21    return new_str
2722
28def enigma_encrypt(plugboard_position, rotor_position):23def enigma_encrypt(plugboard_position, rotor_position):
n29 n
30    def encrypt_decorator(str_func):24    def encrypt_decorator(str_func):
n31 n
32        def decorated_func(input_str):25        def decorated_func(input_str):
33            str_func(rotor(plugboard(input_str, plugboard_position), rotor_position))26            str_func(rotor(plugboard(input_str, plugboard_position), rotor_position))
n34 n
35        return decorated_func27        return decorated_func
n36 n
37    return encrypt_decorator    28    return encrypt_decorator    
3829
39def enigma_decrypt(plugboard_position, rotor_position):30def enigma_decrypt(plugboard_position, rotor_position):
n40 n
41    new_rotor_position = {}31    new_rotor_position = {}
n42 n
43    for key, val in rotor_position.items():32    for key, val in rotor_position.items():
44        new_rotor_position[val] = key33        new_rotor_position[val] = key
4534
46    def decrypt_decorator(str_func):35    def decrypt_decorator(str_func):
n47 n
48        def decorated_func(input_str):36        def decorated_func(input_str):
49            str_func(plugboard(rotor(input_str, new_rotor_position), plugboard_position))37            str_func(plugboard(rotor(input_str, new_rotor_position), plugboard_position))
n50 n
51        return decorated_func38        return decorated_func
n52 n
53    return decrypt_decorator    39    return decrypt_decorator    
5440
t55 t
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op