Домашни > Енигма > Решения > Решението на Магдалена Иванова

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

10 точки общо

9 успешни теста
0 неуспешни теста
Код

 1import copy
 2
 3def plugboard(the_text, matches):
 4    from_set_to_str = ""
 5    ready = ""
 6
 7    for character in the_text:
 8        new_matches = ""
 9        for one_pair in  matches:
10            if character in one_pair:
11               new_matches = ''.join(one_pair)
12               from_set_to_str = copy.deepcopy(one_pair)
13               from_set_to_str.remove(character) 
14               res=''.join(from_set_to_str)
15               ready += res
16               break
17        if character not in new_matches:
18            ready += character
19    return ready
20
21def rotor(the_text, matches):
22    ready=""
23
24    for character in the_text:
25        if character == " ":
26             ready += " "
27        if character in matches: 
28            res = matches.get(character)
29            ready += res
30    return ready
31
32def enigma_encrypt(plugboard_position, rotor_position):
33    def encryptor(func):
34        def encrypt_print(txt):
35            first = plugboard(txt, plugboard_position)
36            result = rotor(first, rotor_position)
37            return func(result)
38        return encrypt_print
39    return encryptor
40
41def enigma_decrypt(plugboard_position, rotor_position):
42    def decryptor(func):
43        def decrypt_print(txt):
44            res = {v: k for k, v in rotor_position.items()}
45            first = rotor(txt, res)
46            result = plugboard(first, plugboard_position)
47            return func(result)
48        return decrypt_print
49    return decryptor
50
51
52
53
54
55plugboard_position = [{'a', 'c'}, {'t', 'z'}]
56rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',
57                  's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',
58                  'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',
59                  'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',
60                  'm': 'r', 'c': 'k'}
61
62rotor('enigma', rotor_position) # tnwora
63plugboard('enigma', plugboard_position) # enigmc
64
65encryptor = enigma_encrypt(plugboard_position=plugboard_position,
66                           rotor_position=rotor_position)
67decryptor = enigma_decrypt(plugboard_position=plugboard_position,
68                           rotor_position=rotor_position)
69
70encrypt_print = encryptor(print)
71decrypt_print = decryptor(print)
72
73encrypt_print('enigma') # tnwork
74decrypt_print('tnwork') # enigma   

tnwork
enigma
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s

OK

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

