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

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

7 точки общо

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

 1def plugboard(initial_input_string, list_of_sets):
 2    converted_string = ''
 3    for letter in initial_input_string:
 4        is_letter_found = False
 5        for first_letter_in_set, second_letter_in_set  in list_of_sets:
 6            if letter == first_letter_in_set:
 7                converted_string += second_letter_in_set
 8                is_letter_found = True
 9                break
10            elif letter == second_letter_in_set:
11                converted_string+=first_letter_in_set
12                is_letter_found = True
13                break
14        if not is_letter_found:
15            converted_string+=letter
16        is_letter_found = False
17    return converted_string
18
19def rotor(initial_input_string, rotor_position):
20    converted_string = ''
21    for letter in initial_input_string:
22        if letter == ' ':
23            converted_string+=letter
24        else:
25            converted_string+=rotor_position[letter]
26    return converted_string
27
28def enigma_encrypt(plugboard_position = [], rotor_position = {}):
29    def function_holder(func):
30        def executor(initial_input_string):
31            initial_input_string = rotor(plugboard(initial_input_string, plugboard_position), rotor_position)
32            func(initial_input_string)
33        return executor
34    return function_holder
35
36def enigma_decrypt(plugboard_position = [], rotor_position = {}):
37    def function_holder(func):
38        def executor(initial_input_string):
39            rotor_position_reversed = {}
40            for key in rotor_position:
41                rotor_position_reversed[rotor_position[key]] = key
42            initial_input_string = plugboard(rotor(initial_input_string, rotor_position_reversed), plugboard_position)
43            func(initial_input_string)
44        return executor
45    return function_holder        

F.FF.....
======================================================================
FAIL: 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')
AssertionError: None != 'i love python'

======================================================================
FAIL: 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'),
AssertionError: None != 'the quick brown fox jumps over the lazy dog'

======================================================================
FAIL: 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'),
AssertionError: None != 'mjg cavsk nrqfy zqd haulp qxgr mjg itob eqw'

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

FAILED (failures=3)

Дискусия
История
Това решение има само една версия.