Given a word, compute the scrabble score for that word.
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
"cabbage" should be scored as worth 14 points:
And to total:
3 + 2*1 + 2*3 + 2 + 1
3 + 2 + 6 + 3
5 + 9
To run the tests, run the command busted
from within the exercise directory.
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.
Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
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
}
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.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments