Skip to content

Instantly share code, notes, and snippets.

@Arsh25
Created October 19, 2016 23:18
Show Gist options
  • Save Arsh25/f300143ae57993009d8faacd6f1a3eb3 to your computer and use it in GitHub Desktop.
Save Arsh25/f300143ae57993009d8faacd6f1a3eb3 to your computer and use it in GitHub Desktop.
Semi working vesion of the first 2016 ACM coding challenge
#!/usr/bin/env python2
# Arsh Chauhan
# Find words in an anagram
# Created 10/19/2016
#Dictionary used: http://norvig.com/ngrams/enable1.txt
#FIXME: Keep tarck of how times a letter occurs in the anaram and mark
# words where letter count exceeds the number of times the letter
# shows up un the anagram as invalid
def anagrams(dict,anagram):
with open (dict,'r') as dictFile:
for word in dictFile:
word = word.rstrip()
found = True;
if word[0] == anagram[0] and word[len(word)-1] == anagram[len(anagram)-1]:
possibleWord = ""
for letter in word:
letterCount = anagram.count(letter)
letterFound = 0
anagramPos = anagram.find(letter)
if anagramPos == -1: #Letter not found in anagram
found = False
break
else:
letterFound += 1
if letterFound <= letterCount:
found = False
possibleWord += letter
else:
break
if possibleWord == word:
print (word)
if __name__ == '__main__':
anagrams("real_dict.txt","qwertyuytresdftyuioknn")
print('\n')
#anagrams("real_dict.txt","gijakjthoijerjidsdfnokg")
#anagrams("dict.txt","queestn")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment