Домашни > Енигма > Решения > Решението на Георги Кунчев

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

10 точки общо

9 успешни теста
0 неуспешни теста
Код (Showcasing messages in history log)

 1def plugboard(text, plugboard_position):
 2    output = ''
 3    for char in text:
 4        for flip in plugboard_position:
 5            if char in flip:
 6                output += (flip - {char}).pop()
 7                break
 8        else:
 9            output += char
10    return output
11
12
13def rotor(text, rotor_position):
14    rotor_position[' '] = ' '
15    return ''.join(map(lambda char: rotor_position[char] or char, text))
16
17
18def enigma_encrypt(plugboard_position, rotor_position):
19    def decorator(f):
20        def decorated(text):
21            flipped = plugboard(text, plugboard_position)
22            rotated = rotor(flipped, rotor_position)
23            return f(rotated)
24        return decorated
25    return decorator
26
27
28def enigma_decrypt(plugboard_position, rotor_position):
29    rotor_position = {
30        value: key for key, value in rotor_position.items()
31    }
32    def decorator(f):
33        def decorated(text):
34            rotated = rotor(text, rotor_position)
35            flipped = plugboard(rotated, plugboard_position)
36            return f(flipped)
37        return decorated
38    return decorator

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

OK

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

t1def plugboard(text, plugboard_position):t1def plugboard(text, plugboard_position):
2    output = ''2    output = ''
3    for char in text:3    for char in text:
4        for flip in plugboard_position:4        for flip in plugboard_position:
5            if char in flip:5            if char in flip:
6                output += (flip - {char}).pop()6                output += (flip - {char}).pop()
7                break7                break
8        else:8        else:
9            output += char9            output += char
10    return output10    return output
1111
1212
13def rotor(text, rotor_position):13def rotor(text, rotor_position):
14    rotor_position[' '] = ' '14    rotor_position[' '] = ' '
15    return ''.join(map(lambda char: rotor_position[char] or char, text))15    return ''.join(map(lambda char: rotor_position[char] or char, text))
1616
1717
18def enigma_encrypt(plugboard_position, rotor_position):18def enigma_encrypt(plugboard_position, rotor_position):
19    def decorator(f):19    def decorator(f):
20        def decorated(text):20        def decorated(text):
21            flipped = plugboard(text, plugboard_position)21            flipped = plugboard(text, plugboard_position)
22            rotated = rotor(flipped, rotor_position)22            rotated = rotor(flipped, rotor_position)
23            return f(rotated)23            return f(rotated)
24        return decorated24        return decorated
25    return decorator25    return decorator
2626
2727
28def enigma_decrypt(plugboard_position, rotor_position):28def enigma_decrypt(plugboard_position, rotor_position):
29    rotor_position = {29    rotor_position = {
30        value: key for key, value in rotor_position.items()30        value: key for key, value in rotor_position.items()
31    }31    }
32    def decorator(f):32    def decorator(f):
33        def decorated(text):33        def decorated(text):
34            rotated = rotor(text, rotor_position)34            rotated = rotor(text, rotor_position)
35            flipped = plugboard(rotated, plugboard_position)35            flipped = plugboard(rotated, plugboard_position)
36            return f(flipped)36            return f(flipped)
37        return decorated37        return decorated
38    return decorator38    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1def plugboard(text, plugboard_position):t1def plugboard(text, plugboard_position):
2    output = ''2    output = ''
3    for char in text:3    for char in text:
4        for flip in plugboard_position:4        for flip in plugboard_position:
5            if char in flip:5            if char in flip:
6                output += (flip - {char}).pop()6                output += (flip - {char}).pop()
7                break7                break
8        else:8        else:
9            output += char9            output += char
10    return output10    return output
1111
1212
13def rotor(text, rotor_position):13def rotor(text, rotor_position):
14    rotor_position[' '] = ' '14    rotor_position[' '] = ' '
15    return ''.join(map(lambda char: rotor_position[char] or char, text))15    return ''.join(map(lambda char: rotor_position[char] or char, text))
1616
1717
18def enigma_encrypt(plugboard_position, rotor_position):18def enigma_encrypt(plugboard_position, rotor_position):
19    def decorator(f):19    def decorator(f):
20        def decorated(text):20        def decorated(text):
21            flipped = plugboard(text, plugboard_position)21            flipped = plugboard(text, plugboard_position)
22            rotated = rotor(flipped, rotor_position)22            rotated = rotor(flipped, rotor_position)
23            return f(rotated)23            return f(rotated)
24        return decorated24        return decorated
25    return decorator25    return decorator
2626
2727
28def enigma_decrypt(plugboard_position, rotor_position):28def enigma_decrypt(plugboard_position, rotor_position):
29    rotor_position = {29    rotor_position = {
30        value: key for key, value in rotor_position.items()30        value: key for key, value in rotor_position.items()
31    }31    }
32    def decorator(f):32    def decorator(f):
33        def decorated(text):33        def decorated(text):
34            rotated = rotor(text, rotor_position)34            rotated = rotor(text, rotor_position)
35            flipped = plugboard(rotated, plugboard_position)35            flipped = plugboard(rotated, plugboard_position)
36            return f(flipped)36            return f(flipped)
37        return decorated37        return decorated
38    return decorator38    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op