Sorry, your browser is not supported
To have the best experience using Skillshare, we recommend that you use one of these supported browsers.

714

--

# Becky's scrabble word finder

I used a recursive function to call the function with fewer letters until we get to two letters. When there are two letters I return the two letters (letter1+letter2 and letter2+letter1) and then I recursively add back each letter onto this word.

To speed up lookup time of good words after I only push on unique possible words.

I could NOT figure out how to store my code in code academy so here it is

-------------

# Input a Scrabble rack (the Scrabble letters you have to play)
# Print all valid Scrabble words that can be constructed from that rack,
# along with their Scrabble scores, sorted by score.

import sys
import collections

def choose (letters,n):
if n < 2:
return []
if n == 2:
return (letters[0]+letters[1],letters[1]+letters[0])
words = []
for letter in letters:
# Make a copy of our letters and remove one letter at a time
temp = list(letters)
temp.remove(letter)
for word in choose (temp,n-1):
# check for membership before pushing to avoid duplicates
if word not in words:
words.append(word)
if letter+word not in words:
words.append(letter + word)
return words

scores = {"A": 1, "C": 3, "B": 3, "E": 1, "D": 2, "G": 2,
"F": 4, "I": 1, "H": 4, "K": 5, "J": 8, "M": 3,
"L": 1, "O": 1, "N": 1, "Q": 10, "P": 3, "S": 1,
"R": 1, "U": 1, "T": 1, "W": 4, "V": 4, "Y": 4,
"X": 8, "Z": 10}

def CountScore (word):
# use scores to compute the scrabble score of word
total = 0
for letter in word:
total += scores[letter]

# Read the letters that we have on our rack
cmdline = str(sys.argv)
if len(sys.argv) != 2:
print "Error usage: python project.py <7 letters>"
print "length = " + str(len(sys.argv))
sys.exit(0)

rack = sys.argv[1]
if len(rack) != 7:
print "Error must have a rack of exactly 7 letters"
sys.exit(0)

# Extract each letter and put in our rack_letters list
rack_letters = []
for letter in rack:
rack_letters.append(letter)

possible_words = choose (rack_letters,len(rack_letters))

# Read in all valid words in the dictionary
f = open ("sowpods.txt","r")
dict_words = []

for line in f:
line = line.strip()
dict_words.append(line)
f.close

# Take all the possible words and keep the legitimate ones, and lookup their scrabble scores
legit_word_scores = {}
for word in possible_words:
if word in dict_words and word not in legit_word_scores:
legit_word_scores[word] = CountScore(word)

# sort the scores
sorted_scores = collections.OrderedDict(sorted(legit_word_scores.items(), key=lambda t: t[1]))

#
print "Legitimate words sorted by scores in increasing order"
for word in sorted_scores:
print str(sorted_scores[word]) + " " + word