1def plugboard(word, swaps):
2 swaps_dict = {}
3 for swap in swaps:
4 first_letter, second_letter = tuple(swap)
5 swaps_dict.update({first_letter: second_letter, second_letter: first_letter})
6 return ''.join(map(lambda n: swaps_dict[n] if n in swaps_dict.keys() else n, word))
7
8
9def rotor(word, swaps):
10 return ''.join(map(lambda n: swaps[n], word))
11
12def rotor_decrypt(word, swaps):
13 reversed_swaps = {v: k for k, v in swaps.items()}
14 return ''.join(map(lambda n: reversed_swaps[n], word))
15
16def enigma_encrypt(plugboard_position, rotor_position):
17 def encrypt_decorator(func):
18 def wrapper(word):
19 word = rotor(plugboard(word, plugboard_position), rotor_position)
20 return func(word)
21 return wrapper
22 return encrypt_decorator
23
24def enigma_decrypt(plugboard_position, rotor_position):
25 def decrypt_decorator(func):
26 def wrapper(word):
27 word = plugboard(rotor_decrypt(word, rotor_position), plugboard_position)
28 return func(word)
29 return wrapper
30 return decrypt_decorator
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 27, in wrapper
word = plugboard(rotor_decrypt(word, rotor_position), plugboard_position)
File "/tmp/solution.py", line 14, in rotor_decrypt
return ''.join(map(lambda n: reversed_swaps[n], word))
File "/tmp/solution.py", line 14, in <lambda>
return ''.join(map(lambda n: reversed_swaps[n], word))
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 27, in wrapper
word = plugboard(rotor_decrypt(word, rotor_position), plugboard_position)
File "/tmp/solution.py", line 14, in rotor_decrypt
return ''.join(map(lambda n: reversed_swaps[n], word))
File "/tmp/solution.py", line 14, in <lambda>
return ''.join(map(lambda n: reversed_swaps[n], word))
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 19, in wrapper
word = rotor(plugboard(word, plugboard_position), rotor_position)
File "/tmp/solution.py", line 10, in rotor
return ''.join(map(lambda n: swaps[n], word))
File "/tmp/solution.py", line 10, in <lambda>
return ''.join(map(lambda n: swaps[n], word))
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 10, in rotor
return ''.join(map(lambda n: swaps[n], word))
File "/tmp/solution.py", line 10, in <lambda>
return ''.join(map(lambda n: swaps[n], word))
KeyError: ' '
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (errors=4)
Георги Кунчев
26.10.2023 15:12Да, няма да има повторения на букви.
|
Димитър Христов
26.10.2023 14:49Нали чифтовете от букви за plugboard са напресичащи се сетове?
|
f | 1 | def plugboard(word, swaps): | f | 1 | def plugboard(word, swaps): |
n | n | 2 | swaps_dict = {} | ||
2 | for swap in swaps: | 3 | for swap in swaps: | ||
3 | first_letter, second_letter = tuple(swap) | 4 | first_letter, second_letter = tuple(swap) | ||
n | 4 | word = word.replace(first_letter, "*").replace(second_letter, first_letter).replace("*", second_letter) | n | 5 | swaps_dict.update({first_letter: second_letter, second_letter: first_letter}) |
5 | return word | 6 | return ''.join(map(lambda n: swaps_dict[n] if n in swaps_dict.keys() else n, word)) | ||
7 | |||||
6 | 8 | ||||
7 | def rotor(word, swaps): | 9 | def rotor(word, swaps): | ||
n | 8 | return ''.join(list(map(lambda n: swaps[n], word))) | n | 10 | return ''.join(map(lambda n: swaps[n], word)) |
9 | 11 | ||||
10 | def rotor_decrypt(word, swaps): | 12 | def rotor_decrypt(word, swaps): | ||
11 | reversed_swaps = {v: k for k, v in swaps.items()} | 13 | reversed_swaps = {v: k for k, v in swaps.items()} | ||
t | 12 | return ''.join(list(map(lambda n: reversed_swaps[n], word))) | t | 14 | return ''.join(map(lambda n: reversed_swaps[n], word)) |
13 | 15 | ||||
14 | def enigma_encrypt(plugboard_position, rotor_position): | 16 | def enigma_encrypt(plugboard_position, rotor_position): | ||
15 | def encrypt_decorator(func): | 17 | def encrypt_decorator(func): | ||
16 | def wrapper(word): | 18 | def wrapper(word): | ||
17 | word = rotor(plugboard(word, plugboard_position), rotor_position) | 19 | word = rotor(plugboard(word, plugboard_position), rotor_position) | ||
18 | return func(word) | 20 | return func(word) | ||
19 | return wrapper | 21 | return wrapper | ||
20 | return encrypt_decorator | 22 | return encrypt_decorator | ||
21 | 23 | ||||
22 | def enigma_decrypt(plugboard_position, rotor_position): | 24 | def enigma_decrypt(plugboard_position, rotor_position): | ||
23 | def decrypt_decorator(func): | 25 | def decrypt_decorator(func): | ||
24 | def wrapper(word): | 26 | def wrapper(word): | ||
25 | word = plugboard(rotor_decrypt(word, rotor_position), plugboard_position) | 27 | word = plugboard(rotor_decrypt(word, rotor_position), plugboard_position) | ||
26 | return func(word) | 28 | return func(word) | ||
27 | return wrapper | 29 | return wrapper | ||
28 | return decrypt_decorator | 30 | return decrypt_decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
26.10.2023 15:14