Домашни > Енигма > Решения > Решението на Адем Црънчалиев

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

3 точки общо

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

 1def plugboard(input_str, list_input):
 2    result = ''
 3    for character in input_str:
 4        set_found = find_set_by_key(list_input, character)
 5
 6        if set_found is not None:
 7            result = result + set_found[1]
 8        else:
 9            result = result + character
10
11    return result
12
13def find_set_by_key(input_set, key_to_find):
14
15    for set_item in input_set:
16        my_list = list(set_item)
17        
18        if key_to_find == my_list[0]:
19            return my_list
20        
21    return None
22
23def rotor(input_str, dict_input):
24    result = ''
25    for character in input_str:
26        value_to_replace = dict_input.get(character)
27        result = result + value_to_replace
28
29    return result
30
31def enigma_encrypt(plugboard_position, rotor_position):
32    def encrypt_decorator(func):
33        def wrapper(text):
34            text = plugboard(text, plugboard_position)
35            text = rotor(text, rotor_position)
36            func(text)
37        return wrapper
38    return encrypt_decorator
39
40def enigma_decrypt(plugboard_position, rotor_position):
41    def decrypt_decorator(func):
42        def wrapper(text):
43            flipped_dict = {value: key for key, value in rotor_position.items()}
44
45            text = rotor(text, flipped_dict)
46            text = plugboard(text, plugboard_position)
47            func(text)
48        return wrapper
49    return decrypt_decorator

EFEE..F.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 45, in wrapper
text = rotor(text, flipped_dict)
File "/tmp/solution.py", line 27, in rotor
result = result + value_to_replace
TypeError: can only concatenate str (not "NoneType") to str

======================================================================
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 45, in wrapper
text = rotor(text, flipped_dict)
File "/tmp/solution.py", line 27, in rotor
result = result + value_to_replace
TypeError: can only concatenate str (not "NoneType") to str

======================================================================
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 35, in wrapper
text = rotor(text, rotor_position)
File "/tmp/solution.py", line 27, in rotor
result = result + value_to_replace
TypeError: can only concatenate str (not "NoneType") to str

======================================================================
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 27, in rotor
result = result + value_to_replace
TypeError: can only concatenate str (not "NoneType") to str

======================================================================
FAIL: test_correct_decorator_order (test.TestDecorators)
Test whether the decorator is applying the functions in correct order.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 166, in test_correct_decorator_order
mock.assert_called_once_with('bumb')
File "/usr/lib/python3.10/unittest/mock.py", line 941, in assert_called_once_with
return self.assert_called_with(*args, **kwargs)
File "/usr/lib/python3.10/unittest/mock.py", line 929, in assert_called_with
raise AssertionError(_error_message()) from cause
AssertionError: expected call not found.
Expected: mock('bumb')
Actual: mock('bubb')

======================================================================
FAIL: test_normal_case (test.TestPlugboard)
Test the plugboard function with normally expected input.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 45, in test_normal_case
self.assertEqual(plugboard('this is a test input', plugboard_position), 'ihts ts z iysi tnpui')
AssertionError: 'thts ts z tyst tnput' != 'ihts ts z iysi tnpui'
- thts ts z tyst tnput
? ^ ^ ^ ^
+ ihts ts z iysi tnpui
? ^ ^ ^ ^

----------------------------------------------------------------------
Ran 9 tests in 0.002s

FAILED (failures=2, errors=4)

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

f1def plugboard(input_str, list_input):f1def plugboard(input_str, list_input):
n2 n
3    result = ''2    result = ''
4    for character in input_str:3    for character in input_str:
5        set_found = find_set_by_key(list_input, character)4        set_found = find_set_by_key(list_input, character)
65
n7        if set_found != None:n6        if set_found is not None:
8            value = set_found[1]
9            result = result+value7            result = result + set_found[1]
10        else:8        else:
n11            result = result+charactern9            result = result + character
1210
13    return result11    return result
1412
15def find_set_by_key(input_set, key_to_find):13def find_set_by_key(input_set, key_to_find):
1614
17    for set_item in input_set:15    for set_item in input_set:
18        my_list = list(set_item)16        my_list = list(set_item)
n19 n
20        key = my_list[0]
21        17        
n22        if(key_to_find == key):n18        if key_to_find == my_list[0]:
23            return my_list19            return my_list
24        20        
25    return None21    return None
2622
27def rotor(input_str, dict_input):23def rotor(input_str, dict_input):
28    result = ''24    result = ''
29    for character in input_str:25    for character in input_str:
30        value_to_replace = dict_input.get(character)26        value_to_replace = dict_input.get(character)
31        result = result + value_to_replace27        result = result + value_to_replace
3228
33    return result29    return result
3430
35def enigma_encrypt(plugboard_position, rotor_position):31def enigma_encrypt(plugboard_position, rotor_position):
36    def encrypt_decorator(func):32    def encrypt_decorator(func):
37        def wrapper(text):33        def wrapper(text):
38            text = plugboard(text, plugboard_position)34            text = plugboard(text, plugboard_position)
39            text = rotor(text, rotor_position)35            text = rotor(text, rotor_position)
40            func(text)36            func(text)
41        return wrapper37        return wrapper
42    return encrypt_decorator38    return encrypt_decorator
4339
44def enigma_decrypt(plugboard_position, rotor_position):40def enigma_decrypt(plugboard_position, rotor_position):
45    def decrypt_decorator(func):41    def decrypt_decorator(func):
46        def wrapper(text):42        def wrapper(text):
n47            text = rotor(text, rotor_position)n43            flipped_dict = {value: key for key, value in rotor_position.items()}
44 
45            text = rotor(text, flipped_dict)
48            text = plugboard(text, plugboard_position)46            text = plugboard(text, plugboard_position)
49            func(text)47            func(text)
50        return wrapper48        return wrapper
51    return decrypt_decorator49    return decrypt_decorator
5250
t53 t
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op