Домашни > Енигма > Решения > Решението на Момчил Узунов

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

10 точки общо

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

 1def plugboard(text, plug):
 2    result = ''
 3    for letter in text:     
 4        for i in plug:            
 5            if letter in i:
 6                result += ''.join(i - set(letter))   
 7                break           
 8        else:                     
 9            result += letter          
10    print(result)
11    return result
12        
13def rotor(text, rot):
14    result = ''
15    for letter in text:     
16        result += str(rot.get(letter, ' '))
17    print(result)
18    return result
19        
20def enigma_encrypt(plugboard_position, rotor_position):
21    def decorator(func):
22        def dummy_func(n):
23            n = plugboard(n, plugboard_position)
24            n = rotor(n,rotor_position)
25            return func(n)
26        return dummy_func     
27    return decorator
28
29def enigma_decrypt(plugboard_position, rotor_position):
30    def decorator(func):
31        def dummy_func(n):
32            rotor_position_2 = {v:k for k,v in rotor_position.items()}
33            n = rotor(n,rotor_position_2)
34            n = plugboard(n, plugboard_position)
35            return func(n)
36        return dummy_func
37    return decorator

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

OK

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

f1def plugboard(text, plug):f1def plugboard(text, plug):
2    result = ''2    result = ''
n3    for letter in text:  n3    for letter in text:     
4        flag = False     
5        for i in plug:            4        for i in plug:            
6            if letter in i:5            if letter in i:
n7                flag = Truen
8                result += ''.join(i - set(letter))6                result += ''.join(i - set(letter))   
7                break           
9        if not flag:                        8        else:                     
10            result += letter          9            result += letter          
nn10    print(result)
11    return result11    return result
12        12        
13def rotor(text, rot):13def rotor(text, rot):
14    result = ''14    result = ''
15    for letter in text:     15    for letter in text:     
n16        if letter not in rot:n16        result += str(rot.get(letter, ' '))
17            result += letter17    print(result)
18        else:
19            result += rot[letter]
20    return result18    return result
21        19        
22def enigma_encrypt(plugboard_position, rotor_position):20def enigma_encrypt(plugboard_position, rotor_position):
n23    def decorator1(func):n21    def decorator(func):
24        def dummy_func(n):22        def dummy_func(n):
25            n = plugboard(n, plugboard_position)23            n = plugboard(n, plugboard_position)
26            n = rotor(n,rotor_position)24            n = rotor(n,rotor_position)
27            return func(n)25            return func(n)
28        return dummy_func     26        return dummy_func     
n29    return decorator1n27    return decorator
3028
31def enigma_decrypt(plugboard_position, rotor_position):29def enigma_decrypt(plugboard_position, rotor_position):
n32    def decorator2(func):n30    def decorator(func):
33        def dummy_func(n):31        def dummy_func(n):
n34            rotor_position_2={v:k for k,v in rotor_position.items()}n32            rotor_position_2 = {v:k for k,v in rotor_position.items()}
35            n = rotor(n,rotor_position_2)33            n = rotor(n,rotor_position_2)
36            n = plugboard(n, plugboard_position)34            n = plugboard(n, plugboard_position)
37            return func(n)35            return func(n)
38        return dummy_func36        return dummy_func
t39    return decorator2t37    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def plugboard(text, plug):f1def plugboard(text, plug):
2    result = ''2    result = ''
3    for letter in text:  3    for letter in text:  
4        flag = False     4        flag = False     
5        for i in plug:            5        for i in plug:            
6            if letter in i:6            if letter in i:
7                flag = True7                flag = True
8                result += ''.join(i - set(letter))8                result += ''.join(i - set(letter))
9        if not flag:                        9        if not flag:                        
10            result += letter          10            result += letter          
11    return result11    return result
12        12        
13def rotor(text, rot):13def rotor(text, rot):
14    result = ''14    result = ''
15    for letter in text:     15    for letter in text:     
16        if letter not in rot:16        if letter not in rot:
17            result += letter17            result += letter
18        else:18        else:
19            result += rot[letter]19            result += rot[letter]
20    return result20    return result
21        21        
22def enigma_encrypt(plugboard_position, rotor_position):22def enigma_encrypt(plugboard_position, rotor_position):
23    def decorator1(func):23    def decorator1(func):
24        def dummy_func(n):24        def dummy_func(n):
n25            n=plugboard(n, plugboard_position)n25            n = plugboard(n, plugboard_position)
26            n=rotor(n,rotor_position)26            n = rotor(n,rotor_position)
27            return func(n)27            return func(n)
28        return dummy_func     28        return dummy_func     
29    return decorator129    return decorator1
3030
31def enigma_decrypt(plugboard_position, rotor_position):31def enigma_decrypt(plugboard_position, rotor_position):
32    def decorator2(func):32    def decorator2(func):
33        def dummy_func(n):33        def dummy_func(n):
34            rotor_position_2={v:k for k,v in rotor_position.items()}34            rotor_position_2={v:k for k,v in rotor_position.items()}
t35            n=rotor(n,rotor_position_2)t35            n = rotor(n,rotor_position_2)
36            n=plugboard(n, plugboard_position)36            n = plugboard(n, plugboard_position)
37            return func(n)37            return func(n)
38        return dummy_func38        return dummy_func
39    return decorator239    return decorator2
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def plugboard(text, plug):f1def plugboard(text, plug):
2    result = ''2    result = ''
3    for letter in text:  3    for letter in text:  
4        flag = False     4        flag = False     
5        for i in plug:            5        for i in plug:            
6            if letter in i:6            if letter in i:
n7                flag=Truen7                flag = True
8                result += ''.join(i - set(letter))8                result += ''.join(i - set(letter))
9        if not flag:                        9        if not flag:                        
10            result += letter          10            result += letter          
11    return result11    return result
12        12        
13def rotor(text, rot):13def rotor(text, rot):
14    result = ''14    result = ''
15    for letter in text:     15    for letter in text:     
16        if letter not in rot:16        if letter not in rot:
17            result += letter17            result += letter
18        else:18        else:
19            result += rot[letter]19            result += rot[letter]
20    return result20    return result
21        21        
22def enigma_encrypt(plugboard_position, rotor_position):22def enigma_encrypt(plugboard_position, rotor_position):
23    def decorator1(func):23    def decorator1(func):
n24        def ankara(n):n24        def dummy_func(n):
25            n=plugboard(n, plugboard_position)25            n=plugboard(n, plugboard_position)
26            n=rotor(n,rotor_position)26            n=rotor(n,rotor_position)
27            return func(n)27            return func(n)
n28        return ankara     n28        return dummy_func     
29    return decorator129    return decorator1
3030
31def enigma_decrypt(plugboard_position, rotor_position):31def enigma_decrypt(plugboard_position, rotor_position):
32    def decorator2(func):32    def decorator2(func):
n33        def ankara(n):n33        def dummy_func(n):
34            rotor_position_2={v:k for k,v in rotor_position.items()}34            rotor_position_2={v:k for k,v in rotor_position.items()}
35            n=rotor(n,rotor_position_2)35            n=rotor(n,rotor_position_2)
36            n=plugboard(n, plugboard_position)36            n=plugboard(n, plugboard_position)
37            return func(n)37            return func(n)
t38        return ankarat38        return dummy_func
39    return decorator239    return decorator2
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op