Sobsz

elian ambigram finder

Oct 10th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. # if this is somehow copyrightable then cc0 (do whatever): https://creativecommons.org/publicdomain/zero/1.0/
  2.  
  3. import math
  4. import string
  5.  
  6. alphabet = string.ascii_lowercase + "&"
  7. upside_down = {}
  8. for i in range(len(alphabet)):
  9.     upside_down[alphabet[i]] = alphabet[math.floor(i/9)*9 + 8-(i%9)] # 012345678 -> 876543210
  10.  
  11. def flip(s):
  12.     return "".join([upside_down[i] if i in upside_down else i for i in s])[::-1]
  13.  
  14. with open("words.txt", "r") as f:
  15.     words = {word.lower() : 0 for word in f.read().splitlines()} # dict is hashmap so fast lookup
  16.  
  17. found = {} # dict keeps order so we can preserve the alphabetizedness
  18. for i in words:
  19.     if flip(i) in words:
  20.         found[tuple(sorted((i, flip(i))))] = None
  21. found = sorted(found, key = lambda i: len(i[0]), reverse = True)
  22.  
  23. print(len(found))
  24.  
  25. with open("elian ambigrams.txt", "w") as f:
  26.     f.write("\n".join([" ".join(i) for i in found]))
  27.  
Add Comment
Please, Sign In to add comment