Skip to content

Instantly share code, notes, and snippets.

@younata
Created February 2, 2014 05:56
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 younata/8763640 to your computer and use it in GitHub Desktop.
Save younata/8763640 to your computer and use it in GitHub Desktop.
Scrabble
#!/usr/bin/env python
dictionary="/usr/share/dict/words"
# usage: scrabble.py <letters>
# also, pass it through grep if you want more complex patterns. Personally, my regex-fu isn't that strong.
# for the most part, I'll do something like './scrabble.py oschwi*us | grep 'us$' for a word ending in us.
# that's about the extent of my regex-fu
def readDict(diction=dictionary):
ret = []
f = open(diction)
for lines in f:
ret.append(lines.strip())
f.close()
return ret;
def dictLetters(letters):
ret = {}
for l in letters:
if l in ret.keys():
ret[l] += 1
else:
ret[l] = 1
return ret
def main(letters, maxlen = -1):
d = readDict()
for word in d:
shouldPrint=True
dl = dictLetters(letters)
if maxlen != -1 and len(word) > maxlen:
continue
for l in word:
if l not in dl.keys() or dl[l] == 0:
if '*' in dl.keys() and dl['*'] != 0:
dl['*'] -= 1
else:
shouldPrint = False
else:
dl[l] -= 1
if shouldPrint:
print word
if __name__ == "__main__":
import sys
a = sys.argv
if (len(a) == 1):
print "wrong"
elif (len(a) == 2):
main(a[1])
else:
main(a[1], int(a[2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment