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

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

6 точки общо

5 успешни теста
4 неуспешни теста
Код

 1def plugboard(text, plugboard_position):
 2    changed = ''
 3    for letter in text:
 4        for twin in plugboard_position:
 5            if letter in twin:
 6                letter = ''.join(twin - {letter})
 7        changed += letter
 8    return changed
 9        
10def rotor(text, rotor_position):
11    changed = ''
12    for letter in text:
13        letter = rotor_position[letter]
14        changed += letter
15    return changed
16        
17def enigma_encrypt(plugboard_position=[], rotor_position={}):
18    def decor(func):
19        def encryption(text):
20            text = plugboard(text, plugboard_position)
21            text = rotor(text, rotor_position)
22            return func(text)
23        return encryption
24    return decor
25        
26def enigma_decrypt(plugboard_position=[], rotor_position={}):
27    def decor(func):
28        def decryption(text):
29            text = rotor(text, {a: b for b, a in rotor_position.items()})
30            text = plugboard(text, plugboard_position)
31            return func(text)
32        return decryption
33    return decor

E.EE....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 29, in decryption
text = rotor(text, {a: b for b, a in rotor_position.items()})
File "/tmp/solution.py", line 13, in rotor
letter = rotor_position[letter]
KeyError: ' '

======================================================================
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 29, in decryption
text = rotor(text, {a: b for b, a in rotor_position.items()})
File "/tmp/solution.py", line 13, in rotor
letter = rotor_position[letter]
KeyError: ' '

======================================================================
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 21, in encryption
text = rotor(text, rotor_position)
File "/tmp/solution.py", line 13, in rotor
letter = rotor_position[letter]
KeyError: ' '

======================================================================
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 13, in rotor
letter = rotor_position[letter]
KeyError: ' '

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

FAILED (errors=4)

Дискусия
Деян Диков
30.10.2023 20:03

Не знам дали декораторите съм ги направил така като изисквахте, въпреки че всичко работи.
История

f1def plugboard(text, plugboard_position):f1def plugboard(text, plugboard_position):
2    changed = ''2    changed = ''
3    for letter in text:3    for letter in text:
4        for twin in plugboard_position:4        for twin in plugboard_position:
n5            if(letter in twin):n5            if letter in twin:
6                letter = ''.join(twin - {letter})6                letter = ''.join(twin - {letter})
7        changed += letter7        changed += letter
8    return changed8    return changed
9        9        
10def rotor(text, rotor_position):10def rotor(text, rotor_position):
11    changed = ''11    changed = ''
12    for letter in text:12    for letter in text:
13        letter = rotor_position[letter]13        letter = rotor_position[letter]
14        changed += letter14        changed += letter
15    return changed15    return changed
16        16        
n17def enigma_encrypt(plugboard_position = [], rotor_position = {}):n17def enigma_encrypt(plugboard_position=[], rotor_position={}):
18    def decor(func):18    def decor(func):
19        def encryption(text):19        def encryption(text):
20            text = plugboard(text, plugboard_position)20            text = plugboard(text, plugboard_position)
21            text = rotor(text, rotor_position)21            text = rotor(text, rotor_position)
22            return func(text)22            return func(text)
23        return encryption23        return encryption
24    return decor24    return decor
25        25        
t26def enigma_decrypt(plugboard_position = [], rotor_position = {}):t26def enigma_decrypt(plugboard_position=[], rotor_position={}):
27    def decor(func):27    def decor(func):
28        def decryption(text):28        def decryption(text):
29            text = rotor(text, {a: b for b, a in rotor_position.items()})29            text = rotor(text, {a: b for b, a in rotor_position.items()})
30            text = plugboard(text, plugboard_position)30            text = plugboard(text, plugboard_position)
31            return func(text)31            return func(text)
32        return decryption32        return decryption
33    return decor33    return decor
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def plugboard(text, plugboard_position):f1def plugboard(text, plugboard_position):
2    changed = ''2    changed = ''
3    for letter in text:3    for letter in text:
4        for twin in plugboard_position:4        for twin in plugboard_position:
5            if(letter in twin):5            if(letter in twin):
6                letter = ''.join(twin - {letter})6                letter = ''.join(twin - {letter})
7        changed += letter7        changed += letter
n8    text = changedn8    return changed
9    return text
10        9        
11def rotor(text, rotor_position):10def rotor(text, rotor_position):
12    changed = ''11    changed = ''
13    for letter in text:12    for letter in text:
14        letter = rotor_position[letter]13        letter = rotor_position[letter]
15        changed += letter14        changed += letter
n16    text = changedn15    return changed
17    return text
18        16        
n19def enigma_encrypt(plugboard_position, rotor_position):n17def enigma_encrypt(plugboard_position = [], rotor_position = {}):
20    def decor(func):18    def decor(func):
21        def encryption(text):19        def encryption(text):
22            text = plugboard(text, plugboard_position)20            text = plugboard(text, plugboard_position)
23            text = rotor(text, rotor_position)21            text = rotor(text, rotor_position)
24            return func(text)22            return func(text)
25        return encryption23        return encryption
26    return decor24    return decor
27        25        
t28def enigma_decrypt(plugboard_position, rotor_position):t26def enigma_decrypt(plugboard_position = [], rotor_position = {}):
29    def decor(func):27    def decor(func):
30        def decryption(text):28        def decryption(text):
31            text = rotor(text, {a: b for b, a in rotor_position.items()})29            text = rotor(text, {a: b for b, a in rotor_position.items()})
32            text = plugboard(text, plugboard_position)30            text = plugboard(text, plugboard_position)
33            return func(text)31            return func(text)
34        return decryption32        return decryption
35    return decor33    return decor
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op