1def plugboard(word, plugboard_position):
2 new_word = ''
3 for letter in word:
4 for each_set in plugboard_position:
5 if letter in each_set:
6 other_letter = each_set - {letter}
7 new_word += list(other_letter).pop()
8 break
9 else:
10 new_word += letter
11 return new_word
12
13def rotor(word, rotor_position):
14 new_word = ''
15 for letter in word:
16 new_word += rotor_position[letter]
17 return new_word
18
19def enigma_encrypt(plugboard_position, rotor_position):
20 def encrypted_word(func):
21 def transformed_word(word):
22 return func(rotor(plugboard(word, plugboard_position), rotor_position))
23 return transformed_word
24 return encrypted_word
25
26def enigma_decrypt(plugboard_position, rotor_position):
27 rotor_position = {value: key for key, value in rotor_position.items()}
28 def decrypted_word(func):
29 def transformed_word(word):
30 return func(plugboard(rotor(word, rotor_position), plugboard_position))
31 return transformed_word
32 return decrypted_word
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 30, in transformed_word
return func(plugboard(rotor(word, rotor_position), plugboard_position))
File "/tmp/solution.py", line 16, in rotor
new_word += 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 30, in transformed_word
return func(plugboard(rotor(word, rotor_position), plugboard_position))
File "/tmp/solution.py", line 16, in rotor
new_word += 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 22, in transformed_word
return func(rotor(plugboard(word, plugboard_position), rotor_position))
File "/tmp/solution.py", line 16, in rotor
new_word += 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 16, in rotor
new_word += rotor_position[letter]
KeyError: ' '
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (errors=4)
Константин Кирязов
28.10.2023 00:07Предишното качено решение беше грешен файл. <br>
oops
|
f | 1 | def plugboard(word, plugboard_position): | f | 1 | def plugboard(word, plugboard_position): |
2 | new_word = '' | 2 | new_word = '' | ||
n | 3 | word = [*word] | n | ||
4 | for letter in word: | 3 | for letter in word: | ||
n | 5 | letter_is_replaced = False | n | ||
6 | for set in plugboard_position: | 4 | for each_set in plugboard_position: | ||
7 | if letter in set: | 5 | if letter in each_set: | ||
8 | other_letter = set - {letter} | 6 | other_letter = each_set - {letter} | ||
9 | new_word += list(other_letter).pop() | 7 | new_word += list(other_letter).pop() | ||
n | 10 | letter_is_replaced = True | n | 8 | break |
11 | if not letter_is_replaced: | 9 | else: | ||
12 | new_word += letter | 10 | new_word += letter | ||
13 | return new_word | 11 | return new_word | ||
14 | 12 | ||||
15 | def rotor(word, rotor_position): | 13 | def rotor(word, rotor_position): | ||
16 | new_word = '' | 14 | new_word = '' | ||
t | 17 | word = [*word] | t | ||
18 | for letter in word: | 15 | for letter in word: | ||
19 | new_word += rotor_position[letter] | 16 | new_word += rotor_position[letter] | ||
20 | return new_word | 17 | return new_word | ||
21 | 18 | ||||
22 | def enigma_encrypt(plugboard_position, rotor_position): | 19 | def enigma_encrypt(plugboard_position, rotor_position): | ||
23 | def encrypted_word(func): | 20 | def encrypted_word(func): | ||
24 | def transformed_word(word): | 21 | def transformed_word(word): | ||
25 | return func(rotor(plugboard(word, plugboard_position), rotor_position)) | 22 | return func(rotor(plugboard(word, plugboard_position), rotor_position)) | ||
26 | return transformed_word | 23 | return transformed_word | ||
27 | return encrypted_word | 24 | return encrypted_word | ||
28 | 25 | ||||
29 | def enigma_decrypt(plugboard_position, rotor_position): | 26 | def enigma_decrypt(plugboard_position, rotor_position): | ||
30 | rotor_position = {value: key for key, value in rotor_position.items()} | 27 | rotor_position = {value: key for key, value in rotor_position.items()} | ||
31 | def decrypted_word(func): | 28 | def decrypted_word(func): | ||
32 | def transformed_word(word): | 29 | def transformed_word(word): | ||
33 | return func(plugboard(rotor(word, rotor_position), plugboard_position)) | 30 | return func(plugboard(rotor(word, rotor_position), plugboard_position)) | ||
34 | return transformed_word | 31 | return transformed_word | ||
35 | return decrypted_word | 32 | return decrypted_word |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
n | 1 | #HELP | n | ||
2 | def plugboard(word, plugboard_position): | 1 | def plugboard(word, plugboard_position): | ||
3 | new_word = '' | 2 | new_word = '' | ||
4 | word = [*word] | 3 | word = [*word] | ||
5 | for letter in word: | 4 | for letter in word: | ||
n | n | 5 | letter_is_replaced = False | ||
6 | for set in plugboard_position: | 6 | for set in plugboard_position: | ||
7 | if letter in set: | 7 | if letter in set: | ||
8 | other_letter = set - {letter} | 8 | other_letter = set - {letter} | ||
9 | new_word += list(other_letter).pop() | 9 | new_word += list(other_letter).pop() | ||
n | 10 | new_word += letter #?????????????????????? | n | 10 | letter_is_replaced = True |
11 | if not letter_is_replaced: | ||||
12 | new_word += letter | ||||
11 | return new_word | 13 | return new_word | ||
12 | 14 | ||||
n | 13 | #tests | n | ||
14 | #plugboard_position = [{'a', 'c'}, {'t', 'z'}] | ||||
15 | #plugboard('enigma', plugboard_position) | ||||
16 | |||||
17 | #DONE | ||||
18 | def rotor(word, rotor_position): | 15 | def rotor(word, rotor_position): | ||
19 | new_word = '' | 16 | new_word = '' | ||
20 | word = [*word] | 17 | word = [*word] | ||
21 | for letter in word: | 18 | for letter in word: | ||
22 | new_word += rotor_position[letter] | 19 | new_word += rotor_position[letter] | ||
23 | return new_word | 20 | return new_word | ||
24 | 21 | ||||
n | 25 | #tests | n | ||
26 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||||
27 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||||
28 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||||
29 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||||
30 | 'm': 'r', 'c': 'k'} | ||||
31 | rotor('enigma', rotor_position) | ||||
32 | |||||
33 | def enigma_encrypt(plugboard_position, rotor_position): | 22 | def enigma_encrypt(plugboard_position, rotor_position): | ||
n | 34 | return | n | 23 | def encrypted_word(func): |
24 | def transformed_word(word): | ||||
25 | return func(rotor(plugboard(word, plugboard_position), rotor_position)) | ||||
26 | return transformed_word | ||||
27 | return encrypted_word | ||||
35 | 28 | ||||
36 | def enigma_decrypt(plugboard_position, rotor_position): | 29 | def enigma_decrypt(plugboard_position, rotor_position): | ||
t | 37 | return | t | 30 | rotor_position = {value: key for key, value in rotor_position.items()} |
38 | 31 | def decrypted_word(func): | |||
39 | 32 | def transformed_word(word): | |||
40 | 33 | return func(plugboard(rotor(word, rotor_position), plugboard_position)) | |||
34 | return transformed_word | ||||
35 | return decrypted_word |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|