f | def plugboard(text_to_be_converted, list_of_paired_letters): | f | def plugboard(text_to_be_converted, list_of_paired_letters): |
| letters_translation_dict = {} | | letters_translation_dict = {} |
n | for pair in list_of_paired_letters: | n | for first_letter, second_letter in list_of_paired_letters: |
| list_pair = list(pair) | | |
| first_letter, second_letter = list_pair[0], list_pair[1] | | |
| letters_translation_dict[first_letter] = second_letter | | letters_translation_dict[first_letter] = second_letter |
| letters_translation_dict[second_letter] = first_letter | | letters_translation_dict[second_letter] = first_letter |
| | | |
n | # Make a translation tabel from the dict with pairs and use it to replace the letters in the original string | n | # Make a translation table from the dict with pairs and use it to replace the letters in the original string |
| translation_tabel = text_to_be_converted.maketrans(letters_translation_dict) | | translation_table = text_to_be_converted.maketrans(letters_translation_dict) |
| converted_text = text_to_be_converted.translate(translation_tabel) | | converted_text = text_to_be_converted.translate(translation_table) |
| return converted_text | | return converted_text |
| | | |
| | | |
| def rotor(text_to_be_converted, alphabet_conversion_dict): | | def rotor(text_to_be_converted, alphabet_conversion_dict): |
n | translate_tabel = text_to_be_converted.maketrans(alphabet_conversion_dict) | n | translate_table = text_to_be_converted.maketrans(alphabet_conversion_dict) |
| converted_text = text_to_be_converted.translate(translate_tabel) | | converted_text = text_to_be_converted.translate(translate_table) |
| return converted_text | | return converted_text |
| | | |
| | | |
| def enigma_encrypt(plugboard_pos, rotor_pos): | | def enigma_encrypt(plugboard_pos, rotor_pos): |
| def decorate_func(f): | | def decorate_func(f): |
| def encrypt_text_wrapper(text): | | def encrypt_text_wrapper(text): |
| text = plugboard(text, plugboard_pos) | | text = plugboard(text, plugboard_pos) |
| text = rotor(text, rotor_pos) | | text = rotor(text, rotor_pos) |
| return f(text) | | return f(text) |
| return encrypt_text_wrapper | | return encrypt_text_wrapper |
| return decorate_func | | return decorate_func |
| | | |
| | | |
| def enigma_decrypt(plugboard_pos, rotor_pos): | | def enigma_decrypt(plugboard_pos, rotor_pos): |
| def decorate_func(f): | | def decorate_func(f): |
| def decrypt_text_wrapper(text): | | def decrypt_text_wrapper(text): |
| reversed_rotor_pos = dict(zip(rotor_pos.values(), rotor_pos.keys())) | | reversed_rotor_pos = dict(zip(rotor_pos.values(), rotor_pos.keys())) |
| text = rotor(text, reversed_rotor_pos) | | text = rotor(text, reversed_rotor_pos) |
| text = plugboard(text, plugboard_pos) | | text = plugboard(text, plugboard_pos) |
| return f(text) | | return f(text) |
| return decrypt_text_wrapper | | return decrypt_text_wrapper |
| return decorate_func | | return decorate_func |
t | | t | |