1def plugboard(text, plugboard_position):
2 result = ""
3 for letter in text:
4 into_another_letter = letter
5 for plug_set in plugboard_position:
6 if letter in plug_set:
7 into_another_letter = (plug_set - {letter}).pop()
8 break
9 result += into_another_letter
10 return result
11
12def rotor_two_ways(text, rotor_position, encrypt_decrypt):
13 result = ""
14 for letter in text:
15 for unchanged, changed in rotor_position.items():
16 if encrypt_decrypt == 'encrypt' and letter == unchanged:
17 result += changed
18 break
19 elif encrypt_decrypt == 'decrypt' and letter == changed:
20 result += unchanged
21 break
22 else:
23 result += letter
24 return result
25
26def rotor(text, rotor_position):
27 return rotor_two_ways(text, rotor_position, 'encrypt')
28
29def enigma_encrypt(plugboard_position, rotor_position):
30 def enigma_decorator(func):
31 def encrypt(text):
32 return rotor(plugboard(text, plugboard_position), rotor_position)
33 return encrypt
34 return enigma_decorator
35
36def enigma_decrypt(plugboard_position, rotor_position):
37 def enigma_decorator(func):
38 def decrypt(text):
39 result = rotor_two_ways(text, rotor_position, 'decrypt')
40 return plugboard(result, plugboard_position)
41 return decrypt
42 return enigma_decorator
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)
f | 1 | def plugboard(text, plugboard_position): | f | 1 | def plugboard(text, plugboard_position): |
2 | result = "" | 2 | result = "" | ||
3 | for letter in text: | 3 | for letter in text: | ||
4 | into_another_letter = letter | 4 | into_another_letter = letter | ||
5 | for plug_set in plugboard_position: | 5 | for plug_set in plugboard_position: | ||
6 | if letter in plug_set: | 6 | if letter in plug_set: | ||
7 | into_another_letter = (plug_set - {letter}).pop() | 7 | into_another_letter = (plug_set - {letter}).pop() | ||
8 | break | 8 | break | ||
9 | result += into_another_letter | 9 | result += into_another_letter | ||
10 | return result | 10 | return result | ||
11 | 11 | ||||
12 | def rotor_two_ways(text, rotor_position, encrypt_decrypt): | 12 | def rotor_two_ways(text, rotor_position, encrypt_decrypt): | ||
13 | result = "" | 13 | result = "" | ||
14 | for letter in text: | 14 | for letter in text: | ||
n | 15 | result += letter | n | ||
16 | for unchanged, changed in rotor_position.items(): | 15 | for unchanged, changed in rotor_position.items(): | ||
17 | if encrypt_decrypt == 'encrypt' and letter == unchanged: | 16 | if encrypt_decrypt == 'encrypt' and letter == unchanged: | ||
n | 18 | result = result[:-1] | n | ||
19 | result += rotor_position[unchanged] | 17 | result += changed | ||
18 | break | ||||
20 | elif encrypt_decrypt == 'decrypt' and letter == rotor_position[unchanged]: | 19 | elif encrypt_decrypt == 'decrypt' and letter == changed: | ||
21 | result = result[:-1] | ||||
22 | result += unchanged | 20 | result += unchanged | ||
n | n | 21 | break | ||
22 | else: | ||||
23 | result += letter | ||||
23 | return result | 24 | return result | ||
24 | 25 | ||||
25 | def rotor(text, rotor_position): | 26 | def rotor(text, rotor_position): | ||
26 | return rotor_two_ways(text, rotor_position, 'encrypt') | 27 | return rotor_two_ways(text, rotor_position, 'encrypt') | ||
27 | 28 | ||||
28 | def enigma_encrypt(plugboard_position, rotor_position): | 29 | def enigma_encrypt(plugboard_position, rotor_position): | ||
29 | def enigma_decorator(func): | 30 | def enigma_decorator(func): | ||
30 | def encrypt(text): | 31 | def encrypt(text): | ||
31 | return rotor(plugboard(text, plugboard_position), rotor_position) | 32 | return rotor(plugboard(text, plugboard_position), rotor_position) | ||
32 | return encrypt | 33 | return encrypt | ||
33 | return enigma_decorator | 34 | return enigma_decorator | ||
34 | 35 | ||||
35 | def enigma_decrypt(plugboard_position, rotor_position): | 36 | def enigma_decrypt(plugboard_position, rotor_position): | ||
36 | def enigma_decorator(func): | 37 | def enigma_decorator(func): | ||
37 | def decrypt(text): | 38 | def decrypt(text): | ||
38 | result = rotor_two_ways(text, rotor_position, 'decrypt') | 39 | result = rotor_two_ways(text, rotor_position, 'decrypt') | ||
39 | return plugboard(result, plugboard_position) | 40 | return plugboard(result, plugboard_position) | ||
40 | return decrypt | 41 | return decrypt | ||
41 | return enigma_decorator | 42 | return enigma_decorator | ||
t | t | 43 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
n | 1 | def plugboard(str, plugboard_position): | n | 1 | def plugboard(text, plugboard_position): |
2 | result = "" | 2 | result = "" | ||
n | 3 | for letter in str: | n | 3 | for letter in text: |
4 | result = result + letter | 4 | into_another_letter = letter | ||
5 | for into_another_letter in plugboard_position: | 5 | for plug_set in plugboard_position: | ||
6 | if letter in into_another_letter: | 6 | if letter in plug_set: | ||
7 | for changed_letter in into_another_letter: | 7 | into_another_letter = (plug_set - {letter}).pop() | ||
8 | if letter != changed_letter: | ||||
9 | result = result[:-1] | ||||
10 | result = result + changed_letter | ||||
11 | break | ||||
12 | break | 8 | break | ||
n | n | 9 | result += into_another_letter | ||
13 | return result | 10 | return result | ||
14 | 11 | ||||
n | 15 | def rotor(str, rotor_position): | n | 12 | def rotor_two_ways(text, rotor_position, encrypt_decrypt): |
16 | result = "" | 13 | result = "" | ||
n | 17 | for letter in str: | n | 14 | for letter in text: |
18 | result += letter | 15 | result += letter | ||
n | 19 | for unchanged in rotor_position: | n | 16 | for unchanged, changed in rotor_position.items(): |
20 | if letter == unchanged: | 17 | if encrypt_decrypt == 'encrypt' and letter == unchanged: | ||
21 | result = result[:-1] | 18 | result = result[:-1] | ||
22 | result += rotor_position[unchanged] | 19 | result += rotor_position[unchanged] | ||
n | n | 20 | elif encrypt_decrypt == 'decrypt' and letter == rotor_position[unchanged]: | ||
21 | result = result[:-1] | ||||
22 | result += unchanged | ||||
23 | return result | 23 | return result | ||
n | n | 24 | |||
25 | def rotor(text, rotor_position): | ||||
26 | return rotor_two_ways(text, rotor_position, 'encrypt') | ||||
24 | 27 | ||||
25 | def enigma_encrypt(plugboard_position, rotor_position): | 28 | def enigma_encrypt(plugboard_position, rotor_position): | ||
26 | def enigma_decorator(func): | 29 | def enigma_decorator(func): | ||
27 | def encrypt(text): | 30 | def encrypt(text): | ||
28 | return rotor(plugboard(text, plugboard_position), rotor_position) | 31 | return rotor(plugboard(text, plugboard_position), rotor_position) | ||
29 | return encrypt | 32 | return encrypt | ||
30 | return enigma_decorator | 33 | return enigma_decorator | ||
31 | 34 | ||||
32 | def enigma_decrypt(plugboard_position, rotor_position): | 35 | def enigma_decrypt(plugboard_position, rotor_position): | ||
33 | def enigma_decorator(func): | 36 | def enigma_decorator(func): | ||
n | 34 | def encrypt(text): | n | 37 | def decrypt(text): |
35 | result = "" | 38 | result = rotor_two_ways(text, rotor_position, 'decrypt') | ||
36 | for letter in text: | ||||
37 | result += letter | ||||
38 | for unchanged in rotor_position: | ||||
39 | if letter == rotor_position[unchanged]: | ||||
40 | result = result[:-1] | ||||
41 | result += unchanged | ||||
42 | return plugboard(result, plugboard_position) | 39 | return plugboard(result, plugboard_position) | ||
n | 43 | return encrypt | n | 40 | return decrypt |
44 | return enigma_decorator | 41 | return enigma_decorator | ||
t | 45 | t | |||
46 | |||||
47 | |||||
48 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|