Skip to content

Instantly share code, notes, and snippets.

@danabauer
Last active March 17, 2021 14:29
Show Gist options
  • Select an option

  • Save danabauer/81994df87617405ac92e to your computer and use it in GitHub Desktop.

Select an option

Save danabauer/81994df87617405ac92e to your computer and use it in GitHub Desktop.
PL coding test
#!/usr/bin/env python
'''
An anagram is a word formed by rearranging the letters of another, like "topside" and "deposit". In some cases, there might be as many (or more) anagrams than there are characters, like "post", "spot", "stop" and "tops".
Write a program to find all of the anagrams in a dictionary in which there are at least 4 letters in the word and at least as many anagrams as there are letters.
The dictionary will be a file on disk with one line per word. Your operating system likely already has such a file in /usr/dict/words or /usr/share/dict/words.
The output produced by your code should be in this format:
emit, item, mite, time
merit, miter, mitre, remit, timer
reins, resin, rinse, risen, serin, siren
inert, inter, niter, retin, trine
inset, neist, snite, stein, stine, tsine
Adapted from http://stackoverflow.com/questions/8286554/find-anagrams-for-a-list-of-words
'''
from collections import defaultdict
def get_words(filename = '/usr/share/dict/words'):
'''
This generator function loads the local word file and loops through the list of words, yielding the next word in the file
each time the function is invoked
'''
with open(filename) as words:
for word in words:
yield word.strip() #yield! elegant!
def find_anagrams(word_source):
'''
This function makes a key from the sorted version of each word and appends a list of anagrams of that word as it loops
through the word file. The function returns a dictionary of lists.
'''
#initialize defaultdict
d = defaultdict(list)
for word in word_source:
if len(word) > 3: #only words with more than three characters per assignment above
key = "".join(sorted(word))
d[key].append(word)
return d
def print_anagrams(word_source):
'''
This function prints the lists of anagrams from the word source, one list at a time. It calls the find_anagrams function and
populates a new dictionary, d, with the key and lists of anagrams. It loops over the key and anagrams and prints the anagrams with
each list on a new line
'''
d = find_anagrams(word_source)
for key, anagrams in d.iteritems(): #iteritems (python 2.x): stateful, keeps track of where we are in the dictionary of lists
if len(anagrams) >= len(key): #only print lists of anagrams where the # of anagrams is >= the length of the word
print(','.join(anagrams)) #print anagrams in format specified in assignment
#Set word_source. In this case, we're using get_words function to load words from local wordlist. Word source could be any list though.
word_source = get_words()
#Call print_anagrams function. The three functions above are streaming into each other. The words are streaming through the functions word by word
print_anagrams(word_source)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment