Skip to content

Instantly share code, notes, and snippets.

@jonathan-ostrander
Last active August 29, 2015 14:16
Show Gist options
  • Save jonathan-ostrander/f3500e5536b9f025dbf2 to your computer and use it in GitHub Desktop.
Save jonathan-ostrander/f3500e5536b9f025dbf2 to your computer and use it in GitHub Desktop.
Find "alphabet swap" words with python.
avez
azaz
clox
girt
grit
hovels
iver
izar
klop
levo
lolo
lololo
ozal
polk
riri
sshh
vire
vivere
vole
wird
wizard
zaza
zira
zola
# Finds all words that exhibit the alphabet swap property, when each letter in the word is mapped to it's opposite (a -> z, b -> y, etc.)
# the new word will be the old reversed.
# Dictionary from http://www.math.sjsu.edu/~foster/dictionary.txt
# Inspired by http://www.reddit.com/r/CasualConversation/comments/2x5lwd/i_wrote_a_program_to_find_all_of_the_words_in_the/
# Determine if the first n/2 + 1 letters in a word with length n map to their opposite
def isWizWord(word):
word = word.lower()
return all([isRev(word[i], word[::-1][i]) for i in range(len(word)/2 + 1)])
# Determines if two characters are opposites
# i.e. the first characters distance from 'z' is equal to the second's distance from 'a'
def isRev(c1, c2):
return ord("z") - ord(c1) == ord(c2) - ord("a")
if __name__ == "__main__":
with file("dictionary.txt", "r") as f:
print "\n".join([word.strip() for word in f if isWizWord(word.strip())])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment