f | | f | |
| def plugboard(text: str, plugboard_positions: list): | | def plugboard(text: str, plugboard_positions: list): |
n | """Puts the input through the plugboard""" | n | """Puts the input through the plugboard.""" |
| new_text = str() | | new_text = '' |
| for letter in text: | | for letter in text: |
| for plugboard_position in plugboard_positions: | | for plugboard_position in plugboard_positions: |
| plugboard_position_copy = plugboard_position.copy() | | plugboard_position_copy = plugboard_position.copy() |
| if letter in plugboard_position_copy: | | if letter in plugboard_position_copy: |
| plugboard_position_copy.remove(letter) | | plugboard_position_copy.remove(letter) |
| new_text += plugboard_position_copy.pop() | | new_text += plugboard_position_copy.pop() |
| break | | break |
| else: | | else: |
| new_text += letter | | new_text += letter |
| return new_text | | return new_text |
| | | |
| | | |
| def rotor(text: str, rotor_positions: dict): | | def rotor(text: str, rotor_positions: dict): |
n | """Puts the input through the rotor""" | n | """Puts the input through the rotor.""" |
| new_text = str() | | new_text = '' |
| for letter in text: | | for letter in text: |
| if letter not in rotor_positions.values(): | | if letter not in rotor_positions.values(): |
| new_text += letter | | new_text += letter |
| continue | | continue |
| new_text += rotor_positions[letter] | | new_text += rotor_positions[letter] |
| return new_text | | return new_text |
| | | |
| | | |
| def enigma_encrypt(plugboard_position: list, rotor_position: dict): | | def enigma_encrypt(plugboard_position: list, rotor_position: dict): |
n | """Returns decorator that executes the steps to encrypt the input""" | n | """Returns decorator that executes the steps to encrypt the input.""" |
| def encrypt(func): | | def encrypt(func): |
| def encrypt_string(text: str): | | def encrypt_string(text: str): |
| text = plugboard(text, plugboard_position) | | text = plugboard(text, plugboard_position) |
| text = rotor(text, rotor_position) | | text = rotor(text, rotor_position) |
| return func(text) | | return func(text) |
| return encrypt_string | | return encrypt_string |
| return encrypt | | return encrypt |
| | | |
| | | |
| def enigma_decrypt(plugboard_position: list, rotor_position: dict): | | def enigma_decrypt(plugboard_position: list, rotor_position: dict): |
t | """Returns decorator that executes the steps to decrypt the input""" | t | """Returns decorator that executes the steps to decrypt the input.""" |
| def decrypt(func): | | def decrypt(func): |
| def decrypt_string(text: str): | | def decrypt_string(text: str): |
| new_rotor_position = {v: k for k, v in rotor_position.items()} | | new_rotor_position = {v: k for k, v in rotor_position.items()} |
| text = rotor(text, new_rotor_position) | | text = rotor(text, new_rotor_position) |
| text = plugboard(text, plugboard_position) | | text = plugboard(text, plugboard_position) |
| return func(text) | | return func(text) |
| return decrypt_string | | return decrypt_string |
| return decrypt | | return decrypt |