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

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

5 точки общо

5 успешни теста
4 неуспешни теста
Код
Скрий всички коментари

 1# This is a sample Python script.
 2
 3# Press Shift+F10 to execute it or replace it with your code.
 4# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
 5
 6def plugboard(text, connections):
 7    result = ''
 8    for current in text:
 9        for element in connections:
10            first, second = element
11            if first == current:
12                result += second
13                break
14            elif second == current:
15                result += first
16                break
17        else:
18            result += current
19    return result
20
21
22def rotor(str, dict):
23    result = ''
24    for ch in str:
25        for key, value in dict.items():
26             if key == ch:
27                 result += value
28    return result
29
30
31
32def enigma_encrypt(plugboard_position, rotor_position):
33    def decorator(func):
34        def wrapper(text):
35            text = plugboard(text, plugboard_position)
36            text = rotor(text, rotor_position)
37            return func(text)
38
39        return wrapper
40
41    return decorator
42
43
44def enigma_decrypt(plugboard_position, rotor_position):
45    def decorator(func):
46        def wrapper(text):
47            text = rotor(text, {v: k for k, v in rotor_position.items()})
48            text = plugboard(text, plugboard_position)
49            return func(text)
50
51        return wrapper
52
53    return decorator

F.FF....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: 'ilovepython' != 'i love python'
- ilovepython
+ 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: 'thequickbrownfoxjumpsoverthelazydog' != 'the quick brown fox jumps over the lazy dog'
- thequickbrownfoxjumpsoverthelazydog
+ 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: 'mjgcavsknrqfyzqdhaulpqxgrmjgitobeqw' != 'mjg cavsk nrqfy zqd haulp qxgr mjg itob eqw'
- mjgcavsknrqfyzqdhaulpqxgrmjgitobeqw
+ mjg cavsk nrqfy zqd haulp qxgr mjg itob eqw
? + + + + + + + +

======================================================================
FAIL: 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')
AssertionError: 'kbjojockdokjylqk' != 'kbjo jo c kdok jylqk'
- kbjojockdokjylqk
+ kbjo jo c kdok jylqk
? + + + +

----------------------------------------------------------------------
Ran 9 tests in 0.002s

FAILED (failures=4)

Дискусия
История
Това решение има само една версия.