Домашни > Енигма > Решения > Решението на Тодор Недковски

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

2 точки общо

3 успешни теста
6 неуспешни теста
Код
Скрий всички коментари

 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`. Това значи, че повече няма да имаш достъп до тях в кода си, което, сам се досещаш, е много лоша практика. За такива неща взимаме точки след първото домашно и първото предизвикателство. Обяснявам обстоятелствено, защото виждам, че досега не си предавал код. Преправи ги, моля.
История

f1def plugboard(input, tuples):f1def plugboard(input, tuples):
22
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 input8    return input
99
1010
11def rotor(input, dictionary):11def 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 input14    return input
1515
1616
17def enigma_encrypt(plugboard_position, rotor_position):17def enigma_encrypt(plugboard_position, rotor_position):
1818
19    def dec(func):19    def dec(func):
n20        def crypt(str):n20        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 
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)
2333
24            return func(str)34            return func(str)
25        return crypt()35        return crypt()
26    return dec36    return dec
2737
t28def 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
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1def plugboard(str, list):n1def plugboard(input, tuples):
22
n3    for set in list:n3    for binary in tuples:
4        chars = []4        chars = []
n5        for char in set:n5        for char in binary:
6            chars.append(char)6            chars.append(char)
n7        str = str.replace(chars[0], chars[1])n7        input = input.replace(chars[0], chars[1])
8    return str8    return input
99
1010
n11print(plugboard("a", [{"a", "b"}]))n11def rotor(input, dictionary):
12    for key in dictionary.keys():
13        input = input.replace(key, dictionary[key])
14    return input
1215
1316
n14def rotor(str, dict):n
15    for key in dict.keys():
16        str = str.replace(key, dict[key])
17    return str
18 
19 
20def enigma_encrypt(plugboard_position , rotor_position ):17def enigma_encrypt(plugboard_position, rotor_position):
2118
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)
2623
27            return func(str)24            return func(str)
28        return crypt()25        return crypt()
29    return dec26    return dec
3027
t31def enigma_decrypt(plugboard_position, rotor_position ):t28def 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)
3633
37            return func(str)34            return func(str)
38        return crypt()35        return crypt()
39    return dec36    return dec
4037
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1def plugboard(str, list):t1def plugboard(str, list):
22
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 str8    return str
99
1010
11print(plugboard("a", [{"a", "b"}]))11print(plugboard("a", [{"a", "b"}]))
1212
1313
14def rotor(str, dict):14def 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 str17    return str
1818
1919
20def enigma_encrypt(plugboard_position , rotor_position ):20def enigma_encrypt(plugboard_position , rotor_position ):
2121
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)
2626
27            return func(str)27            return func(str)
28        return crypt()28        return crypt()
29    return dec29    return dec
3030
31def enigma_decrypt(plugboard_position, rotor_position ):31def 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)
3636
37            return func(str)37            return func(str)
38        return crypt()38        return crypt()
39    return dec39    return dec
4040
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def plugboard(str, list):f1def plugboard(str, list):
22
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 str8    return str
99
1010
11print(plugboard("a", [{"a", "b"}]))11print(plugboard("a", [{"a", "b"}]))
1212
1313
14def rotor(str, dict):14def 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 str17    return str
1818
1919
20def enigma_encrypt(plugboard_position , rotor_position ):20def enigma_encrypt(plugboard_position , rotor_position ):
2121
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)
2626
27            return func(str)27            return func(str)
n28        return crypt();n28        return crypt()
29    return dec
2930
tt31def 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
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op