Домашни > Енигма > Решения > Решението на Добромир Пеев

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

7 точки общо

6 успешни теста
3 неуспешни теста
Код

 1def plugboard(text, plugboard_position):
 2    new_text = ""
 3    for letter in text:
 4        for pair in plugboard_position:
 5            if letter in pair:
 6                new_text += [other_letter for other_letter in pair if other_letter is not letter][0]
 7                break
 8        else:
 9            new_text += letter
10    return new_text
11
12def rotor(text, rotor_position):
13    new_text = ""
14    for letter in text:
15        new_text += rotor_position.get(letter," ")
16    return new_text
17
18def enigma_encrypt(plugboard_position, rotor_position):
19    def encrypt_decorator(func):
20        def encrypt_function(text):
21            text = plugboard(text,plugboard_position)
22            text = rotor(text,rotor_position)
23            func(text)
24        return encrypt_function
25    return encrypt_decorator
26
27def enigma_decrypt(plugboard_position, rotor_position):
28    def enigma_decrypt_decorator(func):
29        def decrypt_function(text):
30            new_rotor_position={}
31            for letter in rotor_position:
32                new_rotor_position[rotor_position[letter]] = letter
33            text = rotor(text,new_rotor_position)
34            text = plugboard(text,plugboard_position)
35            func(text)
36        return decrypt_function
37    return enigma_decrypt_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)

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

n1def plugboard(text,plugboard_position):n1def plugboard(text, plugboard_position):
2    new_text = ""2    new_text = ""
n3    in_pair = Falsen
4    for letter in text:3    for letter in text:
5        for pair in plugboard_position:4        for pair in plugboard_position:
6            if letter in pair:5            if letter in pair:
7                new_text += [other_letter for other_letter in pair if other_letter is not letter][0]6                new_text += [other_letter for other_letter in pair if other_letter is not letter][0]
n8                in_pair = Truen
9                break7                break
n10        new_text += letter if in_pair is False else ""n8        else:
11        in_pair = False9            new_text += letter
12    return new_text10    return new_text
1311
n14def rotor(text,rotor_position):n12def rotor(text, rotor_position):
15    new_text = ""13    new_text = ""
16    for letter in text:14    for letter in text:
n17         if letter in rotor_position:n
18            new_text += rotor_position[letter]15        new_text += rotor_position.get(letter," ")
19         else:
20            new_text += " "
21    return new_text16    return new_text
2217
n23def enigma_encrypt(plugboard_position,rotor_position):n18def enigma_encrypt(plugboard_position, rotor_position):
24    def encrypt_decorator(func):19    def encrypt_decorator(func):
25        def encrypt_function(text):20        def encrypt_function(text):
26            text = plugboard(text,plugboard_position)21            text = plugboard(text,plugboard_position)
27            text = rotor(text,rotor_position)22            text = rotor(text,rotor_position)
28            func(text)23            func(text)
29        return encrypt_function24        return encrypt_function
30    return encrypt_decorator25    return encrypt_decorator
3126
t32def enigma_decrypt(plugboard_position,rotor_position):t27def enigma_decrypt(plugboard_position, rotor_position):
33    def enigma_decrypt_decorator(func):28    def enigma_decrypt_decorator(func):
34        def decrypt_function(text):29        def decrypt_function(text):
35            new_rotor_position={}30            new_rotor_position={}
36            for letter in rotor_position:31            for letter in rotor_position:
37                new_rotor_position[rotor_position[letter]] = letter32                new_rotor_position[rotor_position[letter]] = letter
38            text = rotor(text,new_rotor_position)33            text = rotor(text,new_rotor_position)
39            text = plugboard(text,plugboard_position)34            text = plugboard(text,plugboard_position)
40            func(text)35            func(text)
41        return decrypt_function36        return decrypt_function
42    return enigma_decrypt_decorator37    return enigma_decrypt_decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op