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Не знам дали декораторите съм ги направил така като изисквахте, въпреки че всичко работи.
|
f | 1 | def plugboard(text, plugboard_position): | f | 1 | def 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: | ||
n | 5 | if(letter in twin): | n | 5 | if letter in twin: |
6 | letter = ''.join(twin - {letter}) | 6 | letter = ''.join(twin - {letter}) | ||
7 | changed += letter | 7 | changed += letter | ||
8 | return changed | 8 | return changed | ||
9 | 9 | ||||
10 | def rotor(text, rotor_position): | 10 | def 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 += letter | 14 | changed += letter | ||
15 | return changed | 15 | return changed | ||
16 | 16 | ||||
n | 17 | def enigma_encrypt(plugboard_position = [], rotor_position = {}): | n | 17 | def 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 encryption | 23 | return encryption | ||
24 | return decor | 24 | return decor | ||
25 | 25 | ||||
t | 26 | def enigma_decrypt(plugboard_position = [], rotor_position = {}): | t | 26 | def 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 decryption | 32 | return decryption | ||
33 | return decor | 33 | return decor |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def plugboard(text, plugboard_position): | f | 1 | def 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 += letter | 7 | changed += letter | ||
n | 8 | text = changed | n | 8 | return changed |
9 | return text | ||||
10 | 9 | ||||
11 | def rotor(text, rotor_position): | 10 | def 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 += letter | 14 | changed += letter | ||
n | 16 | text = changed | n | 15 | return changed |
17 | return text | ||||
18 | 16 | ||||
n | 19 | def enigma_encrypt(plugboard_position, rotor_position): | n | 17 | def 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 encryption | 23 | return encryption | ||
26 | return decor | 24 | return decor | ||
27 | 25 | ||||
t | 28 | def enigma_decrypt(plugboard_position, rotor_position): | t | 26 | def 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 decryption | 32 | return decryption | ||
35 | return decor | 33 | return decor |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|