Avatar of JaeHyoLee

JaeHyoLee's solution

to Scrabble Score in the Lua Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given a word, compute the scrabble score for that word.

Letter Values

You'll need these:

Letter                           Value
A, E, I, O, U, L, N, R, S, T       1
D, G                               2
B, C, M, P                         3
F, H, V, W, Y                      4
K                                  5
J, X                               8
Q, Z                               10

Examples

"cabbage" should be scored as worth 14 points:

  • 3 points for C
  • 1 point for A, twice
  • 3 points for B, twice
  • 2 points for G
  • 1 point for E

And to total:

  • 3 + 2*1 + 2*3 + 2 + 1
  • = 3 + 2 + 6 + 3
  • = 5 + 9
  • = 14

Extensions

  • You can play a double or a triple letter.
  • You can play a double or a triple word.

Running the tests

To run the tests, run the command busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

scrabble-score_spec.lua

local score = require('scrabble-score').score

describe('scrabble-score', function()
  it('scores an empty word as zero', function()
    assert.are.equal(0, score(''))
  end)

  it('scores a null as zero', function()
    assert.are.equal(0, score(null))
  end)

  it('scores a very short word', function()
    assert.are.equal(1, score('a'))
  end)

  it('scores the word by the number of letters', function()
    assert.are.equal(6, score('street'))
  end)

  it('scores more complicated words with more', function()
    assert.are.equal(22, score('quirky'))
  end)

  it('scores case insensitive words', function()
    assert.are.equal(41, score('OXYPHENBUTAZONE'))
  end)
end)
local scrabble_score = { a=1, b=3, c=3, d=2, e=1, f=4, g=2, h=4, i=1, j=8, k=5, l=1, m=3, n=1, o=1, p=3, q=10, r=1, s=1, t=1, u=1, v=4, w=4, x=8, y=4, z=10}

local function score(input)
  local s = input or ''
  local sum = 0
  for i = 1, #s do
    sum = sum + scrabble_score[s:sub(i,i):lower()]
  end
  return sum
end

return {
  score = score
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?