ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# giner's solution

## to Scrabble Score in the Bash Track

Published at Jun 08 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution 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.

Run the tests with:

``````bats scrabble_score_test.sh
``````

After the first test(s) pass, continue by commenting out or removing the `skip` annotations prepending other tests.

## Source

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

## External utilities

`Bash` is a language to write scripts that works closely with various system utilities, like `sed`, `awk`, `date` and even other programming languages, like `Python`. This track does not restrict the usage of these utilities, and as long as your solution is portable between systems and does not require installing third party applications, feel free to use them to solve the exercise.

For an extra challenge, if you would like to have a better understanding of the language, try to re-implement the solution in pure `Bash`, without using any external tools.

## Submitting Incomplete Solutions

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

### scrabble_score_test.sh

``````#!/usr/bin/env bash

@test 'lowercase letter' {
#skip
run bash scrabble_score.sh 'a'

[ "\$status" -eq 0 ]
[ "\$output" -eq 1 ]
}

@test 'uppercase letter' {
skip
run bash scrabble_score.sh 'A'

[ "\$status" -eq 0 ]
[ "\$output" -eq 1 ]
}

@test 'valuable letter' {
skip
run bash scrabble_score.sh 'f'

[ "\$status" -eq 0 ]
[ "\$output" -eq 4 ]
}

@test 'short word' {
skip
run bash scrabble_score.sh 'at'

[ "\$status" -eq 0 ]
[ "\$output" -eq 2 ]
}

@test 'short, valuable word' {
skip
run bash scrabble_score.sh 'zoo'

[ "\$status" -eq 0 ]
[ "\$output" -eq 12 ]
}

@test 'medium word' {
skip
run bash scrabble_score.sh 'street'

[ "\$status" -eq 0 ]
[ "\$output" -eq 6 ]
}

@test 'medium, valuable word' {
skip
run bash scrabble_score.sh 'quirky'

[ "\$status" -eq 0 ]
[ "\$output" -eq 22 ]
}

@test 'long, mixed-case word' {
skip
run bash scrabble_score.sh 'OxyphenButazone'

[ "\$status" -eq 0 ]
[ "\$output" -eq 41 ]
}

@test 'english-like word' {
skip
run bash scrabble_score.sh 'pinata'

[ "\$status" -eq 0 ]
[ "\$output" -eq 8 ]
}

@test 'empty input' {
skip
run bash scrabble_score.sh ''

[ "\$status" -eq 0 ]
[ "\$output" -eq 0 ]
}

@test 'entire alphabet available' {
skip
run bash scrabble_score.sh 'abcdefghijklmnopqrstuvwxyz'

[ "\$status" -eq 0 ]
[ "\$output" -eq 87 ]
}``````
``````#!/bin/bash

set -o errexit
set -o nounset

declare -rA value_letters=(
[1]=AEIOULNRST
[2]=DG
[3]=BCMP
[4]=FHVWY
[5]=K
[8]=JX
[10]=QZ
)

# Transform value_letters[\$value] map to letter_value[\$letter] map
declare -A letter_value
for value in "\${!value_letters[@]}"; do
letters=\${value_letters[\$value]}
for ((j=0; j<\${#letters}; j++)); do
letter=\${letters:j:1}
letter_value[\$letter]=\$value
done
done

scrabble_score() {
# Capitalize input
local word=\${1^^}

local score=0
local letter

local i
for ((i=0; i<\${#word}; i++)); do
letter=\${word:\$i:1}
(( score += \${letter_value[\$letter]} ))
done

echo "\$score"
}

scrabble_score "\$@"``````