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

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

8 точки общо

7 успешни теста
2 неуспешни теста
Код

 1def plugboard(text, plugboard_position):
 2    result = ""
 3    for letter in text:
 4        into_another_letter = letter
 5        for plug_set in plugboard_position:
 6            if letter in plug_set:
 7                into_another_letter = (plug_set - {letter}).pop()
 8                break
 9        result += into_another_letter
10    return result
11
12def rotor_two_ways(text, rotor_position, encrypt_decrypt):
13    result = ""
14    for letter in text:
15        for unchanged, changed in rotor_position.items():
16            if encrypt_decrypt == 'encrypt' and letter == unchanged:
17                result += changed
18                break
19            elif encrypt_decrypt == 'decrypt' and letter == changed:
20                result += unchanged
21                break
22        else:
23            result += letter
24    return result
25
26def rotor(text, rotor_position):
27    return rotor_two_ways(text, rotor_position, 'encrypt')
28
29def enigma_encrypt(plugboard_position, rotor_position):
30    def enigma_decorator(func):
31        def encrypt(text):
32            return rotor(plugboard(text, plugboard_position), rotor_position)
33        return encrypt
34    return enigma_decorator
35
36def enigma_decrypt(plugboard_position, rotor_position):
37    def enigma_decorator(func):
38        def decrypt(text):
39            result = rotor_two_ways(text, rotor_position, 'decrypt')
40            return plugboard(result, plugboard_position)
41        return decrypt
42    return enigma_decorator

FF.......
======================================================================
FAIL: 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')
AssertionError: 'a gnjd korynm' != 'i love python'
- a gnjd korynm
+ i love python

======================================================================
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 940, in assert_called_once_with
raise AssertionError(msg)
AssertionError: Expected 'mock' to be called once. Called 0 times.

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

FAILED (failures=2)

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

f1def plugboard(text, plugboard_position):f1def plugboard(text, plugboard_position):
2    result = ""2    result = ""
3    for letter in text:3    for letter in text:
4        into_another_letter = letter4        into_another_letter = letter
5        for plug_set in plugboard_position:5        for plug_set in plugboard_position:
6            if letter in plug_set:6            if letter in plug_set:
7                into_another_letter = (plug_set - {letter}).pop()7                into_another_letter = (plug_set - {letter}).pop()
8                break8                break
9        result += into_another_letter9        result += into_another_letter
10    return result10    return result
1111
12def rotor_two_ways(text, rotor_position, encrypt_decrypt):12def rotor_two_ways(text, rotor_position, encrypt_decrypt):
13    result = ""13    result = ""
14    for letter in text:14    for letter in text:
n15        result += lettern
16        for unchanged, changed in rotor_position.items():15        for unchanged, changed in rotor_position.items():
17            if encrypt_decrypt == 'encrypt' and letter == unchanged:16            if encrypt_decrypt == 'encrypt' and letter == unchanged:
n18                result = result[:-1]n
19                result += rotor_position[unchanged]17                result += changed
18                break
20            elif encrypt_decrypt == 'decrypt' and letter == rotor_position[unchanged]:19            elif encrypt_decrypt == 'decrypt' and letter == changed:
21                result = result[:-1]
22                result += unchanged20                result += unchanged
nn21                break
22        else:
23            result += letter
23    return result24    return result
2425
25def rotor(text, rotor_position):26def rotor(text, rotor_position):
26    return rotor_two_ways(text, rotor_position, 'encrypt')27    return rotor_two_ways(text, rotor_position, 'encrypt')
2728
28def enigma_encrypt(plugboard_position, rotor_position):29def enigma_encrypt(plugboard_position, rotor_position):
29    def enigma_decorator(func):30    def enigma_decorator(func):
30        def encrypt(text):31        def encrypt(text):
31            return rotor(plugboard(text, plugboard_position), rotor_position)32            return rotor(plugboard(text, plugboard_position), rotor_position)
32        return encrypt33        return encrypt
33    return enigma_decorator34    return enigma_decorator
3435
35def enigma_decrypt(plugboard_position, rotor_position):36def enigma_decrypt(plugboard_position, rotor_position):
36    def enigma_decorator(func):37    def enigma_decorator(func):
37        def decrypt(text):38        def decrypt(text):
38            result = rotor_two_ways(text, rotor_position, 'decrypt')39            result = rotor_two_ways(text, rotor_position, 'decrypt')
39            return plugboard(result, plugboard_position)40            return plugboard(result, plugboard_position)
40        return decrypt41        return decrypt
41    return enigma_decorator42    return enigma_decorator
tt43 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1def plugboard(str, plugboard_position):n1def plugboard(text, plugboard_position):
2    result = ""2    result = ""
n3    for letter in str:n3    for letter in text:
4        result = result + letter4        into_another_letter = letter
5        for into_another_letter in plugboard_position:5        for plug_set in plugboard_position:
6            if letter in into_another_letter:6            if letter in plug_set:
7                for changed_letter in into_another_letter:7                into_another_letter = (plug_set - {letter}).pop()
8                    if letter != changed_letter:
9                        result = result[:-1]
10                        result = result + changed_letter
11                        break
12                break8                break
nn9        result += into_another_letter
13    return result10    return result
1411
n15def rotor(str, rotor_position):n12def rotor_two_ways(text, rotor_position, encrypt_decrypt):
16    result = ""13    result = ""
n17    for letter in str:n14    for letter in text:
18        result += letter15        result += letter
n19        for unchanged in rotor_position:n16        for unchanged, changed in rotor_position.items():
20            if letter == unchanged:17            if encrypt_decrypt == 'encrypt' and letter == unchanged:
21                result = result[:-1]18                result = result[:-1]
22                result += rotor_position[unchanged]19                result += rotor_position[unchanged]
nn20            elif encrypt_decrypt == 'decrypt' and letter == rotor_position[unchanged]:
21                result = result[:-1]
22                result += unchanged
23    return result23    return result
nn24 
25def rotor(text, rotor_position):
26    return rotor_two_ways(text, rotor_position, 'encrypt')
2427
25def enigma_encrypt(plugboard_position, rotor_position):28def enigma_encrypt(plugboard_position, rotor_position):
26    def enigma_decorator(func):29    def enigma_decorator(func):
27        def encrypt(text):30        def encrypt(text):
28            return rotor(plugboard(text, plugboard_position), rotor_position)31            return rotor(plugboard(text, plugboard_position), rotor_position)
29        return encrypt32        return encrypt
30    return enigma_decorator33    return enigma_decorator
3134
32def enigma_decrypt(plugboard_position, rotor_position):35def enigma_decrypt(plugboard_position, rotor_position):
33    def enigma_decorator(func):36    def enigma_decorator(func):
n34        def encrypt(text):n37        def decrypt(text):
35            result = ""38            result = rotor_two_ways(text, rotor_position, 'decrypt')
36            for letter in text:
37                result += letter
38                for unchanged in rotor_position:
39                    if letter == rotor_position[unchanged]:
40                        result = result[:-1]
41                        result += unchanged
42            return plugboard(result, plugboard_position)39            return plugboard(result, plugboard_position)
n43        return encryptn40        return decrypt
44    return enigma_decorator41    return enigma_decorator
t45 t
46 
47 
48 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op