Skip to content Skip to sidebar Skip to footer

Find Value Of Scrabble Word

I have little experience in any higher level language. Fiddled with basic and dos-batch when I was a kid. I'm trying to find the point value of a word in scrabble. This kind of rec

Solution 1:

You'd loop over word directly and use sum():

def find_value(word):
    return sum(value_list[char] for char in word)

There is no need to use recursion here; the above needs no globals either. Try to avoid global state, as that easily leads to hard-to-debug problems when you start using functions in multiple locations.


Solution 2:

First of all, you can use list of values instead of dict, and just count index for the letter x in this list as ord(x) - ord('a')

Second, don't use global, it is a bad programming habit. You should accumulate your value in local variable and then return this value from the function.

And third, use loop instead of recursion or, even better, functions like sum.


Solution 3:

def find_value(word):

    return sum(map(lambda x:value_list[x], list(word)))



Solution 4:

Yeah, function call is expensive is Python.

Martijn Pieters's method is a good example for beginners to understand list-comprehensions.

If you want a more simple and readable way, try this:

sum(value_list.values())

Post a Comment for "Find Value Of Scrabble Word"