Vince Carter
Vince Carter

Reputation: 33

Given a Dictionary and a list of letters, find all possible words that can be created with the letters

The function scoreList(Rack) takes in a list of letters. You are also given a global variable Dictionary: ["a", "am", "at", "apple", "bat", "bar", "babble", "can", "foo", "spam", "spammy", "zzyzva"].

Using a list of letters find all possible words that can be made with the letters that are in the Dictionary. For each word that can be made also find the score of that word using scrabbleScore.

scrabbleScore =

           [ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1], 
             ["f", 4], ["g", 2], ["h", 4], ["i", 1], ["j", 8], 
             ["k", 5], ["l", 1], ["m", 3], ["n", 1], ["o", 1], ["p", 3], 
             ["q", 10], ["r", 1], ["s", 1], ["t", 1], ["u", 1], ["v", 4], 
             ["w", 4], ["x", 8], ["y", 4], ["z", 10] ]

I can use expressions made up of list comprehensions(map, filter, reduce, etc.), if statements, and for loops but only if they are in the context of a list comprehension.

Example:

>>> scoreList(["a", "s", "m", "t", "p"])
[['a', 1], ['am', 4], ['at', 2], ['spam', 8]]
>>> scoreList(["a", "s", "m", "o", "f", "o"])
[['a', 1], ['am', 4], ['foo', 6]]

The order in which they are presented does not matter. It also must remain a list and not a dic. Would love some help with this problem or if you could give me a better understanding of how to use map or filter in this problem.

MyCode:

def scoreList(Rack):
    test = [x for x in Dictionary if all(y in Rack for y in x)]
    return test

Output:

>>> scoreList(["a", "s", "m", "t", "p"])
['a', 'am', 'at', 'spam']

As you can see I was able to find the words, but not the score. How could I find it without using dict?

Upvotes: 0

Views: 946

Answers (2)

Chris_Rands
Chris_Rands

Reputation: 41168

You can use itertools to generate all possible permutations of words from a set of characters, but note if your input list becomes large this will become a lengthy and memory consuming process.

import itertools

d = dict([ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1],
             ["f", 4], ["g", 2], ["h", 4], ["i", 1], ["j", 8],
             ["k", 5], ["l", 1], ["m", 3], ["n", 1], ["o", 1], ["p", 3],
             ["q", 10], ["r", 1], ["s", 1], ["t", 1], ["u", 1], ["v", 4],
             ["w", 4], ["x", 8], ["y", 4], ["z", 10] ])

words = ["a", "am", "at", "apple", "bat", "bar", "babble", "can", "foo", "spam", "spammy", "zzyzva"]

def scoreList(l):
    perms = itertools.chain.from_iterable(list(itertools.permutations(l,r)) for r in range(1,len(l)))
    return [["".join(x),sum(d[c] for c in "".join(x))] for x in set(perms) if "".join(x) in words]

print(scoreList(["a", "s", "m", "t", "p"]))
# Outputs: [['at', 2], ['a', 1], ['spam', 8], ['am', 4]]

print(scoreList(["a", "s", "m", "o", "f", "o"]))
# Outputs: [['a', 1], ['foo', 6], ['am', 4]]

Upvotes: 1

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48067

Below is the sample code to achieve this:

score = [["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1],
         ["f", 4], ["g", 2], ["h", 4], ["i", 1], ["j", 8],
         ["k", 5], ["l", 1], ["m", 3], ["n", 1], ["o", 1], 
         ["p", 3], ["q", 10], ["r", 1], ["s", 1], ["t", 1], 
         ["u", 1], ["v", 4], ["w", 4], ["x", 8], ["y", 4], ["z", 10]]

my_list = ["a", "am", "at", "apple", "bat", "bar", "babble", "can", "foo", "spam", "spammy", "zzyzva"]
score_dict = dict(score)  # Storing score value as dict

def get_word_score(my_list):
    return [(item, sum(score_dict[a] for a in item)) for item in my_list]

get_word_score(my_list)
# returns: [('a', 1), ('am', 4), ('at', 2), ('apple', 9), ('bat', 5), ('bar', 5), ('babble', 12), ('can', 5), ('foo', 6), ('spam', 8), ('spammy', 15), ('zzyzva', 39)]

Upvotes: 0

Related Questions