Homework: Write a function to score a scrabble word.
values
is a global dict formatted as values = {'a': 1, 'b': 3,...}
The docstring is supplied as part of the course, thus I'm only looking for feedback on the actual program logic.
I could have written a bunch of variables, iterated over the string word
, looking up points from values
, etc., but I've been reading about functional programming lately and this is my first attempt at something "non-procedural".
I feel like this code (while working) looks terrible, isn't easily understood, and has more than one code smell. For example, I understand that breadth is generally preferred over depth (at least in python, right?), uses the much-hated python ternary, and is 89 chars wide (plus probably more significant smells I don't even recognize).
I'm trying to:
- not declare pointless or temporary variables
- be concise
- avoid traditional loops
- avoid changing variables' values
I considered rewriting with a nested recursive function, would this make more sense? Also, if I'm using the term "functional" in the wrong way, or completely missing the point, please correct.
def getWordScore(word, n):
"""
Returns the score for a word. Assumes the word is a valid word.
The score for a word is the sum of the points for letters in the
word, multiplied by the length of the word, PLUS 50 points if all n
letters are used on the first turn.
word: string (lowercase letters)
n: integer (hand size required for additional points)
returns: int >= 0
"""
return len(word) * sum(map(lambda x: values[x], word)) + (50 if len(word)==n else 0)