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

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

4 точки общо

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

 1def plugboard(word, position):
 2    for letter1, letter2 in position:
 3        if letter1 in word:
 4            word = word.replace(letter1, letter2)
 5        else:
 6            word = word.replace(letter2, letter1)
 7    return word
 8
 9def rotor(word, position):
10    for letter in word:
11        if letter in position:
12            word = word.replace(letter, position[letter])
13    return word
14
15def enigma_encrypt(plugboard_position=[], rotor_position={}):
16    def encrypt_and_process(func):
17        def encrypt_word(word):
18            func(rotor(plugboard(word, plugboard_position), rotor_position))
19        return encrypt_word
20    return encrypt_and_process
21
22def enigma_decrypt(plugboard_position=[], rotor_position={}):
23    def decrypt_and_process(func):
24        def decrypt_word(word):
25            func(plugboard(rotor(word, {v: k for k, v in rotor_position.items()}) ,plugboard_position))
26        return decrypt_word
27    return decrypt_and_process

FFFF..F..
======================================================================
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_correct_decorator_order (test.TestDecorators)
Test whether the decorator is applying the functions in correct order.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 166, in test_correct_decorator_order
mock.assert_called_once_with('bumb')
File "/usr/lib/python3.10/unittest/mock.py", line 941, in assert_called_once_with
return self.assert_called_with(*args, **kwargs)
File "/usr/lib/python3.10/unittest/mock.py", line 929, in assert_called_with
raise AssertionError(_error_message()) from cause
AssertionError: expected call not found.
Expected: mock('bumb')
Actual: mock('mumm')

======================================================================
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'

======================================================================
FAIL: test_normal_case (test.TestPlugboard)
Test the plugboard function with normally expected input.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 45, in test_normal_case
self.assertEqual(plugboard('this is a test input', plugboard_position), 'ihts ts z iysi tnpui')
AssertionError: 'thts ts z tyst tnput' != 'ihts ts z iysi tnpui'
- thts ts z tyst tnput
? ^ ^ ^ ^
+ ihts ts z iysi tnpui
? ^ ^ ^ ^

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

FAILED (failures=5)

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

f1def plugboard(word, position):f1def plugboard(word, position):
nn2    for letter1, letter2 in position:
2    for letter in word:3        if letter1 in word:
3        for pair in position:
4            if letter in pair:
5                for new_letter in pair:
6                    if letter != new_letter:
7                        word = word.replace(letter, new_letter)4            word = word.replace(letter1, letter2)
8                        break5        else:
6            word = word.replace(letter2, letter1)
9    return word7    return word
108
11def rotor(word, position):9def rotor(word, position):
12    for letter in word:10    for letter in word:
13        if letter in position:11        if letter in position:
14            word = word.replace(letter, position[letter])12            word = word.replace(letter, position[letter])
15    return word13    return word
1614
n17def enigma_encrypt(plugboard_position = {}, rotor_position = {}):n15def enigma_encrypt(plugboard_position=[], rotor_position={}):
18    def encrypt_and_process(func):16    def encrypt_and_process(func):
19        def encrypt_word(word):17        def encrypt_word(word):
n20            func(rotor(plugboard(word,plugboard_position),rotor_position))n18            func(rotor(plugboard(word, plugboard_position), rotor_position))
21        return encrypt_word19        return encrypt_word
22    return encrypt_and_process20    return encrypt_and_process
2321
n24def enigma_decrypt(plugboard_position = {}, rotor_position = {}):n22def enigma_decrypt(plugboard_position=[], rotor_position={}):
25    switched_dict = {}
26    if rotor_position:
27        for key, value in rotor_position.items():
28            switched_dict[value] = key
29    def decrypt_and_process(func):23    def decrypt_and_process(func):
30        def decrypt_word(word):24        def decrypt_word(word):
t31            func(plugboard(rotor(word,switched_dict),plugboard_position))t25            func(plugboard(rotor(word, {v: k for k, v in rotor_position.items()}) ,plugboard_position))
32        return decrypt_word26        return decrypt_word
33    return decrypt_and_process27    return decrypt_and_process
3428
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op