Avatar of tivasyk

tivasyk's solution

to Scrabble Score in the Bash Track

Published at Dec 13 2018 · 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 ]
}
#!/usr/bin/env bash
# tivasyk <tivasyk@gmail.com>

show_help() {
    local NAME
    NAME=$(basename "$0")
    cat <<EOF
$NAME will calculate the Scrabble score for a word
    
Usage: $NAME <word>
EOF
}

main () {
    # One argument only (not in the excercise description, but WTH)
    if [[ "$#" -ne 1 ]]; then
        show_help
        exit 1
    fi

    local word ; word="$1"
    local score=0

    # Check every $letter in the $word against the $table[] for points
    for ((i=0; i<${#word}; i++)); do
        local letter=${word:$i:1}

        # Simple solution with case
        case ${letter^^} in
            [AEIOULNRST] ) ((score+=1)) ;;
            [DG] ) ((score+=2)) ;;
            [BCMP] ) ((score+=3)) ;;
            [FHVWY] ) ((score+=4)) ;;
            [K] ) ((score+=5)) ;;
            [JX] ) ((score+=8)) ;;
            [QZ] ) ((score+=10)) ;;
            * ) ;;
        esac
    done

    echo "${score}"
}

main "$@"

Community comments

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

tivasyk's Reflection

my first version used a rather complicated search of a letter within an array. it was something i'd do in some other programming language, and it worked.

however i've borrowed a more beautiful, bash'y idea of just using case, it's simpler, more readable and it works as well! i guess i've just learned something, again ― thanks, exercism.io =)