f | def translation(letter, coupled_letters): | f | def translation(letter, coupled_letters): |
| """ Finding the coupled letter of the input letter, if such exists. """ | | """ Finding the coupled letter of the input letter, if such exists. """ |
| | | |
| for (first, second) in coupled_letters: | | for (first, second) in coupled_letters: |
| if letter == first: | | if letter == first: |
| return second | | return second |
| elif letter == second: | | elif letter == second: |
| return first | | return first |
| return letter | | return letter |
| | | |
| | | |
| def plugboard(sentence, coupled_letters): | | def plugboard(sentence, coupled_letters): |
| """ Plugboard usage. """ | | """ Plugboard usage. """ |
| | | |
| list_sentence = [translation(char, coupled_letters) for char in sentence] | | list_sentence = [translation(char, coupled_letters) for char in sentence] |
| return "".join(list_sentence) | | return "".join(list_sentence) |
| | | |
| | | |
| def rotor(sentence, dict_alphabet): | | def rotor(sentence, dict_alphabet): |
| """ Rotor usage. """ | | """ Rotor usage. """ |
| | | |
| new_sentence = "".join(map(lambda letter: dict_alphabet[letter] if letter.isalpha() else letter, sentence)) | | new_sentence = "".join(map(lambda letter: dict_alphabet[letter] if letter.isalpha() else letter, sentence)) |
| return new_sentence | | return new_sentence |
| | | |
| | | |
n | def enigma_encrypt(*, plugboard_position, rotor_position): | n | def enigma_encrypt(plugboard_position, rotor_position): |
| def decorator_encrypt(func): | | def decorator_encrypt(func): |
| def encrypt(sentence): | | def encrypt(sentence): |
| | | |
| sentence = plugboard(sentence, plugboard_position) | | sentence = plugboard(sentence, plugboard_position) |
| sentence = rotor(sentence, rotor_position) | | sentence = rotor(sentence, rotor_position) |
| | | |
| return func(sentence) | | return func(sentence) |
| return encrypt | | return encrypt |
| return decorator_encrypt | | return decorator_encrypt |
| | | |
| | | |
t | def enigma_decrypt(*, plugboard_position, rotor_position): | t | def enigma_decrypt(plugboard_position, rotor_position): |
| def decorator_decrypt(func): | | def decorator_decrypt(func): |
| def decrypt(sentence): | | def decrypt(sentence): |
| | | |
| new_rotor_position = dict(zip(rotor_position.values(), rotor_position.keys())) | | new_rotor_position = dict(zip(rotor_position.values(), rotor_position.keys())) |
| sentence = rotor(sentence, new_rotor_position) | | sentence = rotor(sentence, new_rotor_position) |
| sentence = plugboard(sentence, plugboard_position) | | sentence = plugboard(sentence, plugboard_position) |
| | | |
| return func(sentence) | | return func(sentence) |
| return decrypt | | return decrypt |
| return decorator_decrypt | | return decorator_decrypt |
31.10.2023 15:30
31.10.2023 15:34
31.10.2023 15:33