Last active
July 23, 2023 08:39
-
-
Save FlorianLatapie/dcbfc6a26423162c2fef6886a3501684 to your computer and use it in GitHub Desktop.
This script prints words that are still words when rot13 is applied.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from unidecode import unidecode | |
import codecs | |
def rot13(mot): | |
return codecs.encode(mot, 'rot_13') | |
def string_set_from_file(file_path, encoding="utf-8"): | |
try: | |
file = open(file_path, "r", encoding=encoding) | |
except FileNotFoundError: | |
print("File not found") | |
sys.exit(1) | |
no_accents_strings = set() | |
for line in file: | |
line = line.rstrip("\n") # Delete the \n at the end of the line | |
no_accents_strings.add(unidecode(line).lower()) # remove accents and convert to lowercase | |
file.close() | |
return no_accents_strings | |
def palin13(all_words): | |
valid_rotated_words = set() | |
# Add the palindromes to the set | |
[valid_rotated_words.add(current_word) for current_word in all_words if | |
rot13(current_word) in all_words and rot13(current_word) not in valid_rotated_words] # do not add duplicates | |
return valid_rotated_words | |
if __name__ == '__main__': | |
import sys | |
path = input("Enter the file path: ") if len(sys.argv) < 2 else sys.argv[1] | |
res = palin13(string_set_from_file(path)) | |
# Print the result | |
print([(word, rot13(word)) for word in res]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment