1plugboard_position = [{'a', 'c'}, {'t', 'z'}]
2rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',
3 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',
4 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',
5 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',
6 'm': 'r', 'c': 'k'}
7
8
9def plugboard(text, list_of_rules):
10 changed_word = ""
11
12 for letter in text:
13 for set_of_letters in list_of_rules:
14 if letter in set_of_letters:
15 letter = set_of_letters.difference({letter}).pop()
16
17 changed_word += letter
18
19 return changed_word
20
21def rotor(text, dict_of_rules):
22 changed_text = ""
23
24 for letter in text:
25 changed_text += dict_of_rules.get(letter, " ")
26
27 return changed_text
28
29def enigma_encrypt(plugboard_position, rotor_position):
30 def decorator(func):
31 def crypting(text):
32 crypted_text = plugboard(text, plugboard_position)
33 crypted_text = rotor(crypted_text, rotor_position)
34 return func(crypted_text)
35 return crypting
36 return decorator
37
38@enigma_encrypt(plugboard_position, rotor_position)
39def encrypt_decorated(text):
40 return text
41
42def enigma_decrypt(plugboard_position, rotor_position):
43 def decorator(func):
44 def decrypting(text):
45 rotor_position_reversed = {y: x for x, y in rotor_position.items()}
46 decrypted_text = rotor(text, rotor_position_reversed)
47 decrypted_text = plugboard(decrypted_text, plugboard_position)
48
49 return func(decrypted_text)
50 return decrypting
51 return decorator
52
53@enigma_decrypt(plugboard_position, rotor_position)
54def decrypt_decorated(text):
55 return text
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
Георги Кунчев
30.10.2023 19:28Да, хванах идеята за стринга и `.join()`, но беше прекалено изкуствено.
Това ти решение е далеч по-добре. Индекс нула отново е изкуствен, но така е по-добре. Можеш да видиш `pop()` метода на `set`.
|
Нелина Тотева
30.10.2023 18:31Относно коментара за .join() в plugboard функцията. Използвах го понеже в протичен случай ми връща сет. Тоест като обединя думата със сета ми дава проблем. Поразрових се из интеренет и открих, че .difference е със същия смисъл като това, което аз бях използвала и го замених с него и понеже то е само с 1 елемент вътре взимам нулевия, който е стринг и нямам проблем с конкатенирането. Не знам доколко е оптимално, но в случая работи.
Благодаря за идеята с .get() метода. Наистина е доста по-удобно.
|
| f | 1 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | f | 1 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] |
| 2 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | 2 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||
| 3 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | 3 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||
| 4 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | 4 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||
| 5 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | 5 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||
| 6 | 'm': 'r', 'c': 'k'} | 6 | 'm': 'r', 'c': 'k'} | ||
| 7 | 7 | ||||
| 8 | 8 | ||||
| 9 | def plugboard(text, list_of_rules): | 9 | def plugboard(text, list_of_rules): | ||
| 10 | changed_word = "" | 10 | changed_word = "" | ||
| 11 | 11 | ||||
| 12 | for letter in text: | 12 | for letter in text: | ||
| 13 | for set_of_letters in list_of_rules: | 13 | for set_of_letters in list_of_rules: | ||
| 14 | if letter in set_of_letters: | 14 | if letter in set_of_letters: | ||
| t | 15 | letter = list(set_of_letters.difference({letter}))[0] | t | 15 | letter = set_of_letters.difference({letter}).pop() |
| 16 | 16 | ||||
| 17 | changed_word += letter | 17 | changed_word += letter | ||
| 18 | 18 | ||||
| 19 | return changed_word | 19 | return changed_word | ||
| 20 | 20 | ||||
| 21 | def rotor(text, dict_of_rules): | 21 | def rotor(text, dict_of_rules): | ||
| 22 | changed_text = "" | 22 | changed_text = "" | ||
| 23 | 23 | ||||
| 24 | for letter in text: | 24 | for letter in text: | ||
| 25 | changed_text += dict_of_rules.get(letter, " ") | 25 | changed_text += dict_of_rules.get(letter, " ") | ||
| 26 | 26 | ||||
| 27 | return changed_text | 27 | return changed_text | ||
| 28 | 28 | ||||
| 29 | def enigma_encrypt(plugboard_position, rotor_position): | 29 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 30 | def decorator(func): | 30 | def decorator(func): | ||
| 31 | def crypting(text): | 31 | def crypting(text): | ||
| 32 | crypted_text = plugboard(text, plugboard_position) | 32 | crypted_text = plugboard(text, plugboard_position) | ||
| 33 | crypted_text = rotor(crypted_text, rotor_position) | 33 | crypted_text = rotor(crypted_text, rotor_position) | ||
| 34 | return func(crypted_text) | 34 | return func(crypted_text) | ||
| 35 | return crypting | 35 | return crypting | ||
| 36 | return decorator | 36 | return decorator | ||
| 37 | 37 | ||||
| 38 | @enigma_encrypt(plugboard_position, rotor_position) | 38 | @enigma_encrypt(plugboard_position, rotor_position) | ||
| 39 | def encrypt_decorated(text): | 39 | def encrypt_decorated(text): | ||
| 40 | return text | 40 | return text | ||
| 41 | 41 | ||||
| 42 | def enigma_decrypt(plugboard_position, rotor_position): | 42 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 43 | def decorator(func): | 43 | def decorator(func): | ||
| 44 | def decrypting(text): | 44 | def decrypting(text): | ||
| 45 | rotor_position_reversed = {y: x for x, y in rotor_position.items()} | 45 | rotor_position_reversed = {y: x for x, y in rotor_position.items()} | ||
| 46 | decrypted_text = rotor(text, rotor_position_reversed) | 46 | decrypted_text = rotor(text, rotor_position_reversed) | ||
| 47 | decrypted_text = plugboard(decrypted_text, plugboard_position) | 47 | decrypted_text = plugboard(decrypted_text, plugboard_position) | ||
| 48 | 48 | ||||
| 49 | return func(decrypted_text) | 49 | return func(decrypted_text) | ||
| 50 | return decrypting | 50 | return decrypting | ||
| 51 | return decorator | 51 | return decorator | ||
| 52 | 52 | ||||
| 53 | @enigma_decrypt(plugboard_position, rotor_position) | 53 | @enigma_decrypt(plugboard_position, rotor_position) | ||
| 54 | def decrypt_decorated(text): | 54 | def decrypt_decorated(text): | ||
| 55 | return text | 55 | return text |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | f | 1 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] |
| 2 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | 2 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||
| 3 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | 3 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||
| 4 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | 4 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||
| 5 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | 5 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||
| 6 | 'm': 'r', 'c': 'k'} | 6 | 'm': 'r', 'c': 'k'} | ||
| 7 | 7 | ||||
| 8 | 8 | ||||
| 9 | def plugboard(text, list_of_rules): | 9 | def plugboard(text, list_of_rules): | ||
| 10 | changed_word = "" | 10 | changed_word = "" | ||
| 11 | 11 | ||||
| 12 | for letter in text: | 12 | for letter in text: | ||
| 13 | for set_of_letters in list_of_rules: | 13 | for set_of_letters in list_of_rules: | ||
| 14 | if letter in set_of_letters: | 14 | if letter in set_of_letters: | ||
| n | 15 | letter = ', '.join(set_of_letters - {letter}) | n | 15 | letter = list(set_of_letters.difference({letter}))[0] |
| 16 | 16 | ||||
| 17 | changed_word += letter | 17 | changed_word += letter | ||
| 18 | 18 | ||||
| 19 | return changed_word | 19 | return changed_word | ||
| 20 | 20 | ||||
| 21 | def rotor(text, dict_of_rules): | 21 | def rotor(text, dict_of_rules): | ||
| 22 | changed_text = "" | 22 | changed_text = "" | ||
| 23 | 23 | ||||
| 24 | for letter in text: | 24 | for letter in text: | ||
| n | 25 | if letter == " ": | n | ||
| 26 | changed_text += " " | ||||
| 27 | else: | ||||
| 28 | changed_text += dict_of_rules[letter] | 25 | changed_text += dict_of_rules.get(letter, " ") | ||
| 29 | 26 | ||||
| 30 | return changed_text | 27 | return changed_text | ||
| 31 | 28 | ||||
| n | 32 | def enigma_encrypt(plugboard_position , rotor_position ): | n | 29 | def enigma_encrypt(plugboard_position, rotor_position): |
| 33 | def decorator(func): | 30 | def decorator(func): | ||
| 34 | def crypting(text): | 31 | def crypting(text): | ||
| n | 35 | crypted_text = plugboard(text,plugboard_position) | n | 32 | crypted_text = plugboard(text, plugboard_position) |
| 36 | crypted_text = rotor(crypted_text, rotor_position) | 33 | crypted_text = rotor(crypted_text, rotor_position) | ||
| n | 37 | return func( crypted_text) | n | 34 | return func(crypted_text) |
| 38 | return crypting | 35 | return crypting | ||
| 39 | return decorator | 36 | return decorator | ||
| 40 | 37 | ||||
| n | 41 | @enigma_encrypt(plugboard_position , rotor_position ) | n | 38 | @enigma_encrypt(plugboard_position, rotor_position) |
| 42 | def encrypt_decorated(text): | 39 | def encrypt_decorated(text): | ||
| 43 | return text | 40 | return text | ||
| 44 | 41 | ||||
| n | 45 | def enigma_decrypt(plugboard_position , rotor_position ): | n | 42 | def enigma_decrypt(plugboard_position, rotor_position): |
| 46 | def decorator(func): | 43 | def decorator(func): | ||
| 47 | def decrypting(text): | 44 | def decrypting(text): | ||
| 48 | rotor_position_reversed = {y: x for x, y in rotor_position.items()} | 45 | rotor_position_reversed = {y: x for x, y in rotor_position.items()} | ||
| 49 | decrypted_text = rotor(text, rotor_position_reversed) | 46 | decrypted_text = rotor(text, rotor_position_reversed) | ||
| n | 50 | decrypted_text = plugboard(decrypted_text,plugboard_position) | n | 47 | decrypted_text = plugboard(decrypted_text, plugboard_position) |
| 51 | 48 | ||||
| n | 52 | return func( decrypted_text) | n | 49 | return func(decrypted_text) |
| 53 | return decrypting | 50 | return decrypting | ||
| 54 | return decorator | 51 | return decorator | ||
| 55 | 52 | ||||
| t | 56 | @enigma_decrypt(plugboard_position , rotor_position ) | t | 53 | @enigma_decrypt(plugboard_position, rotor_position) |
| 57 | def decrypt_decorated(text): | 54 | def decrypt_decorated(text): | ||
| 58 | return text | 55 | return text |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | f | 1 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] |
| 2 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | 2 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||
| 3 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | 3 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||
| 4 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | 4 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||
| 5 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | 5 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||
| 6 | 'm': 'r', 'c': 'k'} | 6 | 'm': 'r', 'c': 'k'} | ||
| 7 | 7 | ||||
| 8 | 8 | ||||
| 9 | def plugboard(text, list_of_rules): | 9 | def plugboard(text, list_of_rules): | ||
| 10 | changed_word = "" | 10 | changed_word = "" | ||
| 11 | 11 | ||||
| 12 | for letter in text: | 12 | for letter in text: | ||
| 13 | for set_of_letters in list_of_rules: | 13 | for set_of_letters in list_of_rules: | ||
| 14 | if letter in set_of_letters: | 14 | if letter in set_of_letters: | ||
| 15 | letter = ', '.join(set_of_letters - {letter}) | 15 | letter = ', '.join(set_of_letters - {letter}) | ||
| 16 | 16 | ||||
| 17 | changed_word += letter | 17 | changed_word += letter | ||
| 18 | 18 | ||||
| 19 | return changed_word | 19 | return changed_word | ||
| 20 | 20 | ||||
| 21 | def rotor(text, dict_of_rules): | 21 | def rotor(text, dict_of_rules): | ||
| 22 | changed_text = "" | 22 | changed_text = "" | ||
| 23 | 23 | ||||
| 24 | for letter in text: | 24 | for letter in text: | ||
| t | t | 25 | if letter == " ": | ||
| 26 | changed_text += " " | ||||
| 27 | else: | ||||
| 25 | changed_text += dict_of_rules[letter] | 28 | changed_text += dict_of_rules[letter] | ||
| 26 | 29 | ||||
| 27 | return changed_text | 30 | return changed_text | ||
| 28 | 31 | ||||
| 29 | def enigma_encrypt(plugboard_position , rotor_position ): | 32 | def enigma_encrypt(plugboard_position , rotor_position ): | ||
| 30 | def decorator(func): | 33 | def decorator(func): | ||
| 31 | def crypting(text): | 34 | def crypting(text): | ||
| 32 | crypted_text = plugboard(text,plugboard_position) | 35 | crypted_text = plugboard(text,plugboard_position) | ||
| 33 | crypted_text = rotor(crypted_text, rotor_position) | 36 | crypted_text = rotor(crypted_text, rotor_position) | ||
| 34 | return func( crypted_text) | 37 | return func( crypted_text) | ||
| 35 | return crypting | 38 | return crypting | ||
| 36 | return decorator | 39 | return decorator | ||
| 37 | 40 | ||||
| 38 | @enigma_encrypt(plugboard_position , rotor_position ) | 41 | @enigma_encrypt(plugboard_position , rotor_position ) | ||
| 39 | def encrypt_decorated(text): | 42 | def encrypt_decorated(text): | ||
| 40 | return text | 43 | return text | ||
| 41 | 44 | ||||
| 42 | def enigma_decrypt(plugboard_position , rotor_position ): | 45 | def enigma_decrypt(plugboard_position , rotor_position ): | ||
| 43 | def decorator(func): | 46 | def decorator(func): | ||
| 44 | def decrypting(text): | 47 | def decrypting(text): | ||
| 45 | rotor_position_reversed = {y: x for x, y in rotor_position.items()} | 48 | rotor_position_reversed = {y: x for x, y in rotor_position.items()} | ||
| 46 | decrypted_text = rotor(text, rotor_position_reversed) | 49 | decrypted_text = rotor(text, rotor_position_reversed) | ||
| 47 | decrypted_text = plugboard(decrypted_text,plugboard_position) | 50 | decrypted_text = plugboard(decrypted_text,plugboard_position) | ||
| 48 | 51 | ||||
| 49 | return func( decrypted_text) | 52 | return func( decrypted_text) | ||
| 50 | return decrypting | 53 | return decrypting | ||
| 51 | return decorator | 54 | return decorator | ||
| 52 | 55 | ||||
| 53 | @enigma_decrypt(plugboard_position , rotor_position ) | 56 | @enigma_decrypt(plugboard_position , rotor_position ) | ||
| 54 | def decrypt_decorated(text): | 57 | def decrypt_decorated(text): | ||
| 55 | return text | 58 | return text |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | f | 1 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] |
| 2 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | 2 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||
| 3 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | 3 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||
| 4 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | 4 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||
| 5 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | 5 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||
| 6 | 'm': 'r', 'c': 'k'} | 6 | 'm': 'r', 'c': 'k'} | ||
| 7 | 7 | ||||
| 8 | 8 | ||||
| 9 | def plugboard(text, list_of_rules): | 9 | def plugboard(text, list_of_rules): | ||
| n | 10 | changed_word="" | n | 10 | changed_word = "" |
| 11 | 11 | ||||
| 12 | for letter in text: | 12 | for letter in text: | ||
| 13 | for set_of_letters in list_of_rules: | 13 | for set_of_letters in list_of_rules: | ||
| 14 | if letter in set_of_letters: | 14 | if letter in set_of_letters: | ||
| 15 | letter = ', '.join(set_of_letters - {letter}) | 15 | letter = ', '.join(set_of_letters - {letter}) | ||
| 16 | 16 | ||||
| 17 | changed_word += letter | 17 | changed_word += letter | ||
| 18 | 18 | ||||
| 19 | return changed_word | 19 | return changed_word | ||
| 20 | 20 | ||||
| 21 | def rotor(text, dict_of_rules): | 21 | def rotor(text, dict_of_rules): | ||
| 22 | changed_text = "" | 22 | changed_text = "" | ||
| 23 | 23 | ||||
| 24 | for letter in text: | 24 | for letter in text: | ||
| 25 | changed_text += dict_of_rules[letter] | 25 | changed_text += dict_of_rules[letter] | ||
| 26 | 26 | ||||
| 27 | return changed_text | 27 | return changed_text | ||
| 28 | 28 | ||||
| 29 | def enigma_encrypt(plugboard_position , rotor_position ): | 29 | def enigma_encrypt(plugboard_position , rotor_position ): | ||
| 30 | def decorator(func): | 30 | def decorator(func): | ||
| 31 | def crypting(text): | 31 | def crypting(text): | ||
| 32 | crypted_text = plugboard(text,plugboard_position) | 32 | crypted_text = plugboard(text,plugboard_position) | ||
| 33 | crypted_text = rotor(crypted_text, rotor_position) | 33 | crypted_text = rotor(crypted_text, rotor_position) | ||
| 34 | return func( crypted_text) | 34 | return func( crypted_text) | ||
| 35 | return crypting | 35 | return crypting | ||
| 36 | return decorator | 36 | return decorator | ||
| 37 | 37 | ||||
| n | 38 | n | |||
| 39 | @enigma_encrypt(plugboard_position , rotor_position ) | 38 | @enigma_encrypt(plugboard_position , rotor_position ) | ||
| 40 | def encrypt_decorated(text): | 39 | def encrypt_decorated(text): | ||
| 41 | return text | 40 | return text | ||
| 42 | 41 | ||||
| 43 | def enigma_decrypt(plugboard_position , rotor_position ): | 42 | def enigma_decrypt(plugboard_position , rotor_position ): | ||
| 44 | def decorator(func): | 43 | def decorator(func): | ||
| 45 | def decrypting(text): | 44 | def decrypting(text): | ||
| 46 | rotor_position_reversed = {y: x for x, y in rotor_position.items()} | 45 | rotor_position_reversed = {y: x for x, y in rotor_position.items()} | ||
| 47 | decrypted_text = rotor(text, rotor_position_reversed) | 46 | decrypted_text = rotor(text, rotor_position_reversed) | ||
| 48 | decrypted_text = plugboard(decrypted_text,plugboard_position) | 47 | decrypted_text = plugboard(decrypted_text,plugboard_position) | ||
| 49 | 48 | ||||
| 50 | return func( decrypted_text) | 49 | return func( decrypted_text) | ||
| 51 | return decrypting | 50 | return decrypting | ||
| 52 | return decorator | 51 | return decorator | ||
| 53 | 52 | ||||
| t | 54 | t | |||
| 55 | @enigma_decrypt(plugboard_position , rotor_position ) | 53 | @enigma_decrypt(plugboard_position , rotor_position ) | ||
| 56 | def decrypt_decorated(text): | 54 | def decrypt_decorated(text): | ||
| 57 | return text | 55 | return text |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| t | t | 1 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | ||
| 2 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||||
| 3 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||||
| 4 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||||
| 5 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||||
| 6 | 'm': 'r', 'c': 'k'} | ||||
| 7 | |||||
| 8 | |||||
| 1 | def plugboard(text, list_of_rules): | 9 | def plugboard(text, list_of_rules): | ||
| 2 | changed_word="" | 10 | changed_word="" | ||
| 3 | 11 | ||||
| 4 | for letter in text: | 12 | for letter in text: | ||
| 5 | for set_of_letters in list_of_rules: | 13 | for set_of_letters in list_of_rules: | ||
| 6 | if letter in set_of_letters: | 14 | if letter in set_of_letters: | ||
| 7 | letter = ', '.join(set_of_letters - {letter}) | 15 | letter = ', '.join(set_of_letters - {letter}) | ||
| 8 | 16 | ||||
| 9 | changed_word += letter | 17 | changed_word += letter | ||
| 10 | 18 | ||||
| 11 | return changed_word | 19 | return changed_word | ||
| 12 | 20 | ||||
| 13 | def rotor(text, dict_of_rules): | 21 | def rotor(text, dict_of_rules): | ||
| 14 | changed_text = "" | 22 | changed_text = "" | ||
| 15 | 23 | ||||
| 16 | for letter in text: | 24 | for letter in text: | ||
| 17 | changed_text += dict_of_rules[letter] | 25 | changed_text += dict_of_rules[letter] | ||
| 18 | 26 | ||||
| 19 | return changed_text | 27 | return changed_text | ||
| 20 | 28 | ||||
| 21 | def enigma_encrypt(plugboard_position , rotor_position ): | 29 | def enigma_encrypt(plugboard_position , rotor_position ): | ||
| 22 | def decorator(func): | 30 | def decorator(func): | ||
| 23 | def crypting(text): | 31 | def crypting(text): | ||
| 24 | crypted_text = plugboard(text,plugboard_position) | 32 | crypted_text = plugboard(text,plugboard_position) | ||
| 25 | crypted_text = rotor(crypted_text, rotor_position) | 33 | crypted_text = rotor(crypted_text, rotor_position) | ||
| 26 | return func( crypted_text) | 34 | return func( crypted_text) | ||
| 27 | return crypting | 35 | return crypting | ||
| 28 | return decorator | 36 | return decorator | ||
| 29 | 37 | ||||
| 30 | 38 | ||||
| 31 | @enigma_encrypt(plugboard_position , rotor_position ) | 39 | @enigma_encrypt(plugboard_position , rotor_position ) | ||
| 32 | def encrypt_decorated(text): | 40 | def encrypt_decorated(text): | ||
| 33 | return text | 41 | return text | ||
| 34 | 42 | ||||
| 35 | def enigma_decrypt(plugboard_position , rotor_position ): | 43 | def enigma_decrypt(plugboard_position , rotor_position ): | ||
| 36 | def decorator(func): | 44 | def decorator(func): | ||
| 37 | def decrypting(text): | 45 | def decrypting(text): | ||
| 38 | rotor_position_reversed = {y: x for x, y in rotor_position.items()} | 46 | rotor_position_reversed = {y: x for x, y in rotor_position.items()} | ||
| 39 | decrypted_text = rotor(text, rotor_position_reversed) | 47 | decrypted_text = rotor(text, rotor_position_reversed) | ||
| 40 | decrypted_text = plugboard(decrypted_text,plugboard_position) | 48 | decrypted_text = plugboard(decrypted_text,plugboard_position) | ||
| 41 | 49 | ||||
| 42 | return func( decrypted_text) | 50 | return func( decrypted_text) | ||
| 43 | return decrypting | 51 | return decrypting | ||
| 44 | return decorator | 52 | return decorator | ||
| 45 | 53 | ||||
| 46 | 54 | ||||
| 47 | @enigma_decrypt(plugboard_position , rotor_position ) | 55 | @enigma_decrypt(plugboard_position , rotor_position ) | ||
| 48 | def decrypt_decorated(text): | 56 | def decrypt_decorated(text): | ||
| 49 | return text | 57 | return text |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||