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)
f | 1 | def plugboard(word, position): | f | 1 | def plugboard(word, position): |
n | n | 2 | 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 | break | 5 | else: | ||
6 | word = word.replace(letter2, letter1) | ||||
9 | return word | 7 | return word | ||
10 | 8 | ||||
11 | def rotor(word, position): | 9 | def 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 word | 13 | return word | ||
16 | 14 | ||||
n | 17 | def enigma_encrypt(plugboard_position = {}, rotor_position = {}): | n | 15 | def 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): | ||
n | 20 | func(rotor(plugboard(word,plugboard_position),rotor_position)) | n | 18 | func(rotor(plugboard(word, plugboard_position), rotor_position)) |
21 | return encrypt_word | 19 | return encrypt_word | ||
22 | return encrypt_and_process | 20 | return encrypt_and_process | ||
23 | 21 | ||||
n | 24 | def enigma_decrypt(plugboard_position = {}, rotor_position = {}): | n | 22 | def 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): | ||
t | 31 | func(plugboard(rotor(word,switched_dict),plugboard_position)) | t | 25 | func(plugboard(rotor(word, {v: k for k, v in rotor_position.items()}) ,plugboard_position)) |
32 | return decrypt_word | 26 | return decrypt_word | ||
33 | return decrypt_and_process | 27 | return decrypt_and_process | ||
34 | 28 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|