f1import copyf1import copy
22
3def plugboard(the_text, matches):3def plugboard(the_text, matches):
4    from_set_to_str = ""4    from_set_to_str = ""
5    ready = ""5    ready = ""
66
7    for character in the_text:7    for character in the_text:
8        new_matches = ""8        new_matches = ""
9        for one_pair in  matches:9        for one_pair in  matches:
10            if character in one_pair:10            if character in one_pair:
11               new_matches = ''.join(one_pair)11               new_matches = ''.join(one_pair)
12               from_set_to_str = copy.deepcopy(one_pair)12               from_set_to_str = copy.deepcopy(one_pair)
13               from_set_to_str.remove(character) 13               from_set_to_str.remove(character) 
14               res=''.join(from_set_to_str)14               res=''.join(from_set_to_str)
15               ready += res15               ready += res
16               break16               break
17        if character not in new_matches:17        if character not in new_matches:
18            ready += character18            ready += character
19    return ready19    return ready
2020
21def rotor(the_text, matches):21def rotor(the_text, matches):
22    ready=""22    ready=""
2323
24    for character in the_text:24    for character in the_text:
25        if character == " ":25        if character == " ":
26             ready += " "26             ready += " "
t27        for one_pair in matches:t27        if character in matches: 
28            if character == str(one_pair): 28            res = matches.get(character)
29               res = matches[one_pair]
30               ready += res29            ready += res
31               break
32    return ready30    return ready
3331
34def enigma_encrypt(plugboard_position, rotor_position):32def enigma_encrypt(plugboard_position, rotor_position):
35    def encryptor(func):33    def encryptor(func):
36        def encrypt_print(txt):34        def encrypt_print(txt):
37            first = plugboard(txt, plugboard_position)35            first = plugboard(txt, plugboard_position)
38            result = rotor(first, rotor_position)36            result = rotor(first, rotor_position)
39            return func(result)37            return func(result)
40        return encrypt_print38        return encrypt_print
41    return encryptor39    return encryptor
4240
43def enigma_decrypt(plugboard_position, rotor_position):41def enigma_decrypt(plugboard_position, rotor_position):
44    def decryptor(func):42    def decryptor(func):
45        def decrypt_print(txt):43        def decrypt_print(txt):
46            res = {v: k for k, v in rotor_position.items()}44            res = {v: k for k, v in rotor_position.items()}
47            first = rotor(txt, res)45            first = rotor(txt, res)
48            result = plugboard(first, plugboard_position)46            result = plugboard(first, plugboard_position)
49            return func(result)47            return func(result)
50        return decrypt_print48        return decrypt_print
51    return decryptor49    return decryptor
5250
5351
5452
5553
5654
57plugboard_position = [{'a', 'c'}, {'t', 'z'}]55plugboard_position = [{'a', 'c'}, {'t', 'z'}]
58rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',56rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',
59                  's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',57                  's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',
60                  'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',58                  'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',
61                  'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',59                  'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',
62                  'm': 'r', 'c': 'k'}60                  'm': 'r', 'c': 'k'}
6361
64rotor('enigma', rotor_position) # tnwora62rotor('enigma', rotor_position) # tnwora
65plugboard('enigma', plugboard_position) # enigmc63plugboard('enigma', plugboard_position) # enigmc
6664
67encryptor = enigma_encrypt(plugboard_position=plugboard_position,65encryptor = enigma_encrypt(plugboard_position=plugboard_position,
68                           rotor_position=rotor_position)66                           rotor_position=rotor_position)
69decryptor = enigma_decrypt(plugboard_position=plugboard_position,67decryptor = enigma_decrypt(plugboard_position=plugboard_position,
70                           rotor_position=rotor_position)68                           rotor_position=rotor_position)
7169
72encrypt_print = encryptor(print)70encrypt_print = encryptor(print)
73decrypt_print = decryptor(print)71decrypt_print = decryptor(print)
7472
75encrypt_print('enigma') # tnwork73encrypt_print('enigma') # tnwork
76decrypt_print('tnwork') # enigma   74decrypt_print('tnwork') # enigma   
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import copyf1import copy
22
3def plugboard(the_text, matches):3def plugboard(the_text, matches):
n4    from_set_to_str=""n4    from_set_to_str = ""
5    ready=""5    ready = ""
66
7    for character in the_text:7    for character in the_text:
8        new_matches = ""8        new_matches = ""
9        for one_pair in  matches:9        for one_pair in  matches:
10            if character in one_pair:10            if character in one_pair:
11               new_matches = ''.join(one_pair)11               new_matches = ''.join(one_pair)
12               from_set_to_str = copy.deepcopy(one_pair)12               from_set_to_str = copy.deepcopy(one_pair)
13               from_set_to_str.remove(character) 13               from_set_to_str.remove(character) 
14               res=''.join(from_set_to_str)14               res=''.join(from_set_to_str)
15               ready += res15               ready += res
16               break16               break
17        if character not in new_matches:17        if character not in new_matches:
18            ready += character18            ready += character
19    return ready19    return ready
2020
21def rotor(the_text, matches):21def rotor(the_text, matches):
22    ready=""22    ready=""
2323
24    for character in the_text:24    for character in the_text:
25        if character == " ":25        if character == " ":
26             ready += " "26             ready += " "
t27        for one_pair in  matches:t27        for one_pair in matches:
28            if character == str(one_pair): 28            if character == str(one_pair): 
29               res = matches[one_pair]29               res = matches[one_pair]
30               ready += res30               ready += res
31               break31               break
32    return ready32    return ready
3333
34def enigma_encrypt(plugboard_position, rotor_position):34def enigma_encrypt(plugboard_position, rotor_position):
35    def encryptor(func):35    def encryptor(func):
36        def encrypt_print(txt):36        def encrypt_print(txt):
37            first = plugboard(txt, plugboard_position)37            first = plugboard(txt, plugboard_position)
38            result = rotor(first, rotor_position)38            result = rotor(first, rotor_position)
39            return func(result)39            return func(result)
40        return encrypt_print40        return encrypt_print
41    return encryptor41    return encryptor
4242
43def enigma_decrypt(plugboard_position, rotor_position):43def enigma_decrypt(plugboard_position, rotor_position):
44    def decryptor(func):44    def decryptor(func):
45        def decrypt_print(txt):45        def decrypt_print(txt):
46            res = {v: k for k, v in rotor_position.items()}46            res = {v: k for k, v in rotor_position.items()}
47            first = rotor(txt, res)47            first = rotor(txt, res)
48            result = plugboard(first, plugboard_position)48            result = plugboard(first, plugboard_position)
49            return func(result)49            return func(result)
50        return decrypt_print50        return decrypt_print
51    return decryptor51    return decryptor
5252
5353
5454
5555
5656
57plugboard_position = [{'a', 'c'}, {'t', 'z'}]57plugboard_position = [{'a', 'c'}, {'t', 'z'}]
58rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',58rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',
59                  's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',59                  's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',
60                  'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',60                  'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',
61                  'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',61                  'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',
62                  'm': 'r', 'c': 'k'}62                  'm': 'r', 'c': 'k'}
6363
64rotor('enigma', rotor_position) # tnwora64rotor('enigma', rotor_position) # tnwora
65plugboard('enigma', plugboard_position) # enigmc65plugboard('enigma', plugboard_position) # enigmc
6666
67encryptor = enigma_encrypt(plugboard_position=plugboard_position,67encryptor = enigma_encrypt(plugboard_position=plugboard_position,
68                           rotor_position=rotor_position)68                           rotor_position=rotor_position)
69decryptor = enigma_decrypt(plugboard_position=plugboard_position,69decryptor = enigma_decrypt(plugboard_position=plugboard_position,
70                           rotor_position=rotor_position)70                           rotor_position=rotor_position)
7171
72encrypt_print = encryptor(print)72encrypt_print = encryptor(print)
73decrypt_print = decryptor(print)73decrypt_print = decryptor(print)
7474
75encrypt_print('enigma') # tnwork75encrypt_print('enigma') # tnwork
76decrypt_print('tnwork') # enigma   76decrypt_print('tnwork') # enigma   
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op