Домашни > Енигма > Решения > Решението на Михаела Хаджиковакева

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

6 точки общо

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

 1def plugboard(word, sets):
 2    wordRes = ""
 3    for letter in word:
 4        for x, y in sets:
 5            if letter == x:
 6                letter = y
 7                break
 8            if letter == y:
 9                letter = x
10                break
11        wordRes+=letter
12    return wordRes
13
14def rotor(word, alphabet):
15    wordRes = ""
16    for letter in word:
17        if(letter == " "):
18            wordRes += letter
19            continue
20        wordRes+=alphabet[letter]
21    return wordRes
22
23def enigma_encrypt(plugboard_position, rotor_position):
24    def decorator(func):
25        def func_params(initial_str):
26           initial_str = rotor(plugboard(initial_str, plugboard_position), rotor_position)
27           func(initial_str)
28        return func_params
29    return decorator
30
31def enigma_decrypt(plugboard_position, rotor_position):
32    def decorator(func):
33        def func_params(initial_str):
34           reversed_rotor_position = {}
35           for x, y in rotor_position.items():
36               reversed_rotor_position[y] = x
37           initial_str = plugboard(rotor(initial_str, reversed_rotor_position), plugboard_position)
38           func(initial_str)
39        return func_params
40    return decorator

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)

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

f1def plugboard(word, sets):f1def plugboard(word, sets):
2    wordRes = ""2    wordRes = ""
3    for letter in word:3    for letter in word:
4        for x, y in sets:4        for x, y in sets:
5            if letter == x:5            if letter == x:
6                letter = y6                letter = y
7                break7                break
8            if letter == y:8            if letter == y:
9                letter = x9                letter = x
10                break10                break
11        wordRes+=letter11        wordRes+=letter
12    return wordRes12    return wordRes
1313
14def rotor(word, alphabet):14def rotor(word, alphabet):
15    wordRes = ""15    wordRes = ""
16    for letter in word:16    for letter in word:
17        if(letter == " "):17        if(letter == " "):
tt18            wordRes += letter
18            continue19            continue
19        wordRes+=alphabet[letter]20        wordRes+=alphabet[letter]
20    return wordRes21    return wordRes
2122
22def enigma_encrypt(plugboard_position, rotor_position):23def enigma_encrypt(plugboard_position, rotor_position):
23    def decorator(func):24    def decorator(func):
24        def func_params(initial_str):25        def func_params(initial_str):
25           initial_str = rotor(plugboard(initial_str, plugboard_position), rotor_position)26           initial_str = rotor(plugboard(initial_str, plugboard_position), rotor_position)
26           func(initial_str)27           func(initial_str)
27        return func_params28        return func_params
28    return decorator29    return decorator
2930
30def enigma_decrypt(plugboard_position, rotor_position):31def enigma_decrypt(plugboard_position, rotor_position):
31    def decorator(func):32    def decorator(func):
32        def func_params(initial_str):33        def func_params(initial_str):
33           reversed_rotor_position = {}34           reversed_rotor_position = {}
34           for x, y in rotor_position.items():35           for x, y in rotor_position.items():
35               reversed_rotor_position[y] = x36               reversed_rotor_position[y] = x
36           initial_str = plugboard(rotor(initial_str, reversed_rotor_position), plugboard_position)37           initial_str = plugboard(rotor(initial_str, reversed_rotor_position), plugboard_position)
37           func(initial_str)38           func(initial_str)
38        return func_params39        return func_params
39    return decorator40    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op