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

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

7 точки общо

6 успешни теста
3 неуспешни теста
Код

 1def plugboard_helper(char, set_list):
 2    """Converts a given character into another based on a given list of sets."""
 3    for double in set_list:
 4        if char in double:
 5            converted_char = double - set(char)
 6            return ''.join(converted_char)
 7    return char
 8
 9
10def plugboard(string, set_list):
11    converted = (''.join([plugboard_helper(char, set_list)
12                          for char in string]))
13    return converted
14
15
16def rotor(string, dictionary):
17    converted = ''.join([dictionary.get(char, char) for char in string])
18    return converted
19
20
21def enigma_encrypt(plugboard_position, rotor_position):
22    def encryptor(func):
23        def inner(string):
24            encrypted = (rotor(plugboard(string, plugboard_position),
25                               rotor_position))
26            func(encrypted)
27        return inner
28    return encryptor
29
30
31def enigma_decrypt(plugboard_position, rotor_position):
32    def decryptor(func):
33        def inner(string):
34            rotor_position_for_decrypting = (dict(
35                list((value, key) for key, value in rotor_position.items())))
36            decrypted = (plugboard(
37                rotor(string, rotor_position_for_decrypting), plugboard_position))
38            func(decrypted)
39        return inner
40    return decryptor

F.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: None != '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: 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'

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

FAILED (failures=3)

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

