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

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

10 точки общо

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

 1def plugboard(text: str, plugboard_positions: list):
 2    """Puts the input through the plugboard."""
 3    new_text = ''
 4    for letter in text:
 5        for plugboard_position in plugboard_positions:
 6            plugboard_position_copy = plugboard_position.copy()
 7            if letter in plugboard_position_copy:
 8                plugboard_position_copy.remove(letter)
 9                new_text += plugboard_position_copy.pop()
10                break
11        else:
12            new_text += letter
13    return new_text
14
15
16def rotor(text: str, rotor_positions: dict):
17    """Puts the input through the rotor."""
18    new_text = ''
19    for letter in text:
20        if letter not in rotor_positions.values():
21            new_text += letter
22            continue
23        new_text += rotor_positions[letter]
24    return new_text
25
26
27def enigma_encrypt(plugboard_position: list, rotor_position: dict):
28    """Returns decorator that executes the steps to encrypt the input."""
29    def encrypt(func):
30        def encrypt_string(text: str):
31            text = plugboard(text, plugboard_position)
32            text = rotor(text, rotor_position)
33            return func(text)
34        return encrypt_string
35    return encrypt
36
37
38def enigma_decrypt(plugboard_position: list, rotor_position: dict):
39    """Returns decorator that executes the steps to decrypt the input."""
40    def decrypt(func):
41        def decrypt_string(text: str):
42            new_rotor_position = {v: k for k, v in rotor_position.items()}
43            text = rotor(text, new_rotor_position)
44            text = plugboard(text, plugboard_position)
45            return func(text)
46        return decrypt_string
47    return decrypt

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

OK

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

f1f1
2def plugboard(text: str, plugboard_positions: list):2def plugboard(text: str, plugboard_positions: list):
n3    """Puts the input through the plugboard"""n3    """Puts the input through the plugboard."""
4    new_text = str()4    new_text = ''
5    for letter in text:5    for letter in text:
6        for plugboard_position in plugboard_positions:6        for plugboard_position in plugboard_positions:
7            plugboard_position_copy = plugboard_position.copy()7            plugboard_position_copy = plugboard_position.copy()
8            if letter in plugboard_position_copy:8            if letter in plugboard_position_copy:
9                plugboard_position_copy.remove(letter)9                plugboard_position_copy.remove(letter)
10                new_text += plugboard_position_copy.pop()10                new_text += plugboard_position_copy.pop()
11                break11                break
12        else:12        else:
13            new_text += letter13            new_text += letter
14    return new_text14    return new_text
1515
1616
17def rotor(text: str, rotor_positions: dict):17def rotor(text: str, rotor_positions: dict):
n18    """Puts the input through the rotor"""n18    """Puts the input through the rotor."""
19    new_text = str()19    new_text = ''
20    for letter in text:20    for letter in text:
21        if letter not in rotor_positions.values():21        if letter not in rotor_positions.values():
22            new_text += letter22            new_text += letter
23            continue23            continue
24        new_text += rotor_positions[letter]24        new_text += rotor_positions[letter]
25    return new_text25    return new_text
2626
2727
28def enigma_encrypt(plugboard_position: list, rotor_position: dict):28def enigma_encrypt(plugboard_position: list, rotor_position: dict):
n29    """Returns decorator that executes the steps to encrypt the input"""n29    """Returns decorator that executes the steps to encrypt the input."""
30    def encrypt(func):30    def encrypt(func):
31        def encrypt_string(text: str):31        def encrypt_string(text: str):
32            text = plugboard(text, plugboard_position)32            text = plugboard(text, plugboard_position)
33            text = rotor(text, rotor_position)33            text = rotor(text, rotor_position)
34            return func(text)34            return func(text)
35        return encrypt_string35        return encrypt_string
36    return encrypt36    return encrypt
3737
3838
39def enigma_decrypt(plugboard_position: list, rotor_position: dict):39def enigma_decrypt(plugboard_position: list, rotor_position: dict):
t40    """Returns decorator that executes the steps to decrypt the input"""t40    """Returns decorator that executes the steps to decrypt the input."""
41    def decrypt(func):41    def decrypt(func):
42        def decrypt_string(text: str):42        def decrypt_string(text: str):
43            new_rotor_position = {v: k for k, v in rotor_position.items()}43            new_rotor_position = {v: k for k, v in rotor_position.items()}
44            text = rotor(text, new_rotor_position)44            text = rotor(text, new_rotor_position)
45            text = plugboard(text, plugboard_position)45            text = plugboard(text, plugboard_position)
46            return func(text)46            return func(text)
47        return decrypt_string47        return decrypt_string
48    return decrypt48    return decrypt
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op