1def plugboard(input, tuples):
2
3 for binary in tuples:
4 chars = []
5 for char in binary:
6 chars.append(char)
7 input = input.replace(chars[0], chars[1])
8 return input
9
10
11def rotor(input, dictionary):
12 for key in dictionary.keys():
13 input = input.replace(key, dictionary[key])
14 return input
15
16
17def enigma_encrypt(plugboard_position, rotor_position):
18
19 def dec(func):
20 def crypt(input):
21 input = plugboard(input, plugboard_position)
22 input = rotor(input, rotor_position)
23
24 return func(input)
25 return crypt()
26 return dec
27
28def enigma_decrypt(plugboard_position, rotor_position):
29 def dec(func):
30 def crypt(input):
31 input = plugboard(input, plugboard_position)
32 input = rotor(input, rotor_position)
33
34 return func(str)
35 return crypt()
36 return dec
EEEE..F.F
======================================================================
ERROR: test_full_letter_set (test.TestCombination)
Test decrypting an encrypted text against itself.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 138, in test_full_letter_set
combined = decryptor(encryptor(TEST_FUN))
File "/tmp/solution.py", line 25, in dec
return crypt()
TypeError: enigma_encrypt.<locals>.dec.<locals>.crypt() missing 1 required positional argument: 'input'
======================================================================
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 25, in dec
return crypt()
TypeError: enigma_encrypt.<locals>.dec.<locals>.crypt() missing 1 required positional argument: 'input'
======================================================================
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 111, in test_full_letter_set
decrypted = decryptor(TEST_FUN)
File "/tmp/solution.py", line 35, in dec
return crypt()
TypeError: enigma_decrypt.<locals>.dec.<locals>.crypt() missing 1 required positional argument: 'input'
======================================================================
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 88, in test_full_letter_set
encrypted = encryptor(TEST_FUN)
File "/tmp/solution.py", line 25, in dec
return crypt()
TypeError: enigma_encrypt.<locals>.dec.<locals>.crypt() missing 1 required positional argument: 'input'
======================================================================
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: 'ihis is z iesi inpui' != 'ihts ts z iysi tnpui'
- ihis is z iesi inpui
+ ihts ts z iysi tnpui
======================================================================
FAIL: 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')
AssertionError: 'kbpo po p kdok ptlqk' != 'kbjo jo c kdok jylqk'
- kbpo po p kdok ptlqk
? ^ ^ ^ ^^
+ kbjo jo c kdok jylqk
? ^ ^ ^ ^^
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (failures=2, errors=4)
Тодор Недковски
25.10.2023 22:38Благодаря за обратната връзка!
|
Георги Кунчев
25.10.2023 21:44Използваш имена на параметри за функции и променливи, които съвпадат със запазените думи, които сочат към типовете данни - `str`, `list`, `dict`. Това значи, че повече няма да имаш достъп до тях в кода си, което, сам се досещаш, е много лоша практика. За такива неща взимаме точки след първото домашно и първото предизвикателство.
Обяснявам обстоятелствено, защото виждам, че досега не си предавал код. Преправи ги, моля.
|
| f | 1 | def plugboard(input, tuples): | f | 1 | def plugboard(input, tuples): |
| 2 | 2 | ||||
| 3 | for binary in tuples: | 3 | for binary in tuples: | ||
| 4 | chars = [] | 4 | chars = [] | ||
| 5 | for char in binary: | 5 | for char in binary: | ||
| 6 | chars.append(char) | 6 | chars.append(char) | ||
| 7 | input = input.replace(chars[0], chars[1]) | 7 | input = input.replace(chars[0], chars[1]) | ||
| 8 | return input | 8 | return input | ||
| 9 | 9 | ||||
| 10 | 10 | ||||
| 11 | def rotor(input, dictionary): | 11 | def rotor(input, dictionary): | ||
| 12 | for key in dictionary.keys(): | 12 | for key in dictionary.keys(): | ||
| 13 | input = input.replace(key, dictionary[key]) | 13 | input = input.replace(key, dictionary[key]) | ||
| 14 | return input | 14 | return input | ||
| 15 | 15 | ||||
| 16 | 16 | ||||
| 17 | def enigma_encrypt(plugboard_position, rotor_position): | 17 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 18 | 18 | ||||
| 19 | def dec(func): | 19 | def dec(func): | ||
| n | 20 | def crypt(str): | n | 20 | def crypt(input): |
| 21 | str = plugboard(str, plugboard_position) | 21 | input = plugboard(input, plugboard_position) | ||
| 22 | str = rotor(str, rotor_position) | 22 | input = rotor(input, rotor_position) | ||
| 23 | |||||
| 24 | return func(input) | ||||
| 25 | return crypt() | ||||
| 26 | return dec | ||||
| 27 | |||||
| 28 | def enigma_decrypt(plugboard_position, rotor_position): | ||||
| 29 | def dec(func): | ||||
| 30 | def crypt(input): | ||||
| 31 | input = plugboard(input, plugboard_position) | ||||
| 32 | input = rotor(input, rotor_position) | ||||
| 23 | 33 | ||||
| 24 | return func(str) | 34 | return func(str) | ||
| 25 | return crypt() | 35 | return crypt() | ||
| 26 | return dec | 36 | return dec | ||
| 27 | 37 | ||||
| t | 28 | def enigma_decrypt(plugboard_position, rotor_position): | t | ||
| 29 | def dec(func): | ||||
| 30 | def crypt(str): | ||||
| 31 | str = plugboard(str, plugboard_position) | ||||
| 32 | str = rotor(str, rotor_position) | ||||
| 33 | |||||
| 34 | return func(str) | ||||
| 35 | return crypt() | ||||
| 36 | return dec | ||||
| 37 |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| n | 1 | def plugboard(str, list): | n | 1 | def plugboard(input, tuples): |
| 2 | 2 | ||||
| n | 3 | for set in list: | n | 3 | for binary in tuples: |
| 4 | chars = [] | 4 | chars = [] | ||
| n | 5 | for char in set: | n | 5 | for char in binary: |
| 6 | chars.append(char) | 6 | chars.append(char) | ||
| n | 7 | str = str.replace(chars[0], chars[1]) | n | 7 | input = input.replace(chars[0], chars[1]) |
| 8 | return str | 8 | return input | ||
| 9 | 9 | ||||
| 10 | 10 | ||||
| n | 11 | print(plugboard("a", [{"a", "b"}])) | n | 11 | def rotor(input, dictionary): |
| 12 | for key in dictionary.keys(): | ||||
| 13 | input = input.replace(key, dictionary[key]) | ||||
| 14 | return input | ||||
| 12 | 15 | ||||
| 13 | 16 | ||||
| n | 14 | def rotor(str, dict): | n | ||
| 15 | for key in dict.keys(): | ||||
| 16 | str = str.replace(key, dict[key]) | ||||
| 17 | return str | ||||
| 18 | |||||
| 19 | |||||
| 20 | def enigma_encrypt(plugboard_position , rotor_position ): | 17 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 21 | 18 | ||||
| 22 | def dec(func): | 19 | def dec(func): | ||
| 23 | def crypt(str): | 20 | def crypt(str): | ||
| 24 | str = plugboard(str, plugboard_position) | 21 | str = plugboard(str, plugboard_position) | ||
| 25 | str = rotor(str, rotor_position) | 22 | str = rotor(str, rotor_position) | ||
| 26 | 23 | ||||
| 27 | return func(str) | 24 | return func(str) | ||
| 28 | return crypt() | 25 | return crypt() | ||
| 29 | return dec | 26 | return dec | ||
| 30 | 27 | ||||
| t | 31 | def enigma_decrypt(plugboard_position, rotor_position ): | t | 28 | def enigma_decrypt(plugboard_position, rotor_position): |
| 32 | def dec(func): | 29 | def dec(func): | ||
| 33 | def crypt(str): | 30 | def crypt(str): | ||
| 34 | str = plugboard(str, plugboard_position) | 31 | str = plugboard(str, plugboard_position) | ||
| 35 | str = rotor(str, rotor_position) | 32 | str = rotor(str, rotor_position) | ||
| 36 | 33 | ||||
| 37 | return func(str) | 34 | return func(str) | ||
| 38 | return crypt() | 35 | return crypt() | ||
| 39 | return dec | 36 | return dec | ||
| 40 | 37 |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| t | 1 | def plugboard(str, list): | t | 1 | def plugboard(str, list): |
| 2 | 2 | ||||
| 3 | for set in list: | 3 | for set in list: | ||
| 4 | chars = [] | 4 | chars = [] | ||
| 5 | for char in set: | 5 | for char in set: | ||
| 6 | chars.append(char) | 6 | chars.append(char) | ||
| 7 | str = str.replace(chars[0], chars[1]) | 7 | str = str.replace(chars[0], chars[1]) | ||
| 8 | return str | 8 | return str | ||
| 9 | 9 | ||||
| 10 | 10 | ||||
| 11 | print(plugboard("a", [{"a", "b"}])) | 11 | print(plugboard("a", [{"a", "b"}])) | ||
| 12 | 12 | ||||
| 13 | 13 | ||||
| 14 | def rotor(str, dict): | 14 | def rotor(str, dict): | ||
| 15 | for key in dict.keys(): | 15 | for key in dict.keys(): | ||
| 16 | str = str.replace(key, dict[key]) | 16 | str = str.replace(key, dict[key]) | ||
| 17 | return str | 17 | return str | ||
| 18 | 18 | ||||
| 19 | 19 | ||||
| 20 | def enigma_encrypt(plugboard_position , rotor_position ): | 20 | def enigma_encrypt(plugboard_position , rotor_position ): | ||
| 21 | 21 | ||||
| 22 | def dec(func): | 22 | def dec(func): | ||
| 23 | def crypt(str): | 23 | def crypt(str): | ||
| 24 | str = plugboard(str, plugboard_position) | 24 | str = plugboard(str, plugboard_position) | ||
| 25 | str = rotor(str, rotor_position) | 25 | str = rotor(str, rotor_position) | ||
| 26 | 26 | ||||
| 27 | return func(str) | 27 | return func(str) | ||
| 28 | return crypt() | 28 | return crypt() | ||
| 29 | return dec | 29 | return dec | ||
| 30 | 30 | ||||
| 31 | def enigma_decrypt(plugboard_position, rotor_position ): | 31 | def enigma_decrypt(plugboard_position, rotor_position ): | ||
| 32 | def dec(func): | 32 | def dec(func): | ||
| 33 | def crypt(str): | 33 | def crypt(str): | ||
| 34 | str = plugboard(str, plugboard_position) | 34 | str = plugboard(str, plugboard_position) | ||
| 35 | str = rotor(str, rotor_position) | 35 | str = rotor(str, rotor_position) | ||
| 36 | 36 | ||||
| 37 | return func(str) | 37 | return func(str) | ||
| 38 | return crypt() | 38 | return crypt() | ||
| 39 | return dec | 39 | return dec | ||
| 40 | 40 |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | def plugboard(str, list): | f | 1 | def plugboard(str, list): |
| 2 | 2 | ||||
| 3 | for set in list: | 3 | for set in list: | ||
| 4 | chars = [] | 4 | chars = [] | ||
| 5 | for char in set: | 5 | for char in set: | ||
| 6 | chars.append(char) | 6 | chars.append(char) | ||
| 7 | str = str.replace(chars[0], chars[1]) | 7 | str = str.replace(chars[0], chars[1]) | ||
| 8 | return str | 8 | return str | ||
| 9 | 9 | ||||
| 10 | 10 | ||||
| 11 | print(plugboard("a", [{"a", "b"}])) | 11 | print(plugboard("a", [{"a", "b"}])) | ||
| 12 | 12 | ||||
| 13 | 13 | ||||
| 14 | def rotor(str, dict): | 14 | def rotor(str, dict): | ||
| 15 | for key in dict.keys(): | 15 | for key in dict.keys(): | ||
| 16 | str = str.replace(key, dict[key]) | 16 | str = str.replace(key, dict[key]) | ||
| 17 | return str | 17 | return str | ||
| 18 | 18 | ||||
| 19 | 19 | ||||
| 20 | def enigma_encrypt(plugboard_position , rotor_position ): | 20 | def enigma_encrypt(plugboard_position , rotor_position ): | ||
| 21 | 21 | ||||
| 22 | def dec(func): | 22 | def dec(func): | ||
| 23 | def crypt(str): | 23 | def crypt(str): | ||
| 24 | str = plugboard(str, plugboard_position) | 24 | str = plugboard(str, plugboard_position) | ||
| 25 | str = rotor(str, rotor_position) | 25 | str = rotor(str, rotor_position) | ||
| 26 | 26 | ||||
| 27 | return func(str) | 27 | return func(str) | ||
| n | 28 | return crypt(); | n | 28 | return crypt() |
| 29 | return dec | ||||
| 29 | 30 | ||||
| t | t | 31 | def enigma_decrypt(plugboard_position, rotor_position ): | ||
| 32 | def dec(func): | ||||
| 33 | def crypt(str): | ||||
| 34 | str = plugboard(str, plugboard_position) | ||||
| 35 | str = rotor(str, rotor_position) | ||||
| 36 | |||||
| 37 | return func(str) | ||||
| 38 | return crypt() | ||||
| 39 | return dec | ||||
| 40 |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
25.10.2023 22:44