f1def plugboard_helper(char, set_list):f1def plugboard_helper(char, set_list):
n2    # converts a given character into another based on a given list of setsn2    """Converts a given character into another based on a given list of sets."""
3    for double in set_list:3    for double in set_list:
4        if char in double:4        if char in double:
5            converted_char = double - set(char)5            converted_char = double - set(char)
6            return ''.join(converted_char)6            return ''.join(converted_char)
7    return char7    return char
88
99
10def plugboard(string, set_list):10def plugboard(string, set_list):
n11    converted = ''.join(list(plugboard_helper(char, set_list) for char in string))n11    converted = (''.join([plugboard_helper(char, set_list)
12                          for char in string]))
12    return converted13    return converted
1314
1415
15def rotor(string, dictionary):16def rotor(string, dictionary):
n16    converted = ''.join(list(dictionary.get(char, char) for char in string))n17    converted = ''.join([dictionary.get(char, char) for char in string])
17    return converted18    return converted
1819
1920
20def enigma_encrypt(plugboard_position, rotor_position):21def enigma_encrypt(plugboard_position, rotor_position):
21    def encryptor(func):22    def encryptor(func):
22        def inner(string):23        def inner(string):
n23            encrypted = rotor(plugboard(string, plugboard_position), \n24            encrypted = (rotor(plugboard(string, plugboard_position),
24                              rotor_position)25                               rotor_position))
25            func(encrypted)26            func(encrypted)
26        return inner27        return inner
27    return encryptor28    return encryptor
2829
2930
30def enigma_decrypt(plugboard_position, rotor_position):31def enigma_decrypt(plugboard_position, rotor_position):
31    def decryptor(func):32    def decryptor(func):
32        def inner(string):33        def inner(string):
t33            rotor_position_for_decrypting = dict( \t34            rotor_position_for_decrypting = (dict(
34                list((value, key) for key, value in rotor_position.items()))35                list((value, key) for key, value in rotor_position.items())))
35            decrypted = plugboard( \36            decrypted = (plugboard(
36                rotor(string, rotor_position_for_decrypting), plugboard_position)37                rotor(string, rotor_position_for_decrypting), plugboard_position))
37            func(decrypted)38            func(decrypted)
38        return inner39        return inner
39    return decryptor40    return decryptor
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def plugboard_helper(char, set_list):f1def plugboard_helper(char, set_list):
2    # converts a given character into another based on a given list of sets2    # converts a given character into another based on a given list of sets
3    for double in set_list:3    for double in set_list:
4        if char in double:4        if char in double:
5            converted_char = double - set(char)5            converted_char = double - set(char)
6            return ''.join(converted_char)6            return ''.join(converted_char)
7    return char7    return char
88
99
10def plugboard(string, set_list):10def plugboard(string, set_list):
11    converted = ''.join(list(plugboard_helper(char, set_list) for char in string))11    converted = ''.join(list(plugboard_helper(char, set_list) for char in string))
12    return converted12    return converted
1313
1414
15def rotor(string, dictionary):15def rotor(string, dictionary):
16    converted = ''.join(list(dictionary.get(char, char) for char in string))16    converted = ''.join(list(dictionary.get(char, char) for char in string))
17    return converted17    return converted
1818
1919
20def enigma_encrypt(plugboard_position, rotor_position):20def enigma_encrypt(plugboard_position, rotor_position):
21    def encryptor(func):21    def encryptor(func):
22        def inner(string):22        def inner(string):
23            encrypted = rotor(plugboard(string, plugboard_position), \23            encrypted = rotor(plugboard(string, plugboard_position), \
n24                               rotor_position)n24                              rotor_position)
25            func(encrypted)25            func(encrypted)
26        return inner26        return inner
27    return encryptor27    return encryptor
2828
2929
30def enigma_decrypt(plugboard_position, rotor_position):30def enigma_decrypt(plugboard_position, rotor_position):
31    def decryptor(func):31    def decryptor(func):
32        def inner(string):32        def inner(string):
t33            rotor_position_for_decrypting = dict(t33            rotor_position_for_decrypting = dict( \
34                [(value, key) for key, value in rotor_position.items()])34                list((value, key) for key, value in rotor_position.items()))
35            decrypted = plugboard(35            decrypted = plugboard( \
36                rotor(string, rotor_position_for_decrypting), plugboard_position)36                rotor(string, rotor_position_for_decrypting), plugboard_position)
37            func(decrypted)37            func(decrypted)
38        return inner38        return inner
39    return decryptor39    return decryptor
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def plugboard_helper(char, set_list):f1def plugboard_helper(char, set_list):
2    # converts a given character into another based on a given list of sets2    # converts a given character into another based on a given list of sets
3    for double in set_list:3    for double in set_list:
4        if char in double:4        if char in double:
5            converted_char = double - set(char)5            converted_char = double - set(char)
6            return ''.join(converted_char)6            return ''.join(converted_char)
7    return char7    return char
88
99
10def plugboard(string, set_list):10def plugboard(string, set_list):
11    converted = ''.join(list(plugboard_helper(char, set_list) for char in string))11    converted = ''.join(list(plugboard_helper(char, set_list) for char in string))
12    return converted12    return converted
1313
1414
15def rotor(string, dictionary):15def rotor(string, dictionary):
16    converted = ''.join(list(dictionary.get(char, char) for char in string))16    converted = ''.join(list(dictionary.get(char, char) for char in string))
17    return converted17    return converted
1818
1919
20def enigma_encrypt(plugboard_position, rotor_position):20def enigma_encrypt(plugboard_position, rotor_position):
21    def encryptor(func):21    def encryptor(func):
t22        def inner(str):t22        def inner(string):
23            encrypted = rotor(plugboard(str, plugboard_position), \23            encrypted = rotor(plugboard(string, plugboard_position), \
24                               rotor_position)24                               rotor_position)
25            func(encrypted)25            func(encrypted)
26        return inner26        return inner
27    return encryptor27    return encryptor
2828
2929
30def enigma_decrypt(plugboard_position, rotor_position):30def enigma_decrypt(plugboard_position, rotor_position):
31    def decryptor(func):31    def decryptor(func):
32        def inner(string):32        def inner(string):
33            rotor_position_for_decrypting = dict(33            rotor_position_for_decrypting = dict(
34                [(value, key) for key, value in rotor_position.items()])34                [(value, key) for key, value in rotor_position.items()])
35            decrypted = plugboard(35            decrypted = plugboard(
36                rotor(string, rotor_position_for_decrypting), plugboard_position)36                rotor(string, rotor_position_for_decrypting), plugboard_position)
37            func(decrypted)37            func(decrypted)
38        return inner38        return inner
39    return decryptor39    return decryptor
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1def plugboard_helper(char, list):n1def plugboard_helper(char, set_list):
2    # converts a given character into another based on a given list of sets2    # converts a given character into another based on a given list of sets
n3    for double in list:n3    for double in set_list:
4        if char in double:4        if char in double:
5            converted_char = double - set(char)5            converted_char = double - set(char)
6            return ''.join(converted_char)6            return ''.join(converted_char)
7    return char7    return char
88
99
n10def plugboard(str, list):n10def plugboard(stringset_list):
11    converted = ''.join([plugboard_helper(char, list) for char in str])11    converted = ''.join(list(plugboard_helper(char, set_list) for char in string))
12    return converted12    return converted
1313
1414
n15def rotor_helper(char, dict):n
16    # converts a given character into another based on a given dictionary
17    if char == ' ':
18        return char
19    return dict[char]
20 
21 
22def rotor(str, dict):15def rotor(string, dictionary):
23    converted = ''.join(list(rotor_helper(char, dict) for char in str))16    converted = ''.join(list(dictionary.get(char, char) for char in string))
24    return converted17    return converted
2518
2619
27def enigma_encrypt(plugboard_position, rotor_position):20def enigma_encrypt(plugboard_position, rotor_position):
28    def encryptor(func):21    def encryptor(func):
29        def inner(str):22        def inner(str):
n30            encrypted = rotor(n23            encrypted = rotor(plugboard(str, plugboard_position), \
31                plugboard(str, plugboard_position), rotor_position)24                               rotor_position)
32            func(encrypted)25            func(encrypted)
33        return inner26        return inner
34    return encryptor27    return encryptor
3528
3629
37def enigma_decrypt(plugboard_position, rotor_position):30def enigma_decrypt(plugboard_position, rotor_position):
38    def decryptor(func):31    def decryptor(func):
n39        def inner(str):n32        def inner(string):
40            rotor_position_for_decrypting = dict(33            rotor_position_for_decrypting = dict(
41                [(value, key) for key, value in rotor_position.items()])34                [(value, key) for key, value in rotor_position.items()])
42            decrypted = plugboard(35            decrypted = plugboard(
t43                rotor(str, rotor_position_for_decrypting), plugboard_position)t36                rotor(string, rotor_position_for_decrypting), plugboard_position)
44            func(decrypted)37            func(decrypted)
45        return inner38        return inner
46    return decryptor39    return decryptor
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op