# Functional Scrabble Word Score

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 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)


Generally, generator expressions and list comprehensions are preferred to map() in Python. I'd write sum(values[c] for c in word). Using c for "character" would be preferable to x, which has a connotation of being a floating-point number.
If you are practicing functional programming, then there is no sense in including "get" in the name, as every function should be a deterministic calculation based on its parameters. By PEP 8, word_score would be a more appropriate name for the function. Also, I suggest defaulting n=7, as that is the size of a full Scrabble hand.
• Unfortunately, the function name is determined by the instructors, as are some global variables, and can't be changed or the grading software won't work right. In general naming conventions in this course are driving me nuts! values is actually SCRABBLE_LETTER_VALUES, I've just added a values = SCRA.... line for my convenience, for example. Feb 5, 2016 at 19:40