1rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',
2 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',
3 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',
4 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',
5 'm': 'r', 'c': 'k'}
6plugboard_position = [{'a', 'c'}, {'t', 'z'}]
7
8def plugboard(text, pb_pos):
9 new_text = ''
10 for letter in text:
11 for index in pb_pos:
12 new_list = list(index)
13 if letter == new_list[0]:
14 letter = new_list[1]
15 elif letter == new_list[1]:
16 letter = new_list[0]
17 new_text = new_text + letter
18 return new_text
19
20def rotor(new_text, r_pos):
21 new_encrypt = ''
22 for letter in new_text:
23 letter = r_pos.get(letter)
24 new_encrypt = new_encrypt + letter
25 return new_encrypt
26
27def enigma_encrypt(pb_pos=plugboard_position,
28 r_pos=rotor_position):
29 def wrapper(func):
30 def second_wrapper(*args, **kwargs):
31 first_result = plugboard(func(*args, **kwargs), pb_pos)
32 second_result = rotor(first_result, r_pos)
33 return second_result
34 return second_wrapper
35 return wrapper
36
37def enigma_decrypt(pb_pos=plugboard_position,
38 r_pos=rotor_position):
39 def decorator(func):
40 def wrapper(*args, **kwargs):
41 inv_rotor = {a: b for b, a in r_pos.items()}
42 first_result = rotor(func(*args, **kwargs), inv_rotor)
43 second_result = plugboard(first_result, pb_pos)
44 return second_result
45 return wrapper
46 return decorator
47
48@enigma_decrypt(plugboard_position, rotor_position)
49def random_func(text):
50 return text
EEEE....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 42, in wrapper
first_result = rotor(func(*args, **kwargs), inv_rotor)
File "/tmp/solution.py", line 32, in second_wrapper
second_result = rotor(first_result, r_pos)
File "/tmp/solution.py", line 24, in rotor
new_encrypt = new_encrypt + letter
TypeError: can only concatenate str (not "NoneType") to str
======================================================================
ERROR: 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 165, in test_correct_decorator_order
encryptor(mock)('test')
File "/tmp/solution.py", line 31, in second_wrapper
first_result = plugboard(func(*args, **kwargs), pb_pos)
File "/tmp/solution.py", line 10, in plugboard
for letter in text:
TypeError: 'Mock' object is not iterable
======================================================================
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 42, in wrapper
first_result = rotor(func(*args, **kwargs), inv_rotor)
File "/tmp/solution.py", line 24, in rotor
new_encrypt = new_encrypt + letter
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 32, in second_wrapper
second_result = rotor(first_result, r_pos)
File "/tmp/solution.py", line 24, in rotor
new_encrypt = new_encrypt + letter
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 24, in rotor
new_encrypt = new_encrypt + letter
TypeError: can only concatenate str (not "NoneType") to str
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (errors=5)
Георги Кунчев
31.10.2023 16:57Няма проблем. Тези функции не пречат на тестовете.
|
Стелиан Витанов
31.10.2023 16:25Здравейте,
Искам да попитам дали трябва да махна функцията и декоратора на редове 48-50 заедно с променливите rotor_position и plugboard_position,
или тестовете работят независимо от тях.
Поздрави,
Стелиан Витанов
|
f | 1 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | f | 1 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', |
2 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | 2 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||
3 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | 3 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||
4 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | 4 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||
5 | 'm': 'r', 'c': 'k'} | 5 | 'm': 'r', 'c': 'k'} | ||
6 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | 6 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | ||
7 | 7 | ||||
8 | def plugboard(text, pb_pos): | 8 | def plugboard(text, pb_pos): | ||
9 | new_text = '' | 9 | new_text = '' | ||
10 | for letter in text: | 10 | for letter in text: | ||
n | 11 | for index in enumerate(pb_pos): | n | 11 | for index in pb_pos: |
12 | new_list = list(pb_pos[index[0]]) | 12 | new_list = list(index) | ||
13 | if letter == new_list[0]: | 13 | if letter == new_list[0]: | ||
14 | letter = new_list[1] | 14 | letter = new_list[1] | ||
15 | elif letter == new_list[1]: | 15 | elif letter == new_list[1]: | ||
16 | letter = new_list[0] | 16 | letter = new_list[0] | ||
17 | new_text = new_text + letter | 17 | new_text = new_text + letter | ||
18 | return new_text | 18 | return new_text | ||
19 | 19 | ||||
20 | def rotor(new_text, r_pos): | 20 | def rotor(new_text, r_pos): | ||
21 | new_encrypt = '' | 21 | new_encrypt = '' | ||
22 | for letter in new_text: | 22 | for letter in new_text: | ||
n | 23 | for elem in r_pos: | n | 23 | letter = r_pos.get(letter) |
24 | if letter == elem: | ||||
25 | letter = r_pos[elem] | ||||
26 | break | ||||
27 | new_encrypt = new_encrypt + letter | 24 | new_encrypt = new_encrypt + letter | ||
28 | return new_encrypt | 25 | return new_encrypt | ||
29 | 26 | ||||
n | 30 | def enigma_encrypt(pb_pos = plugboard_position, | n | 27 | def enigma_encrypt(pb_pos=plugboard_position, |
31 | r_pos = rotor_position): | 28 | r_pos=rotor_position): | ||
32 | def wrapper(func): | 29 | def wrapper(func): | ||
33 | def second_wrapper(*args, **kwargs): | 30 | def second_wrapper(*args, **kwargs): | ||
34 | first_result = plugboard(func(*args, **kwargs), pb_pos) | 31 | first_result = plugboard(func(*args, **kwargs), pb_pos) | ||
35 | second_result = rotor(first_result, r_pos) | 32 | second_result = rotor(first_result, r_pos) | ||
36 | return second_result | 33 | return second_result | ||
37 | return second_wrapper | 34 | return second_wrapper | ||
38 | return wrapper | 35 | return wrapper | ||
39 | 36 | ||||
t | 40 | def enigma_decrypt(pb_pos = plugboard_position, | t | 37 | def enigma_decrypt(pb_pos=plugboard_position, |
41 | r_pos = rotor_position): | 38 | r_pos=rotor_position): | ||
42 | def decorator(func): | 39 | def decorator(func): | ||
43 | def wrapper(*args, **kwargs): | 40 | def wrapper(*args, **kwargs): | ||
44 | inv_rotor = {a: b for b, a in r_pos.items()} | 41 | inv_rotor = {a: b for b, a in r_pos.items()} | ||
45 | first_result = rotor(func(*args, **kwargs), inv_rotor) | 42 | first_result = rotor(func(*args, **kwargs), inv_rotor) | ||
46 | second_result = plugboard(first_result, pb_pos) | 43 | second_result = plugboard(first_result, pb_pos) | ||
47 | return second_result | 44 | return second_result | ||
48 | return wrapper | 45 | return wrapper | ||
49 | return decorator | 46 | return decorator | ||
50 | 47 | ||||
51 | @enigma_decrypt(plugboard_position, rotor_position) | 48 | @enigma_decrypt(plugboard_position, rotor_position) | ||
52 | def random_func(text): | 49 | def random_func(text): | ||
53 | return text | 50 | return text |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|