Домашни > Енигма > Решения > Решението на Калоян Крайнин

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

6 точки общо

5 успешни теста
4 неуспешни теста
Код

 1def switch(letter, connection):
 2    if letter not in connection:
 3        return letter
 4    for l in connection:
 5        if l != letter:
 6            return l
 7    return letter
 8
 9
10def plugboard_one_letter(letter, plugboard_position):
11    new_letter = letter
12    for connection in plugboard_position:
13        if letter not in connection:
14            continue
15        return switch(letter, connection)
16    return letter
17
18
19def plugboard(to_encrypt, plugboard_position):
20    return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt)))
21
22
23def rotor(to_encrypt, rotor_position):
24    return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))
25
26
27def rotor_decrycpt(to_decrypt, rotor_position):
28    decrypting_rotor_pos = {}
29    for key, value in rotor_position.items():
30        decrypting_rotor_pos[value] = key
31    return rotor(to_decrypt, decrypting_rotor_pos)
32
33
34def enigma_encrypt(plugboard_position, rotor_position):
35    def decorator(func):
36        def encrypted(string):
37            return func(rotor(plugboard(string, plugboard_position), rotor_position))
38        return encrypted
39    return decorator
40
41
42def enigma_decrypt(plugboard_position, rotor_position):
43    def decorator(func):
44        def decrypted(string):
45            return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position))
46        return decrypted
47    return decorator

E.EE....E
======================================================================
ERROR: 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')
File "/tmp/solution.py", line 45, in decrypted
return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position))
File "/tmp/solution.py", line 31, in rotor_decrycpt
return rotor(to_decrypt, decrypting_rotor_pos)
File "/tmp/solution.py", line 24, in rotor
return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))
TypeError: sequence item 1: expected str instance, NoneType found

======================================================================
ERROR: 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'),
File "/tmp/solution.py", line 45, in decrypted
return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position))
File "/tmp/solution.py", line 31, in rotor_decrycpt
return rotor(to_decrypt, decrypting_rotor_pos)
File "/tmp/solution.py", line 24, in rotor
return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))
TypeError: sequence item 3: expected str instance, NoneType found

======================================================================
ERROR: 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'),
File "/tmp/solution.py", line 37, in encrypted
return func(rotor(plugboard(string, plugboard_position), rotor_position))
File "/tmp/solution.py", line 24, in rotor
return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))
TypeError: sequence item 3: expected str instance, NoneType found

======================================================================
ERROR: 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')
File "/tmp/solution.py", line 24, in rotor
return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))
TypeError: sequence item 4: expected str instance, NoneType found

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

FAILED (errors=4)

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

