9/15 scrabble_score variants

#1

Having in mind several solutions to an exercise I wonder which one is better?
As a student here I realize that there are lots of ways to solve a given problem, but anyway, from the self-improvement perspective I would like to know what method is more preferable optimization-wise, etc.

p.s. removed ‘if’ statement after some googleing.

Also on `print(scrabble_score_v_1(new_word))` the course outputted an error `Your function fails on scrabble_score("pie" ). It returns "14" when it should return "5"` with different values on ‘14’ while The Python 3.6.1 IDLE didn’t have any of these problems and printed everything as intended. After I tried the second solution the exercise became marked as solved and no longer this error appeared regardless of my attempt to reset the exercise, was that just an issue in the course interpreter?

``````new_word = input('Your word : ')

def scrabble_score_v1(word):
total = []
for i in word.lower():
total.append(score[i])
return sum(total)

scrabble_score_v1(new_word)

def scrabble_score_v2(word):
total = 0
for i in word.lower():
total += score[i]

scrabble_score_v2(new_word)

def scrabble_score_v3(word):
total = 0
for i in word.lower():
total += score.get(i)
scrabble_score_v3(new_word)
``````

#2

the exercise expects the function to be named scrabble_score, naming the function anything else will throw an error

v1 is not the approach i would go for, doing math is more efficient then working with lists, plus `sum()` very likely uses a loop under the hood, you don’t really need two loops to solve this problem

optimization is a tricky word, spending a lot of time on an optimization which only gains you 5% “speed” is not worth the time. Programming is always a balance.

i like v3 more then v2 because with `.get()` you can more easily anticipate if you might get values that are not in score. At the moment, that is not part of the exercise, but in real life, specifications can change. So you need to code with flexibility.

#3

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