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

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

6 точки общо

5 успешни теста
4 неуспешни теста
Код
Скрий всички коментари

 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 са напресичащи се сетове?
История

f1def plugboard(word, swaps):f1def plugboard(word, swaps):
nn2    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)
n4        word = word.replace(first_letter, "*").replace(second_letter, first_letter).replace("*", second_letter)n5        swaps_dict.update({first_letter: second_letter, second_letter: first_letter})
5    return word6    return ''.join(map(lambda n: swaps_dict[n] if n in swaps_dict.keys() else n, word))
7 
68
7def rotor(word, swaps):9def rotor(word, swaps):
n8    return ''.join(list(map(lambda n: swaps[n], word)))n10    return ''.join(map(lambda n: swaps[n], word))
911
10def rotor_decrypt(word, swaps):12def 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()}
t12    return ''.join(list(map(lambda n: reversed_swaps[n], word)))t14    return ''.join(map(lambda n: reversed_swaps[n], word))
1315
14def enigma_encrypt(plugboard_position, rotor_position):16def 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 wrapper21        return wrapper
20    return encrypt_decorator22    return encrypt_decorator
2123
22def enigma_decrypt(plugboard_position, rotor_position):24def 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 wrapper29        return wrapper
28    return decrypt_decorator30    return decrypt_decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op