f | """ This is my second homework :) | f | """ This is my second homework :) |
| ,\ | | ,\ |
| # (_ | | # (_ |
| _[]## | | _[]## |
| ###(())_ | | ###(())_ |
| ){}#### | | ){}#### |
| _(( | | _(( |
| ####/ )\ | | ####/ )\ |
| ,;;`;, | | ,;;`;, |
| (_______) | | (_______) |
| {===} | | {===} |
| /===\ | | /===\ |
| /=aat=\ | | /=aat=\ |
| The bonsai is PEP8 escape compatible | | The bonsai is PEP8 escape compatible |
| The bonsai is here because my ssd failed n times, | | The bonsai is here because my ssd failed n times, |
| and the laptop is giving blue screen every 5 minutes(git files are also not physically saving, | | and the laptop is giving blue screen every 5 minutes(git files are also not physically saving, |
| just as the homework files in the git repo ...) :) | | just as the homework files in the git repo ...) :) |
| The homework function were having validation and exceptions, but yep in the past .... | | The homework function were having validation and exceptions, but yep in the past .... |
| """ | | """ |
| | | |
| | | |
| def plugboard(text, plugboard_map): | | def plugboard(text, plugboard_map): |
| """ | | """ |
| Plugboard for enigma machine | | Plugboard for enigma machine |
| | | |
| :param str text: The input text | | :param str text: The input text |
| :param list(two_element_sets()) plugboard_map: List of two element sets (Letter swap mapping) | | :param list(two_element_sets()) plugboard_map: List of two element sets (Letter swap mapping) |
| :return: Encoded text | | :return: Encoded text |
| :rtype str | | :rtype str |
| """ | | """ |
| def plugboard_transform(letter): | | def plugboard_transform(letter): |
| for set_pair in plugboard_map: | | for set_pair in plugboard_map: |
| if letter in set_pair: | | if letter in set_pair: |
| return next(iter(set_pair.difference(set(letter)))) | | return next(iter(set_pair.difference(set(letter)))) |
| return letter | | return letter |
| | | |
n | text = text.lower() | n | return ''.join([plugboard_transform(letter) for letter in text]) |
| mapped_text = [] | | |
| [mapped_text.append(plugboard_transform(letter)) for letter in text] | | |
| return ''.join(mapped_text) | | |
| | | |
| | | |
| def rotor(text, dictionary): | | def rotor(text, dictionary): |
| """ | | """ |
| Rotor for enigma machine | | Rotor for enigma machine |
| | | |
| :param str text: The input text | | :param str text: The input text |
| :param {one_letter_str,one_letter_str} dictionary: Rotor dictionary | | :param {one_letter_str,one_letter_str} dictionary: Rotor dictionary |
| :return: Encoded text | | :return: Encoded text |
| :rtype str | | :rtype str |
| """ | | """ |
n | | n | |
| text = text.lower() | | |
| mapped_text = [] | | mapped_text = [] |
| for letter in text: | | for letter in text: |
| if letter.isalpha(): | | if letter.isalpha(): |
n | mapped_text.append(dictionary[letter]) | n | mapped_text.append(dictionary.get(letter)) |
| else: | | else: |
| mapped_text.append(letter) | | mapped_text.append(letter) |
| return ''.join(mapped_text) | | return ''.join(mapped_text) |
| | | |
| | | |
| def enigma_encrypt(plugboard_position, rotor_position): | | def enigma_encrypt(plugboard_position, rotor_position): |
| """ | | """ |
| Encryption function for enigma machine | | Encryption function for enigma machine |
| | | |
| :param list(two_element_set(str))) plugboard_position: Plugboard configuration | | :param list(two_element_set(str))) plugboard_position: Plugboard configuration |
| :param {one_letter_str,one_letter_str} rotor_position : Rotor dictionary configuration | | :param {one_letter_str,one_letter_str} rotor_position : Rotor dictionary configuration |
| :return: Decorator acting over function(str) giving the function enigma-encoded str | | :return: Decorator acting over function(str) giving the function enigma-encoded str |
| :rtype function-decorator | | :rtype function-decorator |
| """ | | """ |
t | | t | |
| def decorator(func): | | def decorator(func): |
| def encoder(text): | | def encoder(text): |
| return func(rotor(plugboard(text, plugboard_position), rotor_position)) | | return func(rotor(plugboard(text, plugboard_position), rotor_position)) |
| return encoder | | return encoder |
| return decorator | | return decorator |
| | | |
| | | |
| def enigma_decrypt(plugboard_position, rotor_position): | | def enigma_decrypt(plugboard_position, rotor_position): |
| """ | | """ |
| Decryption function for enigma machine | | Decryption function for enigma machine |
| | | |
| :param list(two_element_set(str))) plugboard_position: Plugboard configuration | | :param list(two_element_set(str))) plugboard_position: Plugboard configuration |
| :param {one_letter_str,one_letter_str} rotor_position : Rotor dictionary configuration | | :param {one_letter_str,one_letter_str} rotor_position : Rotor dictionary configuration |
| :return: Decorator acting over function(str) giving the function enigma-encoded str | | :return: Decorator acting over function(str) giving the function enigma-encoded str |
| :rtype function-decorator | | :rtype function-decorator |
| """ | | """ |
| def decorator(func): | | def decorator(func): |
| def decoder(text): | | def decoder(text): |
| # Do you see the snake? :P | | # Do you see the snake? :P |
| return func(plugboard(rotor(text, dict(zip(rotor_position.values(), | | return func(plugboard(rotor(text, dict(zip(rotor_position.values(), |
| rotor_position.keys()))), plugboard_position)) | | rotor_position.keys()))), plugboard_position)) |
| return decoder | | return decoder |
| return decorator | | return decorator |