1def plugboard(input_str, current_plugboard):
2 new_str = []
3 for letter in input_str:
4 for plugboard_set in current_plugboard:
5 if letter in plugboard_set:
6 # gets the letter from the set which is not the current letter and turns it into char
7 new_letter = plugboard_set.difference({letter}).pop()
8 new_str.append(new_letter)
9 break
10 else:
11 new_str.append(letter)
12 return ''.join(new_str)
13
14
15def rotor(input_str, current_rotor):
16 new_str = []
17 for letter in input_str:
18 new_letter = current_rotor.get(letter, ' ')
19 new_str.append(new_letter)
20 return ''.join(new_str)
21
22
23def enigma_encrypt(plugboard_position, rotor_position):
24 def decorator(my_function):
25 def func(input_str):
26 new_str = plugboard(input_str, plugboard_position)
27 new_str = rotor(new_str, rotor_position)
28 return my_function(new_str)
29 return func
30 return decorator
31
32
33def enigma_decrypt(plugboard_position, rotor_position):
34 def decorator(my_function):
35 def func(input_str):
36 # swapping the key and the value in the current rotor
37 new_rotor_position = dict([(value, key) for key, value in rotor_position.items()])
38 new_str = rotor(input_str, new_rotor_position)
39 new_str = plugboard(new_str, plugboard_position)
40 return my_function(new_str)
41 return func
42 return decorator
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
Георги Кунчев
31.10.2023 09:28Оставих коментари, с които да си поопростиш кода. Като цяло решението е добро, но се опитай да даваш по-добри имена на променливите си. `my_str` е просто начин да избегнеш запазеното име `str`. По принцип в Пайтън има конвенция за избягване на запазено име. Добавяш долна черта в края, т.е. `str_`, `list_`, НО това не ги прави по-добри имена. Слагай имена, които наистина казват какви са тези данни, а не само техния тип.
|
| n | 1 | def plugboard(my_str, my_list): | n | 1 | def plugboard(input_str, current_plugboard): |
| 2 | new_str = [] | 2 | new_str = [] | ||
| n | 3 | for letter in my_str: | n | 3 | for letter in input_str: |
| 4 | if letter == ' ': | 4 | for plugboard_set in current_plugboard: | ||
| 5 | if letter in plugboard_set: | ||||
| 6 | # gets the letter from the set which is not the current letter and turns it into char | ||||
| 7 | new_letter = plugboard_set.difference({letter}).pop() | ||||
| 5 | new_str.append(' ') | 8 | new_str.append(new_letter) | ||
| 9 | break | ||||
| 6 | else: | 10 | else: | ||
| n | 7 | wanted_letter = False | n | ||
| 8 | for my_set in my_list: | ||||
| 9 | if letter in my_set: | ||||
| 10 | # gets the letter from the set which is not the current letter and turns it into char | ||||
| 11 | new_letter = my_set.difference({letter}).pop() | ||||
| 12 | new_str.append(new_letter) | ||||
| 13 | wanted_letter = True | ||||
| 14 | if wanted_letter == False: | ||||
| 15 | new_str.append(letter) | 11 | new_str.append(letter) | ||
| 16 | return ''.join(new_str) | 12 | return ''.join(new_str) | ||
| 17 | 13 | ||||
| 18 | 14 | ||||
| n | 19 | def rotor(my_str, my_dict): | n | 15 | def rotor(input_str, current_rotor): |
| 20 | new_str = [] | 16 | new_str = [] | ||
| n | 21 | for letter in my_str: | n | 17 | for letter in input_str: |
| 22 | if letter == ' ': | 18 | new_letter = current_rotor.get(letter, ' ') | ||
| 23 | new_str.append(' ') | ||||
| 24 | else: | ||||
| 25 | new_letter = my_dict[letter] | ||||
| 26 | new_str.append(new_letter) | 19 | new_str.append(new_letter) | ||
| 27 | return ''.join(new_str) | 20 | return ''.join(new_str) | ||
| 28 | 21 | ||||
| 29 | 22 | ||||
| 30 | def enigma_encrypt(plugboard_position, rotor_position): | 23 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 31 | def decorator(my_function): | 24 | def decorator(my_function): | ||
| n | 32 | def func(my_str): | n | 25 | def func(input_str): |
| 33 | new_str = plugboard(my_str, plugboard_position) | 26 | new_str = plugboard(input_str, plugboard_position) | ||
| 34 | new_str = rotor(new_str, rotor_position) | 27 | new_str = rotor(new_str, rotor_position) | ||
| 35 | return my_function(new_str) | 28 | return my_function(new_str) | ||
| 36 | return func | 29 | return func | ||
| 37 | return decorator | 30 | return decorator | ||
| 38 | 31 | ||||
| 39 | 32 | ||||
| 40 | def enigma_decrypt(plugboard_position, rotor_position): | 33 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 41 | def decorator(my_function): | 34 | def decorator(my_function): | ||
| n | 42 | def func(my_str): | n | 35 | def func(input_str): |
| 43 | # swapping the key and the value in the current rotor | 36 | # swapping the key and the value in the current rotor | ||
| 44 | new_rotor_position = dict([(value, key) for key, value in rotor_position.items()]) | 37 | new_rotor_position = dict([(value, key) for key, value in rotor_position.items()]) | ||
| t | 45 | new_str = rotor(my_str, new_rotor_position) | t | 38 | new_str = rotor(input_str, new_rotor_position) |
| 46 | new_str = plugboard(new_str, plugboard_position) | 39 | new_str = plugboard(new_str, plugboard_position) | ||
| 47 | return my_function(new_str) | 40 | return my_function(new_str) | ||
| 48 | return func | 41 | return func | ||
| 49 | return decorator | 42 | return decorator |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| t | 1 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | t | ||
| 2 | |||||
| 3 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||||
| 4 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||||
| 5 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||||
| 6 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||||
| 7 | 'm': 'r', 'c': 'k'} | ||||
| 8 | |||||
| 9 | |||||
| 10 | def plugboard(my_str, my_list): | 1 | def plugboard(my_str, my_list): | ||
| 11 | new_str = [] | 2 | new_str = [] | ||
| 12 | for letter in my_str: | 3 | for letter in my_str: | ||
| 13 | if letter == ' ': | 4 | if letter == ' ': | ||
| 14 | new_str.append(' ') | 5 | new_str.append(' ') | ||
| 15 | else: | 6 | else: | ||
| 16 | wanted_letter = False | 7 | wanted_letter = False | ||
| 17 | for my_set in my_list: | 8 | for my_set in my_list: | ||
| 18 | if letter in my_set: | 9 | if letter in my_set: | ||
| 19 | # gets the letter from the set which is not the current letter and turns it into char | 10 | # gets the letter from the set which is not the current letter and turns it into char | ||
| 20 | new_letter = my_set.difference({letter}).pop() | 11 | new_letter = my_set.difference({letter}).pop() | ||
| 21 | new_str.append(new_letter) | 12 | new_str.append(new_letter) | ||
| 22 | wanted_letter = True | 13 | wanted_letter = True | ||
| 23 | if wanted_letter == False: | 14 | if wanted_letter == False: | ||
| 24 | new_str.append(letter) | 15 | new_str.append(letter) | ||
| 25 | return ''.join(new_str) | 16 | return ''.join(new_str) | ||
| 26 | 17 | ||||
| 27 | 18 | ||||
| 28 | def rotor(my_str, my_dict): | 19 | def rotor(my_str, my_dict): | ||
| 29 | new_str = [] | 20 | new_str = [] | ||
| 30 | for letter in my_str: | 21 | for letter in my_str: | ||
| 31 | if letter == ' ': | 22 | if letter == ' ': | ||
| 32 | new_str.append(' ') | 23 | new_str.append(' ') | ||
| 33 | else: | 24 | else: | ||
| 34 | new_letter = my_dict[letter] | 25 | new_letter = my_dict[letter] | ||
| 35 | new_str.append(new_letter) | 26 | new_str.append(new_letter) | ||
| 36 | return ''.join(new_str) | 27 | return ''.join(new_str) | ||
| 37 | 28 | ||||
| 38 | 29 | ||||
| 39 | def enigma_encrypt(plugboard_position, rotor_position): | 30 | def enigma_encrypt(plugboard_position, rotor_position): | ||
| 40 | def decorator(my_function): | 31 | def decorator(my_function): | ||
| 41 | def func(my_str): | 32 | def func(my_str): | ||
| 42 | new_str = plugboard(my_str, plugboard_position) | 33 | new_str = plugboard(my_str, plugboard_position) | ||
| 43 | new_str = rotor(new_str, rotor_position) | 34 | new_str = rotor(new_str, rotor_position) | ||
| 44 | return my_function(new_str) | 35 | return my_function(new_str) | ||
| 45 | return func | 36 | return func | ||
| 46 | return decorator | 37 | return decorator | ||
| 47 | 38 | ||||
| 48 | 39 | ||||
| 49 | def enigma_decrypt(plugboard_position, rotor_position): | 40 | def enigma_decrypt(plugboard_position, rotor_position): | ||
| 50 | def decorator(my_function): | 41 | def decorator(my_function): | ||
| 51 | def func(my_str): | 42 | def func(my_str): | ||
| 52 | # swapping the key and the value in the current rotor | 43 | # swapping the key and the value in the current rotor | ||
| 53 | new_rotor_position = dict([(value, key) for key, value in rotor_position.items()]) | 44 | new_rotor_position = dict([(value, key) for key, value in rotor_position.items()]) | ||
| 54 | new_str = rotor(my_str, new_rotor_position) | 45 | new_str = rotor(my_str, new_rotor_position) | ||
| 55 | new_str = plugboard(new_str, plugboard_position) | 46 | new_str = plugboard(new_str, plugboard_position) | ||
| 56 | return my_function(new_str) | 47 | return my_function(new_str) | ||
| 57 | return func | 48 | return func | ||
| 58 | return decorator | 49 | return decorator |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||