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

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

6 точки общо

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

 1def plugboard(to_convert, encoding):
 2    #Convert to list because str does not support item assignment
 3    interpret = list(to_convert)
 4    
 5    for ind in range(len(to_convert)):
 6        for first, second in encoding:
 7            if to_convert[ind] == first:
 8                interpret[ind] = second
 9            elif to_convert[ind] == second:
10                interpret[ind] = first
11                
12    return ''.join(interpret)
13
14
15def rotor(to_convert, encoding):
16    #Convert to list because str does not support item assignment
17    interpret = list(to_convert)
18    
19    for ind in range(len(to_convert)):
20        interpret[ind] = encoding[interpret[ind]]
21        
22    return ''.join(interpret)
23
24
25def enigma_encrypt(plugboard_position=[], rotor_position={}):
26    def enigma(string_function):
27        def encrypt(to_encrypt):
28            return string_function(rotor(plugboard(to_encrypt, plugboard_position), rotor_position))
29        return encrypt
30    
31    return enigma
32
33
34def enigma_decrypt(plugboard_position=[], rotor_position={}):
35    reverse_rotor = {val: key for key, val in rotor_position.items()}
36    
37    def enigma(string_function):
38        def encrypt(to_encrypt):
39            return string_function(plugboard(rotor(to_encrypt, reverse_rotor), plugboard_position))
40        return encrypt
41    
42    return enigma

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 39, in encrypt
return string_function(plugboard(rotor(to_encrypt, reverse_rotor), plugboard_position))
File "/tmp/solution.py", line 20, in rotor
interpret[ind] = encoding[interpret[ind]]
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 39, in encrypt
return string_function(plugboard(rotor(to_encrypt, reverse_rotor), plugboard_position))
File "/tmp/solution.py", line 20, in rotor
interpret[ind] = encoding[interpret[ind]]
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 28, in encrypt
return string_function(rotor(plugboard(to_encrypt, plugboard_position), rotor_position))
File "/tmp/solution.py", line 20, in rotor
interpret[ind] = encoding[interpret[ind]]
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 20, in rotor
interpret[ind] = encoding[interpret[ind]]
KeyError: ' '

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

FAILED (errors=4)

Дискусия
Георги Кунчев
25.10.2023 10:09

Похвално за тестовете. Идеята с тях, обаче, е да ги споделяш с останалите студенти, за което да получиш евентуална звезда. Разбира се, дали искаш да го направиш, зависи от теб. Ако просто ги споделяш с нас, няма да ги коментираме, дори да са грешни. Ако искаш да ги споделиш със останалите студенти, правиш го в секцията "Дискусия" под самото домашно, и ние преценяваме дали тестът е достатъчно добър, за звезда. В конкретния случай тестваш само с по един пример, без конкретна функционалност. Реално просто автоматизираш това, което всеки прави в началото като Sanity test на кода си. Не ме разбирай погрешно. Като за първи тест е бомба, но за да получиш звезда ще е нужно да напишеш конкретни сценарии, които да тестваш (функционалност, corner case-ове...).
Владимир Великов
25.10.2023 01:19

Написах и тестове по зададения в условието пример: import unittest from <filename> import * class TestingEnigma(unittest.TestCase): def setUp(self): self.plugboard_position = [{'a', 'c'}, {'t', 'z'}] self.rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', 'm': 'r', 'c': 'k'} def tearDown(self): del self.plugboard_position del self.rotor_position def test_rotor(self): self.assertEqual(rotor('enigma', self.rotor_position), 'tnwora') def test_plugboard(self): self.assertEqual(plugboard('enigma', self.plugboard_position), 'enigmc') def test_encryption(self): encryptor = enigma_encrypt(self.plugboard_position, self.rotor_position) encrypted_repr = encryptor(repr) self.assertEqual(encrypted_repr('enigma'), "'tnwork'") def test_decryption(self): decryptor = enigma_decrypt(self.plugboard_position, self.rotor_position) decrypted_repr = decryptor(repr) self.assertEqual(decrypted_repr('tnwork'), "'enigma'") if __name__ == '__main__': unittest.main()
История

f1def plugboard(to_convert, encoding):f1def plugboard(to_convert, encoding):
nn2    #Convert to list because str does not support item assignment
2    interpret = list(to_convert)3    interpret = list(to_convert)
nn4    
3    for ind in range(0, len(interpret)):5    for ind in range(len(to_convert)):
4        for first, second in encoding:6        for first, second in encoding:
n5            if interpret[ind] == first:n7            if to_convert[ind] == first:
6                interpret[ind] = second8                interpret[ind] = second
n7            elif interpret[ind] == second:n9            elif to_convert[ind] == second:
8                interpret[ind] = first10                interpret[ind] = first
9                11                
10    return ''.join(interpret)12    return ''.join(interpret)
1113
1214
13def rotor(to_convert, encoding):15def rotor(to_convert, encoding):
nn16    #Convert to list because str does not support item assignment
14    interpret = list(to_convert)17    interpret = list(to_convert)
nn18    
15    for ind in range(0, len(interpret)):19    for ind in range(len(to_convert)):
16        interpret[ind] = encoding[interpret[ind]]20        interpret[ind] = encoding[interpret[ind]]
17        21        
18    return ''.join(interpret)22    return ''.join(interpret)
1923
2024
n21def enigma_encrypt(plugboard_position = [], rotor_position = {}):n25def enigma_encrypt(plugboard_position=[], rotor_position={}):
22    def enigma(string_function):26    def enigma(string_function):
23        def encrypt(to_encrypt):27        def encrypt(to_encrypt):
24            return string_function(rotor(plugboard(to_encrypt, plugboard_position), rotor_position))28            return string_function(rotor(plugboard(to_encrypt, plugboard_position), rotor_position))
25        return encrypt29        return encrypt
26    30    
27    return enigma31    return enigma
2832
2933
t30def enigma_decrypt(plugboard_position = [], rotor_position = {}):t34def enigma_decrypt(plugboard_position=[], rotor_position={}):
31    reverse_rotor = {val: key for key, val in rotor_position.items()}35    reverse_rotor = {val: key for key, val in rotor_position.items()}
32    36    
33    def enigma(string_function):37    def enigma(string_function):
34        def encrypt(to_encrypt):38        def encrypt(to_encrypt):
35            return string_function(plugboard(rotor(to_encrypt, reverse_rotor), plugboard_position))39            return string_function(plugboard(rotor(to_encrypt, reverse_rotor), plugboard_position))
36        return encrypt40        return encrypt
37    41    
38    return enigma42    return enigma
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op