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

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

6 точки общо

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

 1def plugboard(str_to_convert, list_with_sets):
 2    list_of_lists = [[first, second] for [first, second] in list_with_sets]
 3    list_of_str = list(str_to_convert)
 4    for i, character in enumerate(list_of_str):
 5        for pair_in_list in list_of_lists:
 6            if character == pair_in_list[0]:
 7                list_of_str[i] = pair_in_list[1]
 8            elif character == pair_in_list[1]:
 9                list_of_str[i] = pair_in_list[0]
10                
11    return ''.join(list_of_str)
12
13
14def rotor(str_to_convert, dict_for_convertion):
15    list_of_str = list(str_to_convert)
16    for i,character in enumerate(list_of_str):
17        list_of_str[i] = dict_for_convertion[character]
18        
19    return ''.join(list_of_str)
20
21
22def enigma_encrypt(plugboard_position=[], rotor_position={}):
23    def decorator(func):
24        def encrypt(str_to_encrypt):
25            str_to_encrypt = plugboard(str_to_encrypt, plugboard_position)
26            str_to_encrypt = rotor(str_to_encrypt, rotor_position)
27            return func(str_to_encrypt)
28        return encrypt
29    return decorator
30
31
32def enigma_decrypt(plugboard_position=[], rotor_position={}):
33    def decorator(func):
34        def decrypt(str_to_decrypt):
35            new_rotor_position = dict((value, key) for key, value in rotor_position.items())
36            str_to_decrypt = rotor(str_to_decrypt, new_rotor_position)
37            str_to_decrypt = plugboard(str_to_decrypt, plugboard_position)
38            return func(str_to_decrypt)
39        return decrypt
40    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 36, in decrypt
str_to_decrypt = rotor(str_to_decrypt, new_rotor_position)
File "/tmp/solution.py", line 17, in rotor
list_of_str[i] = dict_for_convertion[character]
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 36, in decrypt
str_to_decrypt = rotor(str_to_decrypt, new_rotor_position)
File "/tmp/solution.py", line 17, in rotor
list_of_str[i] = dict_for_convertion[character]
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 26, in encrypt
str_to_encrypt = rotor(str_to_encrypt, rotor_position)
File "/tmp/solution.py", line 17, in rotor
list_of_str[i] = dict_for_convertion[character]
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
list_of_str[i] = dict_for_convertion[character]
KeyError: ' '

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

FAILED (errors=4)

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

f1def plugboard(str_to_convert, list_with_sets):f1def plugboard(str_to_convert, list_with_sets):
2    list_of_lists = [[first, second] for [first, second] in list_with_sets]2    list_of_lists = [[first, second] for [first, second] in list_with_sets]
3    list_of_str = list(str_to_convert)3    list_of_str = list(str_to_convert)
n4    for i,character in enumerate(list_of_str):n4    for i, character in enumerate(list_of_str):
5        for pair_in_list in list_of_lists:5        for pair_in_list in list_of_lists:
6            if character == pair_in_list[0]:6            if character == pair_in_list[0]:
7                list_of_str[i] = pair_in_list[1]7                list_of_str[i] = pair_in_list[1]
8            elif character == pair_in_list[1]:8            elif character == pair_in_list[1]:
9                list_of_str[i] = pair_in_list[0]9                list_of_str[i] = pair_in_list[0]
10                10                
11    return ''.join(list_of_str)11    return ''.join(list_of_str)
1212
1313
14def rotor(str_to_convert, dict_for_convertion):14def rotor(str_to_convert, dict_for_convertion):
15    list_of_str = list(str_to_convert)15    list_of_str = list(str_to_convert)
16    for i,character in enumerate(list_of_str):16    for i,character in enumerate(list_of_str):
17        list_of_str[i] = dict_for_convertion[character]17        list_of_str[i] = dict_for_convertion[character]
18        18        
19    return ''.join(list_of_str)19    return ''.join(list_of_str)
2020
2121
n22def enigma_encrypt(plugboard_position = [], rotor_position = {}):n22def enigma_encrypt(plugboard_position=[], rotor_position={}):
23    def decorator(func):23    def decorator(func):
24        def encrypt(str_to_encrypt):24        def encrypt(str_to_encrypt):
25            str_to_encrypt = plugboard(str_to_encrypt, plugboard_position)25            str_to_encrypt = plugboard(str_to_encrypt, plugboard_position)
26            str_to_encrypt = rotor(str_to_encrypt, rotor_position)26            str_to_encrypt = rotor(str_to_encrypt, rotor_position)
27            return func(str_to_encrypt)27            return func(str_to_encrypt)
28        return encrypt28        return encrypt
29    return decorator29    return decorator
3030
3131
n32def enigma_decrypt(plugboard_position = [], rotor_position = {}):n32def enigma_decrypt(plugboard_position=[], rotor_position={}):
33    def decorator(func):33    def decorator(func):
34        def decrypt(str_to_decrypt):34        def decrypt(str_to_decrypt):
35            new_rotor_position = dict((value, key) for key, value in rotor_position.items())35            new_rotor_position = dict((value, key) for key, value in rotor_position.items())
t36            str_to_decrypt = rotor(str_to_decrypt,new_rotor_position)t36            str_to_decrypt = rotor(str_to_decrypt, new_rotor_position)
37            str_to_decrypt = plugboard(str_to_decrypt, plugboard_position)37            str_to_decrypt = plugboard(str_to_decrypt, plugboard_position)
38            return func(str_to_decrypt)38            return func(str_to_decrypt)
39        return decrypt39        return decrypt
40    return decorator40    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op