Домашни > Енигма > Решения > Решението на Николай Георгиев

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

6 точки общо

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

 1def plugboard(text, conversion_list):
 2    conversion_dict = {}
 3    for conversion_set in conversion_list:
 4        letter_one, letter_two = conversion_set
 5        conversion_dict[letter_one] = letter_two
 6        conversion_dict[letter_two] = letter_one
 7    return "".join(conversion_dict.get(letter, letter) for letter in text)
 8
 9def rotor(text, converion_dict):
10    return "".join(converion_dict[letter] for letter in text)
11
12def get_enigma_decorator(first_operation, first_operation_arg, second_operation, second_operation_arg):
13    def enigma_decorator(decorated_function):
14        def text_converter(text):
15                return decorated_function(second_operation(first_operation(text, first_operation_arg), second_operation_arg))
16        return text_converter
17    return enigma_decorator
18
19def enigma_encrypt(plugboard_position, rotor_position):
20    return get_enigma_decorator(plugboard, plugboard_position, rotor, rotor_position)
21
22def enigma_decrypt(plugboard_position, rotor_position):
23    decrypt_rotor_position = {rotor_position[key]: key for key in rotor_position}
24    return get_enigma_decorator(rotor, decrypt_rotor_position, plugboard, plugboard_position)

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 15, in text_converter
return decorated_function(second_operation(first_operation(text, first_operation_arg), second_operation_arg))
File "/tmp/solution.py", line 10, in rotor
return "".join(converion_dict[letter] for letter in text)
File "/tmp/solution.py", line 10, in <genexpr>
return "".join(converion_dict[letter] for letter in text)
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 15, in text_converter
return decorated_function(second_operation(first_operation(text, first_operation_arg), second_operation_arg))
File "/tmp/solution.py", line 10, in rotor
return "".join(converion_dict[letter] for letter in text)
File "/tmp/solution.py", line 10, in <genexpr>
return "".join(converion_dict[letter] for letter in text)
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 15, in text_converter
return decorated_function(second_operation(first_operation(text, first_operation_arg), second_operation_arg))
File "/tmp/solution.py", line 10, in rotor
return "".join(converion_dict[letter] for letter in text)
File "/tmp/solution.py", line 10, in <genexpr>
return "".join(converion_dict[letter] for letter in text)
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(converion_dict[letter] for letter in text)
File "/tmp/solution.py", line 10, in <genexpr>
return "".join(converion_dict[letter] for letter in text)
KeyError: ' '

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

FAILED (errors=4)

Дискусия
История

f1def plugboard(text, conversion_list):f1def plugboard(text, conversion_list):
2    conversion_dict = {}2    conversion_dict = {}
n3    for set in conversion_list:n3    for conversion_set in conversion_list:
4        letter_one, letter_two = set4        letter_one, letter_two = conversion_set
5        conversion_dict[letter_one] = letter_two5        conversion_dict[letter_one] = letter_two
6        conversion_dict[letter_two] = letter_one6        conversion_dict[letter_two] = letter_one
t7    return "".join(conversion_dict[letter] if letter in conversion_dict else letter for letter in text)t7    return "".join(conversion_dict.get(letter, letter) for letter in text)
88
9def rotor(text, converion_dict):9def rotor(text, converion_dict):
10    return "".join(converion_dict[letter] for letter in text)10    return "".join(converion_dict[letter] for letter in text)
1111
12def get_enigma_decorator(first_operation, first_operation_arg, second_operation, second_operation_arg):12def get_enigma_decorator(first_operation, first_operation_arg, second_operation, second_operation_arg):
13    def enigma_decorator(decorated_function):13    def enigma_decorator(decorated_function):
14        def text_converter(text):14        def text_converter(text):
15                return decorated_function(second_operation(first_operation(text, first_operation_arg), second_operation_arg))15                return decorated_function(second_operation(first_operation(text, first_operation_arg), second_operation_arg))
16        return text_converter16        return text_converter
17    return enigma_decorator17    return enigma_decorator
1818
19def enigma_encrypt(plugboard_position, rotor_position):19def enigma_encrypt(plugboard_position, rotor_position):
20    return get_enigma_decorator(plugboard, plugboard_position, rotor, rotor_position)20    return get_enigma_decorator(plugboard, plugboard_position, rotor, rotor_position)
2121
22def enigma_decrypt(plugboard_position, rotor_position):22def enigma_decrypt(plugboard_position, rotor_position):
23    decrypt_rotor_position = {rotor_position[key]: key for key in rotor_position}23    decrypt_rotor_position = {rotor_position[key]: key for key in rotor_position}
24    return get_enigma_decorator(rotor, decrypt_rotor_position, plugboard, plugboard_position)24    return get_enigma_decorator(rotor, decrypt_rotor_position, plugboard, plugboard_position)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op