# Scrabble_score (both recursive and loop)

#1

OK, so after finishing the last one, I decided to tackle this one. I figured both solutions out within fifteen minutes.

This said, I am starting to wonder if there's a better way to do the recursive function now. So far I've been doing a function with a helper function for the recursive solution; I don't think there's a way to get away from this, at least so far.

I wonder if there's a way to use regular expressions to help with this. Or if there are other functions available to help reduce the size of the code.

For loop version:

``````score = {"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 scrabble_score(word):
word = word.lower()
total = 0
for char in word:
total += score[char]
print total

print scrabble_score("Mix")``````

Recursive with helper function:

``````score = {"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 scrabble_score(word):
word = word.lower()
total = 0
word_length = len(word)
if word_length == 1:
return score[word]
else:
return sum_score(word, word_length-1)

def sum_score(word, index):
if index == 0:
return score[word[index]]
else:
return sum_score(word, index-1) + score[word[index]]

print scrabble_score("Mix")``````

#2

I think in order to get out of the helper function trap, create one with a helper and then see if you can somehow incorporate that into your main function. Here, you need index but you can actually do something like `word[0]` or `word[1:]` in your main function.

I believe this `scrabble_score` will use almost the same concept of how I did `anit_vowel` where you'll have to move onto the next letter using `word[1:]` and that becomes `word[0]`.
This is actually a really short recursive function and let me give you the cases.

You have 2, if word is empty and when it's not.
If word is empty, your score is 0, right?
If word is not empty, then you need to get the score for the letter + score for the next letters by calling the function again but doing `word[1:]` instead of `word`.

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.