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

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

10 точки общо

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

  1def plugboard(word, plugboard_position):
  2    transformed_word = []
  3    for letter in word:
  4        for pair_of_letters in plugboard_position:
  5            if letter in pair_of_letters:
  6                transformed_word.append((pair_of_letters - {letter}).pop())
  7                break
  8        else:
  9            transformed_word.append(letter)
 10
 11    return "".join(transformed_word)
 12
 13
 14def rotor(word, rotor_position):
 15    transformed_word = []
 16    for letter in word:
 17        value = rotor_position.get(letter, letter)
 18        transformed_word.append(value)
 19    return "".join(transformed_word)
 20
 21
 22def enigma_encrypt(plugboard_position, rotor_position):
 23    def decorator(func):
 24        def wrapper(word):
 25            plugboarded_word = plugboard(word, plugboard_position)
 26            encrypted_word = rotor(plugboarded_word, rotor_position)
 27            return func(encrypted_word)
 28
 29        return wrapper
 30
 31    return decorator
 32
 33
 34def enigma_decrypt(plugboard_position, rotor_position):
 35    def decorator(func):
 36        def wrapper(word):
 37            swapped_rotor_position = {v: k for k, v in rotor_position.items()}
 38            encrypted_word = rotor(word, swapped_rotor_position)
 39            plugboarded_word = plugboard(encrypted_word, plugboard_position)
 40            return func(plugboarded_word)
 41
 42        return wrapper
 43
 44    return decorator
 45
 46
 47def crypt_behaivior(additional_func):
 48    def decorator(logic_func):
 49        def wrapper(*args):
 50            additional_func(logic_func(*args))
 51
 52        return wrapper
 53
 54    return decorator
 55
 56
 57def encrypt(word):
 58    return word
 59
 60
 61def decrypt(word):
 62    return word
 63
 64
 65plugboard_position = [{"a", "c"}, {"t", "z"}, {"k", "l"}, {"d", "y"}, {"s", "j"}]
 66rotor_position = {
 67    "v": "d",
 68    "d": "v",
 69    "y": "u",
 70    "n": "n",
 71    "i": "w",
 72    "z": "p",
 73    "s": "e",
 74    "x": "s",
 75    "h": "f",
 76    "b": "x",
 77    "u": "c",
 78    "p": "q",
 79    "r": "g",
 80    "q": "j",
 81    "e": "t",
 82    "l": "y",
 83    "o": "z",
 84    "g": "o",
 85    "k": "b",
 86    "t": "h",
 87    "j": "m",
 88    "a": "a",
 89    "w": "i",
 90    "f": "l",
 91    "m": "r",
 92    "c": "k",
 93}
 94
 95
 96encrypt = enigma_encrypt(plugboard_position, rotor_position)(encrypt)
 97decrypt = enigma_decrypt(plugboard_position, rotor_position)(decrypt)
 98
 99
100print(decrypt(encrypt("hitler is awesome")))

hitler is awesome
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s

OK

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

