1def encrypt_decrypt_rotor(change_text, text, rotor_position):
2 result = ""
3 for letter in text:
4 result += letter
5 for unchanged, changed in rotor_position.items():
6 if letter == rotor_position[unchanged] and change_text == 'to decrypt':
7 result = result[:-1]
8 result += unchanged
9 elif letter == unchanged and change_text == 'to encrypt':
10 result = result[:-1]
11 result += rotor_position[unchanged]
12 return result
13
14def plugboard(text, plugboard_position):
15 result = ""
16 for letter in text:
17 next_letter = letter
18 for p_set in plugboard_position:
19 if letter in p_set:
20 next_letter = (p_set - {letter}).pop()
21 break
22 result += next_letter
23 return result
24
25def rotor(text, rotor_position):
26 return encrypt_decrypt_rotor('to encrypt', text, rotor_position, )
27
28def enigma_encrypt(plugboard_position, rotor_position):
29 def decorator(*args):
30 def text_encrypt(text):
31 return rotor(plugboard(text, plugboard_position), rotor_position)
32 return text_encrypt
33 return decorator
34
35def enigma_decrypt(plugboard_position, rotor_position):
36 def enigma_decorator(*args):
37 def decrypt(text):
38 result = encrypt_decrypt_rotor('to decrypt', text, rotor_position)
39 return plugboard(result, plugboard_position)
40 return decrypt
41 return enigma_decorator
42
43
44plugboard_position = [{'a', 'c'}, {'t', 'z'}]
45rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',
46 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',
47 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',
48 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',
49 'm': 'r', 'c': 'k'}
FF.......
======================================================================
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: 'a gnjd korynm' != 'i love python'
- a gnjd korynm
+ 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 940, in assert_called_once_with
raise AssertionError(msg)
AssertionError: Expected 'mock' to be called once. Called 0 times.
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (failures=2)
31.10.2023 17:22
31.10.2023 17:26