f1def switch(letter, connection):f1def switch(letter, connection):
2    if letter not in connection:2    if letter not in connection:
3        return letter3        return letter
4    for l in connection:4    for l in connection:
5        if l != letter:5        if l != letter:
6            return l6            return l
7    return letter7    return letter
88
99
10def plugboard_one_letter(letter, plugboard_position):10def plugboard_one_letter(letter, plugboard_position):
11    new_letter = letter11    new_letter = letter
12    for connection in plugboard_position:12    for connection in plugboard_position:
13        if letter not in connection:13        if letter not in connection:
14            continue14            continue
15        return switch(letter, connection)15        return switch(letter, connection)
16    return letter16    return letter
1717
1818
19def plugboard(to_encrypt, plugboard_position):19def plugboard(to_encrypt, plugboard_position):
20    return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt)))20    return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt)))
2121
2222
23def rotor(to_encrypt, rotor_position):23def rotor(to_encrypt, rotor_position):
24    return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))24    return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))
2525
2626
27def rotor_decrycpt(to_decrypt, rotor_position):27def rotor_decrycpt(to_decrypt, rotor_position):
28    decrypting_rotor_pos = {}28    decrypting_rotor_pos = {}
29    for key, value in rotor_position.items():29    for key, value in rotor_position.items():
30        decrypting_rotor_pos[value] = key30        decrypting_rotor_pos[value] = key
31    return rotor(to_decrypt, decrypting_rotor_pos)31    return rotor(to_decrypt, decrypting_rotor_pos)
3232
3333
34def enigma_encrypt(plugboard_position, rotor_position):34def enigma_encrypt(plugboard_position, rotor_position):
35    def decorator(func):35    def decorator(func):
36        def encrypted(string):36        def encrypted(string):
37            return func(rotor(plugboard(string, plugboard_position), rotor_position))37            return func(rotor(plugboard(string, plugboard_position), rotor_position))
38        return encrypted38        return encrypted
39    return decorator39    return decorator
4040
4141
42def enigma_decrypt(plugboard_position, rotor_position):42def enigma_decrypt(plugboard_position, rotor_position):
43    def decorator(func):43    def decorator(func):
44        def decrypted(string):44        def decrypted(string):
45            return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position))45            return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position))
46        return decrypted46        return decrypted
47    return decorator47    return decorator
t48 t
49 
50plugboard_position = [{'a', 'c'}, {'t', 'z'}]
51rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',
52                  's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',
53                  'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',
54                  'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',
55                  'm': 'r', 'c': 'k'}
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def switch(letter, connection):f1def switch(letter, connection):
2    if letter not in connection:2    if letter not in connection:
3        return letter3        return letter
4    for l in connection:4    for l in connection:
5        if l != letter:5        if l != letter:
6            return l6            return l
7    return letter7    return letter
88
99
10def plugboard_one_letter(letter, plugboard_position):10def plugboard_one_letter(letter, plugboard_position):
11    new_letter = letter11    new_letter = letter
12    for connection in plugboard_position:12    for connection in plugboard_position:
13        if letter not in connection:13        if letter not in connection:
14            continue14            continue
15        return switch(letter, connection)15        return switch(letter, connection)
16    return letter16    return letter
1717
1818
19def plugboard(to_encrypt, plugboard_position):19def plugboard(to_encrypt, plugboard_position):
20    return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt)))20    return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt)))
2121
2222
23def rotor(to_encrypt, rotor_position):23def rotor(to_encrypt, rotor_position):
24    return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))24    return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))
2525
2626
27def rotor_decrycpt(to_decrypt, rotor_position):27def rotor_decrycpt(to_decrypt, rotor_position):
28    decrypting_rotor_pos = {}28    decrypting_rotor_pos = {}
n29    for kvp in rotor_position.items():n29    for key, value in rotor_position.items():
30        decrypting_rotor_pos[kvp[1]] = kvp[0]30        decrypting_rotor_pos[value] = key
31    return rotor(to_decrypt, decrypting_rotor_pos)31    return rotor(to_decrypt, decrypting_rotor_pos)
3232
3333
34def enigma_encrypt(plugboard_position, rotor_position):34def enigma_encrypt(plugboard_position, rotor_position):
35    def decorator(func):35    def decorator(func):
36        def encrypted(string):36        def encrypted(string):
37            return func(rotor(plugboard(string, plugboard_position), rotor_position))37            return func(rotor(plugboard(string, plugboard_position), rotor_position))
38        return encrypted38        return encrypted
39    return decorator39    return decorator
4040
4141
42def enigma_decrypt(plugboard_position, rotor_position):42def enigma_decrypt(plugboard_position, rotor_position):
43    def decorator(func):43    def decorator(func):
44        def decrypted(string):44        def decrypted(string):
45            return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position))45            return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position))
46        return decrypted46        return decrypted
47    return decorator47    return decorator
tt48 
49 
50plugboard_position = [{'a', 'c'}, {'t', 'z'}]
51rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',
52                  's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',
53                  'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',
54                  'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',
55                  'm': 'r', 'c': 'k'}
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def switch(letter, connection):f1def switch(letter, connection):
2    if letter not in connection:2    if letter not in connection:
3        return letter3        return letter
4    for l in connection:4    for l in connection:
5        if l != letter:5        if l != letter:
6            return l6            return l
7    return letter7    return letter
88
99
10def plugboard_one_letter(letter, plugboard_position):10def plugboard_one_letter(letter, plugboard_position):
11    new_letter = letter11    new_letter = letter
12    for connection in plugboard_position:12    for connection in plugboard_position:
13        if letter not in connection:13        if letter not in connection:
14            continue14            continue
t15        new_letter = switch(letter, connection)t15        return switch(letter, connection)
16        break
17    return new_letter16    return letter
1817
1918
20def plugboard(to_encrypt, plugboard_position):19def plugboard(to_encrypt, plugboard_position):
21    return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt)))20    return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt)))
2221
2322
24def rotor(to_encrypt, rotor_position):23def rotor(to_encrypt, rotor_position):
25    return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))24    return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))
2625
2726
28def rotor_decrycpt(to_decrypt, rotor_position):27def rotor_decrycpt(to_decrypt, rotor_position):
29    decrypting_rotor_pos = {}28    decrypting_rotor_pos = {}
30    for kvp in rotor_position.items():29    for kvp in rotor_position.items():
31        decrypting_rotor_pos[kvp[1]] = kvp[0]30        decrypting_rotor_pos[kvp[1]] = kvp[0]
32    return rotor(to_decrypt, decrypting_rotor_pos)31    return rotor(to_decrypt, decrypting_rotor_pos)
3332
3433
35def enigma_encrypt(plugboard_position, rotor_position):34def enigma_encrypt(plugboard_position, rotor_position):
36    def decorator(func):35    def decorator(func):
37        def encrypted(string):36        def encrypted(string):
38            return func(rotor(plugboard(string, plugboard_position), rotor_position))37            return func(rotor(plugboard(string, plugboard_position), rotor_position))
39        return encrypted38        return encrypted
40    return decorator39    return decorator
4140
4241
43def enigma_decrypt(plugboard_position, rotor_position):42def enigma_decrypt(plugboard_position, rotor_position):
44    def decorator(func):43    def decorator(func):
45        def decrypted(string):44        def decrypted(string):
46            return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position))45            return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position))
47        return decrypted46        return decrypted
48    return decorator47    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1def check_and_switch(letter, connection):n1def switch(letter, connection):
2    if letter not in connection:2    if letter not in connection:
3        return letter3        return letter
4    for l in connection:4    for l in connection:
5        if l != letter:5        if l != letter:
6            return l6            return l
7    return letter7    return letter
88
99
10def plugboard_one_letter(letter, plugboard_position):10def plugboard_one_letter(letter, plugboard_position):
11    new_letter = letter11    new_letter = letter
12    for connection in plugboard_position:12    for connection in plugboard_position:
tt13        if letter not in connection:
14            continue
13        new_letter = check_and_switch(letter, connection)15        new_letter = switch(letter, connection)
16        break
14    return new_letter17    return new_letter
1518
1619
17def plugboard(to_encrypt, plugboard_position):20def plugboard(to_encrypt, plugboard_position):
18    return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt)))21    return "".join(list(map(lambda l: plugboard_one_letter(l, plugboard_position), to_encrypt)))
1922
2023
21def rotor(to_encrypt, rotor_position):24def rotor(to_encrypt, rotor_position):
22    return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))25    return "".join(list(map(lambda l: rotor_position.get(l), to_encrypt)))
2326
2427
25def rotor_decrycpt(to_decrypt, rotor_position):28def rotor_decrycpt(to_decrypt, rotor_position):
26    decrypting_rotor_pos = {}29    decrypting_rotor_pos = {}
27    for kvp in rotor_position.items():30    for kvp in rotor_position.items():
28        decrypting_rotor_pos[kvp[1]] = kvp[0]31        decrypting_rotor_pos[kvp[1]] = kvp[0]
29    return rotor(to_decrypt, decrypting_rotor_pos)32    return rotor(to_decrypt, decrypting_rotor_pos)
3033
3134
32def enigma_encrypt(plugboard_position, rotor_position):35def enigma_encrypt(plugboard_position, rotor_position):
33    def decorator(func):36    def decorator(func):
34        def encrypted(string):37        def encrypted(string):
35            return func(rotor(plugboard(string, plugboard_position), rotor_position))38            return func(rotor(plugboard(string, plugboard_position), rotor_position))
36        return encrypted39        return encrypted
37    return decorator40    return decorator
3841
3942
40def enigma_decrypt(plugboard_position, rotor_position):43def enigma_decrypt(plugboard_position, rotor_position):
41    def decorator(func):44    def decorator(func):
42        def decrypted(string):45        def decrypted(string):
43            return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position))46            return func(plugboard(rotor_decrycpt(string, rotor_position), plugboard_position))
44        return decrypted47        return decrypted
45    return decorator48    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op