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

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

6 точки общо

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

 1def plugboard(word, plugboard_position):
 2    new_word = ''
 3    for letter in word:
 4        for each_set in plugboard_position:
 5            if letter in each_set:
 6                other_letter = each_set - {letter}
 7                new_word += list(other_letter).pop()
 8                break
 9        else:
10            new_word += letter
11    return new_word
12
13def rotor(word, rotor_position):
14    new_word = ''
15    for letter in word:
16        new_word += rotor_position[letter]
17    return new_word
18
19def enigma_encrypt(plugboard_position, rotor_position):
20    def encrypted_word(func):
21        def transformed_word(word):
22            return func(rotor(plugboard(word, plugboard_position), rotor_position))
23        return transformed_word
24    return encrypted_word
25
26def enigma_decrypt(plugboard_position, rotor_position):
27    rotor_position = {value: key for key, value in rotor_position.items()}
28    def decrypted_word(func):
29        def transformed_word(word):
30            return func(plugboard(rotor(word, rotor_position), plugboard_position))
31        return transformed_word
32    return decrypted_word

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 30, in transformed_word
return func(plugboard(rotor(word, rotor_position), plugboard_position))
File "/tmp/solution.py", line 16, 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 30, in transformed_word
return func(plugboard(rotor(word, rotor_position), plugboard_position))
File "/tmp/solution.py", line 16, 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 22, in transformed_word
return func(rotor(plugboard(word, plugboard_position), rotor_position))
File "/tmp/solution.py", line 16, 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 16, in rotor
new_word += rotor_position[letter]
KeyError: ' '

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

FAILED (errors=4)

Дискусия
Константин Кирязов
28.10.2023 00:07

Предишното качено решение беше грешен файл. <br> oops
История

f1def plugboard(word, plugboard_position):f1def plugboard(word, plugboard_position):
2    new_word = ''2    new_word = ''
n3    word = [*word]n
4    for letter in word:3    for letter in word:
n5        letter_is_replaced = Falsen
6        for set in plugboard_position:4        for each_set in plugboard_position:
7            if letter in set:5            if letter in each_set:
8                other_letter = set - {letter}6                other_letter = each_set - {letter}
9                new_word += list(other_letter).pop()7                new_word += list(other_letter).pop()
n10                letter_is_replaced = Truen8                break
11        if not letter_is_replaced:9        else:
12            new_word += letter10            new_word += letter
13    return new_word11    return new_word
1412
15def rotor(word, rotor_position):13def rotor(word, rotor_position):
16    new_word = ''14    new_word = ''
t17    word = [*word]t
18    for letter in word:15    for letter in word:
19        new_word += rotor_position[letter]16        new_word += rotor_position[letter]
20    return new_word17    return new_word
2118
22def enigma_encrypt(plugboard_position, rotor_position):19def enigma_encrypt(plugboard_position, rotor_position):
23    def encrypted_word(func):20    def encrypted_word(func):
24        def transformed_word(word):21        def transformed_word(word):
25            return func(rotor(plugboard(word, plugboard_position), rotor_position))22            return func(rotor(plugboard(word, plugboard_position), rotor_position))
26        return transformed_word23        return transformed_word
27    return encrypted_word24    return encrypted_word
2825
29def enigma_decrypt(plugboard_position, rotor_position):26def enigma_decrypt(plugboard_position, rotor_position):
30    rotor_position = {value: key for key, value in rotor_position.items()}27    rotor_position = {value: key for key, value in rotor_position.items()}
31    def decrypted_word(func):28    def decrypted_word(func):
32        def transformed_word(word):29        def transformed_word(word):
33            return func(plugboard(rotor(word, rotor_position), plugboard_position))30            return func(plugboard(rotor(word, rotor_position), plugboard_position))
34        return transformed_word31        return transformed_word
35    return decrypted_word32    return decrypted_word
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1#HELPn
2def plugboard(word, plugboard_position):1def plugboard(word, plugboard_position):
3    new_word = ''2    new_word = ''
4    word = [*word]3    word = [*word]
5    for letter in word:4    for letter in word:
nn5        letter_is_replaced = False
6        for set in plugboard_position:6        for set in plugboard_position:
7            if letter in set:7            if letter in set:
8                other_letter = set - {letter}8                other_letter = set - {letter}
9                new_word += list(other_letter).pop()9                new_word += list(other_letter).pop()
n10        new_word += letter #??????????????????????n10                letter_is_replaced = True
11        if not letter_is_replaced:
12            new_word += letter
11    return new_word13    return new_word
1214
n13#testsn
14#plugboard_position = [{'a', 'c'}, {'t', 'z'}]
15#plugboard('enigma', plugboard_position)
16 
17#DONE
18def rotor(word, rotor_position):15def rotor(word, rotor_position):
19    new_word = ''16    new_word = ''
20    word = [*word]17    word = [*word]
21    for letter in word:18    for letter in word:
22        new_word += rotor_position[letter]19        new_word += rotor_position[letter]
23    return new_word20    return new_word
2421
n25#testsn
26rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',
27                  's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',
28                  'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',
29                  'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',
30                  'm': 'r', 'c': 'k'}
31rotor('enigma', rotor_position)
32 
33def enigma_encrypt(plugboard_position, rotor_position):22def enigma_encrypt(plugboard_position, rotor_position):
n34    returnn23    def encrypted_word(func):
24        def transformed_word(word):
25            return func(rotor(plugboard(word, plugboard_position), rotor_position))
26        return transformed_word
27    return encrypted_word
3528
36def enigma_decrypt(plugboard_position, rotor_position):29def enigma_decrypt(plugboard_position, rotor_position):
t37    returnt30    rotor_position = {value: key for key, value in rotor_position.items()}
38 31    def decrypted_word(func):
39 32        def transformed_word(word):
40 33            return func(plugboard(rotor(word, rotor_position), plugboard_position))
34        return transformed_word
35    return decrypted_word
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op