1"""
2Solution to homework 2 for Introduction to Python course at FMI
3by Nikolay Nikolaev.
4"""
5
6plugboard_pos = [{'a', 'c'}, {'t', 'z'}]
7rotor_pos = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',
8 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',
9 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',
10 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',
11 'm': 'r', 'c': 'k'}
12
13
14def plugboard(text, plugboard_position):
15 """
16 Changes every occurence of a letter with its plugboard_position pair,
17 using plugboard_position's values parsed into a helper dict.
18 """
19 new_text = ""
20 plugboard_dict = {}
21
22 for set_entity in plugboard_position:
23 set_entity = list(set_entity)
24 plugboard_dict[set_entity[0]] = set_entity[1]
25 plugboard_dict[set_entity[1]] = set_entity[0]
26
27 for letter in text:
28 new_text = new_text + plugboard_dict.get(letter, letter)
29 return new_text
30
31
32def rotor(text, rotor_position):
33 """Changes every occurence of a letter with its rotor_position value."""
34 new_text = ""
35 for letter in text:
36 new_text = new_text + rotor_position.get(letter, letter)
37 return new_text
38
39
40def enigma_encrypt(plugboard_position=plugboard_pos, rotor_position=rotor_pos):
41 """
42 Encrypts the text, given to the decorated function,
43 using the given list and dict.
44 """
45 def encrypt_decorator(func):
46 def wrapper(text):
47 new_text = plugboard(text, plugboard_position)
48 new_text = rotor(new_text, rotor_position)
49 return func(new_text)
50 return wrapper
51 return encrypt_decorator
52
53
54def enigma_decrypt(plugboard_position = plugboard_pos, rotor_position = rotor_pos):
55 """
56 Decrypts the text, given to the decorated function,
57 using the given list and dict."""
58 def decrypt_decorator(func):
59 def wrapper(text):
60 swapped_dict = {value: key for key, value in rotor_position.items()}
61 new_text = rotor(text, swapped_dict)
62 new_text = plugboard(new_text, plugboard_position)
63 return func(new_text)
64 return wrapper
65 return decrypt_decorator
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
| f | 1 | """ | f | 1 | """ |
| 2 | Solution to homework 2 for Introduction to Python course at FMI | 2 | Solution to homework 2 for Introduction to Python course at FMI | ||
| 3 | by Nikolay Nikolaev. | 3 | by Nikolay Nikolaev. | ||
| 4 | """ | 4 | """ | ||
| 5 | 5 | ||||
| 6 | plugboard_pos = [{'a', 'c'}, {'t', 'z'}] | 6 | plugboard_pos = [{'a', 'c'}, {'t', 'z'}] | ||
| 7 | rotor_pos = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | 7 | rotor_pos = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||
| 8 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | 8 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||
| 9 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | 9 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||
| 10 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | 10 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||
| 11 | 'm': 'r', 'c': 'k'} | 11 | 'm': 'r', 'c': 'k'} | ||
| 12 | 12 | ||||
| 13 | 13 | ||||
| 14 | def plugboard(text, plugboard_position): | 14 | def plugboard(text, plugboard_position): | ||
| 15 | """ | 15 | """ | ||
| 16 | Changes every occurence of a letter with its plugboard_position pair, | 16 | Changes every occurence of a letter with its plugboard_position pair, | ||
| 17 | using plugboard_position's values parsed into a helper dict. | 17 | using plugboard_position's values parsed into a helper dict. | ||
| 18 | """ | 18 | """ | ||
| 19 | new_text = "" | 19 | new_text = "" | ||
| 20 | plugboard_dict = {} | 20 | plugboard_dict = {} | ||
| 21 | 21 | ||||
| 22 | for set_entity in plugboard_position: | 22 | for set_entity in plugboard_position: | ||
| 23 | set_entity = list(set_entity) | 23 | set_entity = list(set_entity) | ||
| 24 | plugboard_dict[set_entity[0]] = set_entity[1] | 24 | plugboard_dict[set_entity[0]] = set_entity[1] | ||
| 25 | plugboard_dict[set_entity[1]] = set_entity[0] | 25 | plugboard_dict[set_entity[1]] = set_entity[0] | ||
| t | 26 | t | 26 | ||
| 27 | print(plugboard_dict) | ||||
| 28 | for letter in text: | 27 | for letter in text: | ||
| 29 | new_text = new_text + plugboard_dict.get(letter, letter) | 28 | new_text = new_text + plugboard_dict.get(letter, letter) | ||
| 30 | return new_text | 29 | return new_text | ||
| 31 | 30 | ||||
| 32 | 31 | ||||
| 33 | def rotor(text, rotor_position): | 32 | def rotor(text, rotor_position): | ||
| 34 | """Changes every occurence of a letter with its rotor_position value.""" | 33 | """Changes every occurence of a letter with its rotor_position value.""" | ||
| 35 | new_text = "" | 34 | new_text = "" | ||
| 36 | for letter in text: | 35 | for letter in text: | ||
| 37 | new_text = new_text + rotor_position.get(letter, letter) | 36 | new_text = new_text + rotor_position.get(letter, letter) | ||
| 38 | return new_text | 37 | return new_text | ||
| 39 | 38 | ||||
| 40 | 39 | ||||
| 41 | def enigma_encrypt(plugboard_position=plugboard_pos, rotor_position=rotor_pos): | 40 | def enigma_encrypt(plugboard_position=plugboard_pos, rotor_position=rotor_pos): | ||
| 42 | """ | 41 | """ | ||
| 43 | Encrypts the text, given to the decorated function, | 42 | Encrypts the text, given to the decorated function, | ||
| 44 | using the given list and dict. | 43 | using the given list and dict. | ||
| 45 | """ | 44 | """ | ||
| 46 | def encrypt_decorator(func): | 45 | def encrypt_decorator(func): | ||
| 47 | def wrapper(text): | 46 | def wrapper(text): | ||
| 48 | new_text = plugboard(text, plugboard_position) | 47 | new_text = plugboard(text, plugboard_position) | ||
| 49 | new_text = rotor(new_text, rotor_position) | 48 | new_text = rotor(new_text, rotor_position) | ||
| 50 | return func(new_text) | 49 | return func(new_text) | ||
| 51 | return wrapper | 50 | return wrapper | ||
| 52 | return encrypt_decorator | 51 | return encrypt_decorator | ||
| 53 | 52 | ||||
| 54 | 53 | ||||
| 55 | def enigma_decrypt(plugboard_position = plugboard_pos, rotor_position = rotor_pos): | 54 | def enigma_decrypt(plugboard_position = plugboard_pos, rotor_position = rotor_pos): | ||
| 56 | """ | 55 | """ | ||
| 57 | Decrypts the text, given to the decorated function, | 56 | Decrypts the text, given to the decorated function, | ||
| 58 | using the given list and dict.""" | 57 | using the given list and dict.""" | ||
| 59 | def decrypt_decorator(func): | 58 | def decrypt_decorator(func): | ||
| 60 | def wrapper(text): | 59 | def wrapper(text): | ||
| 61 | swapped_dict = {value: key for key, value in rotor_position.items()} | 60 | swapped_dict = {value: key for key, value in rotor_position.items()} | ||
| 62 | new_text = rotor(text, swapped_dict) | 61 | new_text = rotor(text, swapped_dict) | ||
| 63 | new_text = plugboard(new_text, plugboard_position) | 62 | new_text = plugboard(new_text, plugboard_position) | ||
| 64 | return func(new_text) | 63 | return func(new_text) | ||
| 65 | return wrapper | 64 | return wrapper | ||
| 66 | return decrypt_decorator | 65 | return decrypt_decorator |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | """ | f | 1 | """ |
| 2 | Solution to homework 2 for Introduction to Python course at FMI | 2 | Solution to homework 2 for Introduction to Python course at FMI | ||
| 3 | by Nikolay Nikolaev. | 3 | by Nikolay Nikolaev. | ||
| 4 | """ | 4 | """ | ||
| 5 | 5 | ||||
| 6 | plugboard_pos = [{'a', 'c'}, {'t', 'z'}] | 6 | plugboard_pos = [{'a', 'c'}, {'t', 'z'}] | ||
| 7 | rotor_pos = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | 7 | rotor_pos = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||
| 8 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | 8 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||
| 9 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | 9 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||
| 10 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | 10 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||
| 11 | 'm': 'r', 'c': 'k'} | 11 | 'm': 'r', 'c': 'k'} | ||
| 12 | 12 | ||||
| 13 | 13 | ||||
| 14 | def plugboard(text, plugboard_position): | 14 | def plugboard(text, plugboard_position): | ||
| n | n | 15 | """ | ||
| 15 | """Changes every occurence of a letter with its plugboard_position pair.""" | 16 | Changes every occurence of a letter with its plugboard_position pair, | ||
| 17 | using plugboard_position's values parsed into a helper dict. | ||||
| 18 | """ | ||||
| 16 | new_text = "" | 19 | new_text = "" | ||
| n | 17 | for text_index, letter in enumerate(text): | n | 20 | plugboard_dict = {} |
| 21 | |||||
| 18 | for set_index, set_entity in enumerate(plugboard_position): | 22 | for set_entity in plugboard_position: | ||
| 19 | if letter in set_entity: | 23 | set_entity = list(set_entity) | ||
| 20 | for item in set_entity: | 24 | plugboard_dict[set_entity[0]] = set_entity[1] | ||
| 21 | if item != letter: | 25 | plugboard_dict[set_entity[1]] = set_entity[0] | ||
| 22 | new_text = new_text + item | 26 | |||
| 23 | break | 27 | print(plugboard_dict) | ||
| 24 | elif (set_index + 1 == len(plugboard_position) and | 28 | for letter in text: | ||
| 25 | text_index + 1 > len(new_text)): | 29 | new_text = new_text + plugboard_dict.get(letter, letter) | ||
| 26 | new_text = new_text + letter | ||||
| 27 | return new_text | 30 | return new_text | ||
| 28 | 31 | ||||
| 29 | 32 | ||||
| 30 | def rotor(text, rotor_position): | 33 | def rotor(text, rotor_position): | ||
| 31 | """Changes every occurence of a letter with its rotor_position value.""" | 34 | """Changes every occurence of a letter with its rotor_position value.""" | ||
| 32 | new_text = "" | 35 | new_text = "" | ||
| 33 | for letter in text: | 36 | for letter in text: | ||
| n | 34 | if letter in rotor_position: | n | ||
| 35 | new_text = new_text + rotor_position[letter] | 37 | new_text = new_text + rotor_position.get(letter, letter) | ||
| 36 | else: | ||||
| 37 | new_text = new_text + letter | ||||
| 38 | return new_text | 38 | return new_text | ||
| 39 | 39 | ||||
| 40 | 40 | ||||
| t | 41 | def enigma_encrypt(plugboard_position = plugboard_pos, rotor_position = rotor_pos): | t | 41 | def enigma_encrypt(plugboard_position=plugboard_pos, rotor_position=rotor_pos): |
| 42 | """ | 42 | """ | ||
| 43 | Encrypts the text, given to the decorated function, | 43 | Encrypts the text, given to the decorated function, | ||
| 44 | using the given list and dict. | 44 | using the given list and dict. | ||
| 45 | """ | 45 | """ | ||
| 46 | def encrypt_decorator(func): | 46 | def encrypt_decorator(func): | ||
| 47 | def wrapper(text): | 47 | def wrapper(text): | ||
| 48 | new_text = plugboard(text, plugboard_position) | 48 | new_text = plugboard(text, plugboard_position) | ||
| 49 | new_text = rotor(new_text, rotor_position) | 49 | new_text = rotor(new_text, rotor_position) | ||
| 50 | return func(new_text) | 50 | return func(new_text) | ||
| 51 | return wrapper | 51 | return wrapper | ||
| 52 | return encrypt_decorator | 52 | return encrypt_decorator | ||
| 53 | 53 | ||||
| 54 | 54 | ||||
| 55 | def enigma_decrypt(plugboard_position = plugboard_pos, rotor_position = rotor_pos): | 55 | def enigma_decrypt(plugboard_position = plugboard_pos, rotor_position = rotor_pos): | ||
| 56 | """ | 56 | """ | ||
| 57 | Decrypts the text, given to the decorated function, | 57 | Decrypts the text, given to the decorated function, | ||
| 58 | using the given list and dict.""" | 58 | using the given list and dict.""" | ||
| 59 | def decrypt_decorator(func): | 59 | def decrypt_decorator(func): | ||
| 60 | def wrapper(text): | 60 | def wrapper(text): | ||
| 61 | swapped_dict = {value: key for key, value in rotor_position.items()} | 61 | swapped_dict = {value: key for key, value in rotor_position.items()} | ||
| 62 | new_text = rotor(text, swapped_dict) | 62 | new_text = rotor(text, swapped_dict) | ||
| 63 | new_text = plugboard(new_text, plugboard_position) | 63 | new_text = plugboard(new_text, plugboard_position) | ||
| 64 | return func(new_text) | 64 | return func(new_text) | ||
| 65 | return wrapper | 65 | return wrapper | ||
| 66 | return decrypt_decorator | 66 | return decrypt_decorator |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||