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

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

6 точки общо

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

 1def plugboard(word, plugboard_position):
 2    new_word = ""
 3    for letter in word:
 4        for element in plugboard_position:
 5            if letter in element:
 6                new_word += element.difference(letter).pop()
 7                break
 8        else:
 9            new_word += letter
10    return new_word
11
12def rotor(word, rotor_position):
13    new_word = ""
14    for letter in word:
15        new_word += rotor_position[letter]
16    return new_word
17
18def enigma_encrypt(plugboard_position, rotor_position):
19    def wrapper(function):
20        def crypt(word):
21            return function(rotor(plugboard(word, plugboard_position), rotor_position))
22        return crypt
23    return wrapper
24
25def enigma_decrypt(plugboard_position, rotor_position):
26    def wrapper(function):
27        def crypt(word):
28            reversed_rotor_position = {value: key for key, value in rotor_position.items()}
29            return function(plugboard(rotor(word, reversed_rotor_position), plugboard_position))
30        return crypt
31    return wrapper

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 29, in crypt
return function(plugboard(rotor(word, reversed_rotor_position), plugboard_position))
File "/tmp/solution.py", line 15, in rotor
new_word += rotor_position[letter]
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 29, in crypt
return function(plugboard(rotor(word, reversed_rotor_position), plugboard_position))
File "/tmp/solution.py", line 15, in rotor
new_word += rotor_position[letter]
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 21, in crypt
return function(rotor(plugboard(word, plugboard_position), rotor_position))
File "/tmp/solution.py", line 15, in rotor
new_word += rotor_position[letter]
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 15, in rotor
new_word += rotor_position[letter]
KeyError: ' '

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

FAILED (errors=4)

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

f1def plugboard(word, plugboard_position):f1def plugboard(word, plugboard_position):
2    new_word = ""2    new_word = ""
3    for letter in word:3    for letter in word:
n4        check = Falsen
5        for element in plugboard_position:4        for element in plugboard_position:
6            if letter in element:5            if letter in element:
7                new_word += element.difference(letter).pop()6                new_word += element.difference(letter).pop()
n8                check = Truen
9                break7                break
n10        if check == False:n8        else:
11            new_word += letter9            new_word += letter
12    return new_word10    return new_word
1311
14def rotor(word, rotor_position):12def rotor(word, rotor_position):
15    new_word = ""13    new_word = ""
16    for letter in word:14    for letter in word:
17        new_word += rotor_position[letter]15        new_word += rotor_position[letter]
18    return new_word16    return new_word
1917
20def enigma_encrypt(plugboard_position, rotor_position):18def enigma_encrypt(plugboard_position, rotor_position):
21    def wrapper(function):19    def wrapper(function):
22        def crypt(word):20        def crypt(word):
23            return function(rotor(plugboard(word, plugboard_position), rotor_position))21            return function(rotor(plugboard(word, plugboard_position), rotor_position))
24        return crypt22        return crypt
25    return wrapper23    return wrapper
2624
27def enigma_decrypt(plugboard_position, rotor_position):25def enigma_decrypt(plugboard_position, rotor_position):
28    def wrapper(function):26    def wrapper(function):
29        def crypt(word):27        def crypt(word):
t30            reversed_rotor_position = dict([(value, key) for key, value in rotor_position.items()])t28            reversed_rotor_position = {value: key for key, value in rotor_position.items()}
31            return function(plugboard(rotor(word, reversed_rotor_position), plugboard_position))29            return function(plugboard(rotor(word, reversed_rotor_position), plugboard_position))
32        return crypt30        return crypt
33    return wrapper31    return wrapper
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op