Домашни > Енигма > Решения > Решението на Данаил Тодоров

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

6 точки общо

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

 1def plugboard(text_to_convert, list_of_sets):
 2    converted_string = ''
 3    for character in text_to_convert:
 4        for x, y in list_of_sets:
 5            if character == x:
 6                character = y
 7                break
 8            elif character == y:
 9                character = x
10                break
11        converted_string += character
12    return converted_string
13
14def rotor(text_to_convert, template_dict):
15    converted_string = ''
16    for character in text_to_convert:
17        character = template_dict.get(character)
18        converted_string += character
19    return converted_string
20
21def enigma_encrypt(plugboard_position, rotor_position):
22    def decorator(function):
23        def encrypt_text(current_string):
24            function(rotor(plugboard(current_string, plugboard_position), rotor_position))
25        return encrypt_text
26    return decorator
27
28def enigma_decrypt(plugboard_position, rotor_position):
29    def decorator(function):
30        def decrypt_text(string_to_decrypt):
31            reversed_dictionary = {value:key for key, value in rotor_position.items()}
32            function(plugboard(rotor(string_to_decrypt, reversed_dictionary), plugboard_position))
33        return decrypt_text
34    return 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 32, in decrypt_text
function(plugboard(rotor(string_to_decrypt, reversed_dictionary), plugboard_position))
File "/tmp/solution.py", line 18, in rotor
converted_string += character
TypeError: can only concatenate str (not "NoneType") to str

======================================================================
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 32, in decrypt_text
function(plugboard(rotor(string_to_decrypt, reversed_dictionary), plugboard_position))
File "/tmp/solution.py", line 18, in rotor
converted_string += character
TypeError: can only concatenate str (not "NoneType") to str

======================================================================
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 24, in encrypt_text
function(rotor(plugboard(current_string, plugboard_position), rotor_position))
File "/tmp/solution.py", line 18, in rotor
converted_string += character
TypeError: can only concatenate str (not "NoneType") to str

======================================================================
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 18, in rotor
converted_string += character
TypeError: can only concatenate str (not "NoneType") to str

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

FAILED (errors=4)

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

f1def plugboard(text_to_convert, list_of_sets):f1def plugboard(text_to_convert, list_of_sets):
2    converted_string = ''2    converted_string = ''
3    for character in text_to_convert:3    for character in text_to_convert:
n4        for (x, y) in list_of_sets:n4        for x, y in list_of_sets:
5            if character == x:5            if character == x:
6                character = y6                character = y
7                break7                break
8            elif character == y:8            elif character == y:
9                character = x9                character = x
10                break10                break
11        converted_string += character11        converted_string += character
12    return converted_string12    return converted_string
1313
14def rotor(text_to_convert, template_dict):14def rotor(text_to_convert, template_dict):
15    converted_string = ''15    converted_string = ''
16    for character in text_to_convert:16    for character in text_to_convert:
n17        for key, value in template_dict.items():n17        character = template_dict.get(character)
18            if character == key:
19                character = value
20                break
21        converted_string += character18        converted_string += character
22    return converted_string19    return converted_string
2320
24def enigma_encrypt(plugboard_position, rotor_position):21def enigma_encrypt(plugboard_position, rotor_position):
25    def decorator(function):22    def decorator(function):
26        def encrypt_text(current_string):23        def encrypt_text(current_string):
27            function(rotor(plugboard(current_string, plugboard_position), rotor_position))24            function(rotor(plugboard(current_string, plugboard_position), rotor_position))
28        return encrypt_text25        return encrypt_text
29    return decorator26    return decorator
3027
31def enigma_decrypt(plugboard_position, rotor_position):28def enigma_decrypt(plugboard_position, rotor_position):
32    def decorator(function):29    def decorator(function):
33        def decrypt_text(string_to_decrypt):30        def decrypt_text(string_to_decrypt):
t34            reversed_dictionary = {value:key for key,value in rotor_position.items()}t31            reversed_dictionary = {value:key for key, value in rotor_position.items()}
35            function(plugboard(rotor(string_to_decrypt, reversed_dictionary), plugboard_position))32            function(plugboard(rotor(string_to_decrypt, reversed_dictionary), plugboard_position))
36        return decrypt_text33        return decrypt_text
37    return decorator34    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op