Skip to content

Instantly share code, notes, and snippets.

@zackmdavis
Last active March 27, 2022 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zackmdavis/d9cbf8053d92bc6857f10583da9b0cb4 to your computer and use it in GitHub Desktop.
Save zackmdavis/d9cbf8053d92bc6857f10583da9b0cb4 to your computer and use it in GitHub Desktop.
words that are still words if you swap a vowel
from string import ascii_lowercase
def load_dictionary():
dictionary = set()
with open("/usr/share/dict/words") as dictionary_file:
for word in dictionary_file:
dictionary.add(word.strip())
return dictionary
VOWELS = ['a', 'e', 'i', 'o', 'u']
def vowel_swap_words():
dictionary = load_dictionary()
results = []
for head in ascii_lowercase:
for tail in ascii_lowercase:
if all(head+vowel+tail in dictionary for vowel in VOWELS):
results.append((head, tail))
return results
if __name__ == "__main__":
print(vowel_swap_words())
from string import ascii_lowercase
def load_dictionary():
dictionary = set()
with open("/usr/share/dict/words") as dictionary_file:
for word in dictionary_file:
dictionary.add(word.strip())
return dictionary
VOWELS = ['a', 'e', 'i', 'o', 'u']
def vowel_swap_words():
dictionary = load_dictionary()
results = set()
for word in dictionary:
if "'" in word:
continue # exclude possessives
for i, letter in enumerate(word):
if letter in VOWELS:
head, tail = word[:i], word[i+1:]
if not head and not tail:
break # exclude 'a', &c. as words
if all(head+vowel+tail in dictionary for vowel in VOWELS):
results.add((head, tail))
return results
if __name__ == "__main__":
answer = vowel_swap_words()
print(len(answer))
print(answer)
# zmd@ReflectiveCoherence:~/Code/Misc$ python3 vowel_swap_words.py
# 20
# {('f', 'r'), ('m', 'sses'), ('p', 'tting'), ('b', 'lls'), ('bl', 'nder'), ('p', 'ps'), ('p', 'ts'), ('p', 'tted'), ('p', 't'), ('p', 'p'), ('t', 'n'), ('b', 'll'), ('m', 'te'), ('l', 'st'), ('m', 'ss'), ('b', 'g'), ('p', 'cks'), ('p', 'ck'), ('t', 'ns'), ('m', 'tes')}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment