9. Scrabble_score


#1
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):
  total = 0
  word = word.lower()
  for x in word:
    total = total + score[x]
  return total
scrabble_score("king")

Hello,
I think the above code is much simpler than the one in the answer and it seemed to work perfectly fine. Am I missing something?
Here’s the code recommended by the program:
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 letter in word:
for leter in score:
if letter == leter:
total = total + score[leter]
return total


#2

well, the recommended solution would catch invalid input, yours won’t, but then the instructions used to say that you can assume valid input, so a bit confusing.


#3

Is this from the recommended solution? It is inefficient …

  for letter in word:
    for leter in score:
      if letter == leter:
        total = total + score[leter]

Wouldn’t this be better? …

  for letter in word:
    if letter in score:
        total = total + score[letter]

#4

yes, it is. (2x), its the recommended solution which also happens to be inefficient.

what about:

if score.get(letter):

but if in and get() are both certainly more effective then 2 loops


#5

@stetim94,

Yes, yours is nice and concise, as well as efficient.


#6

Yeah that’s near criminal lol.


That wants to be:

if (letter_score = score.get(letter)):
Which of course is some kind of C-ism (aside from not being valid python)

I just want to map the dict onto input:

from collections import defaultdict

score, oldscore = defaultdict(int), score
score.update(oldscore)
score.update({k.upper(): score[k] for k in score.keys()})


def scrabble_score(word):
    return sum(map(score.get, word))


print(scrabble_score('helLO'))

That got out of hand. Probably more like this:

def scrabble_score(word):
    return sum(score.get(letter, 0) for letter in word.lower())

… Not that either would be an appropriate example solution :confused:
And regardless of what I want, a regular loop would be more obvious


#7

why? .get() returns None if key is not in dictionary, can’t we rely on anything at all?


#8

I mean, you’d still have to do two lookups! And then in ends up preferable for testing membership

You’d do: score.get(char, 0) and skip the if-statement completely


#9

yes, but that is not my question. Why can’t we simply rely on .get() to return None:

if score.get(letter):

do we have to do:

if (letter_score = score.get(letter)):

of course there are more effective ways to implement this program, but i didn’t want to introduce it in this topic.


#10

if score.get(letter)
Doesn’t use the value, so it should be
if letter in score
get suggests that it “wants” is to both look up and test, which in C is done with assignment in the condition, though get allows for a default value

You can do that! But it wants to be something else, and what it does can be written with in

Overall though, this exercise has me itching to use map, just map the translation table onto the input to get the output


#11

that makes sense, what about:

if letter in score:
        total = total + score[letter]

vs:

x = score.get(letter)
if x:
    total = total + score[letter]

now we reduced the code to a single lookup.


#12

Yeah! But we can go one step further with

total += score.get(letter, 0)

#13

that is clever :slight_smile: True, i forgot, i should have checked the documentation, i remembered the .get() function, good lesson, always check documentation before implementing something


#14

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