f1def plugboard(word, plugboard_position):f1def plugboard(word, plugboard_position):
2    transformed_word = []2    transformed_word = []
3    for letter in word:3    for letter in word:
n4        written = Falsen
5        for pair_of_letters in plugboard_position:4        for pair_of_letters in plugboard_position:
6            if letter in pair_of_letters:5            if letter in pair_of_letters:
7                transformed_word.append((pair_of_letters - {letter}).pop())6                transformed_word.append((pair_of_letters - {letter}).pop())
n8                written = Truen
9                break7                break
n10        if not written:n8        else:
11            transformed_word.append(letter)9            transformed_word.append(letter)
1210
13    return "".join(transformed_word)11    return "".join(transformed_word)
1412
1513
16def rotor(word, rotor_position):14def rotor(word, rotor_position):
17    transformed_word = []15    transformed_word = []
18    for letter in word:16    for letter in word:
t19        value = rotor_position.get(letter, None)t17        value = rotor_position.get(letter, letter)
20        if value is not None:
21            transformed_word.append(value)18        transformed_word.append(value)
22            continue
23        else:
24            transformed_word.append(letter)
25    return "".join(transformed_word)19    return "".join(transformed_word)
2620
2721
28def enigma_encrypt(plugboard_position, rotor_position):22def enigma_encrypt(plugboard_position, rotor_position):
29    def decorator(func):23    def decorator(func):
30        def wrapper(word):24        def wrapper(word):
31            plugboarded_word = plugboard(word, plugboard_position)25            plugboarded_word = plugboard(word, plugboard_position)
32            encrypted_word = rotor(plugboarded_word, rotor_position)26            encrypted_word = rotor(plugboarded_word, rotor_position)
33            return func(encrypted_word)27            return func(encrypted_word)
3428
35        return wrapper29        return wrapper
3630
37    return decorator31    return decorator
3832
3933
40def enigma_decrypt(plugboard_position, rotor_position):34def enigma_decrypt(plugboard_position, rotor_position):
41    def decorator(func):35    def decorator(func):
42        def wrapper(word):36        def wrapper(word):
43            swapped_rotor_position = {v: k for k, v in rotor_position.items()}37            swapped_rotor_position = {v: k for k, v in rotor_position.items()}
44            encrypted_word = rotor(word, swapped_rotor_position)38            encrypted_word = rotor(word, swapped_rotor_position)
45            plugboarded_word = plugboard(encrypted_word, plugboard_position)39            plugboarded_word = plugboard(encrypted_word, plugboard_position)
46            return func(plugboarded_word)40            return func(plugboarded_word)
4741
48        return wrapper42        return wrapper
4943
50    return decorator44    return decorator
5145
5246
53def crypt_behaivior(additional_func):47def crypt_behaivior(additional_func):
54    def decorator(logic_func):48    def decorator(logic_func):
55        def wrapper(*args):49        def wrapper(*args):
56            additional_func(logic_func(*args))50            additional_func(logic_func(*args))
5751
58        return wrapper52        return wrapper
5953
60    return decorator54    return decorator
6155
6256
63def encrypt(word):57def encrypt(word):
64    return word58    return word
6559
6660
67def decrypt(word):61def decrypt(word):
68    return word62    return word
6963
7064
71plugboard_position = [{"a", "c"}, {"t", "z"}, {"k", "l"}, {"d", "y"}, {"s", "j"}]65plugboard_position = [{"a", "c"}, {"t", "z"}, {"k", "l"}, {"d", "y"}, {"s", "j"}]
72rotor_position = {66rotor_position = {
73    "v": "d",67    "v": "d",
74    "d": "v",68    "d": "v",
75    "y": "u",69    "y": "u",
76    "n": "n",70    "n": "n",
77    "i": "w",71    "i": "w",
78    "z": "p",72    "z": "p",
79    "s": "e",73    "s": "e",
80    "x": "s",74    "x": "s",
81    "h": "f",75    "h": "f",
82    "b": "x",76    "b": "x",
83    "u": "c",77    "u": "c",
84    "p": "q",78    "p": "q",
85    "r": "g",79    "r": "g",
86    "q": "j",80    "q": "j",
87    "e": "t",81    "e": "t",
88    "l": "y",82    "l": "y",
89    "o": "z",83    "o": "z",
90    "g": "o",84    "g": "o",
91    "k": "b",85    "k": "b",
92    "t": "h",86    "t": "h",
93    "j": "m",87    "j": "m",
94    "a": "a",88    "a": "a",
95    "w": "i",89    "w": "i",
96    "f": "l",90    "f": "l",
97    "m": "r",91    "m": "r",
98    "c": "k",92    "c": "k",
99}93}
10094
10195
102encrypt = enigma_encrypt(plugboard_position, rotor_position)(encrypt)96encrypt = enigma_encrypt(plugboard_position, rotor_position)(encrypt)
103decrypt = enigma_decrypt(plugboard_position, rotor_position)(decrypt)97decrypt = enigma_decrypt(plugboard_position, rotor_position)(decrypt)
10498
10599
106print(decrypt(encrypt("hitler is awesome")))100print(decrypt(encrypt("hitler is awesome")))
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op