Avatar of binaryphile

binaryphile's solution

to Scrabble Score in the Bash Track

Published at Sep 16 2018 · 0 comments
Instructions
Test suite
Solution

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

# http://www.binaryphile.com/bash/2018/07/26/approach-bash-like-a-developer-part-1-intro.html

IFS=$'\n'
set -o noglob
shopt -s nocasematch expand_aliases
alias args?='(( $# ))'
alias args_include?='include? "$*"'
alias fewer_args_than?='fewer_than? $#'

get () {
  local ref_=$1 indent_

  ! read -rd '' $ref_
  indent_=${!ref_%%[^[:space:]]*}
  printf -v $ref_ %s "${!ref_#$indent_}"
  printf -v $ref_ %s "${!ref_//$'\n'$indent_/$'\n'}"
}

Prog=${0##*/}

get Usage <<END
  $Prog: Calculate the scrabble score of a word

  Usage:

    $Prog WORD

  A blank word is acceptable and scores 0.
END

main () {
  blank? ${1:-} && die 0
  split $1 | map score | reduce +
}

blank? () {
  [[ -z ${1:-} ]]
}

die () {
  local rc=$?

  present? ${2:-}   && rc=$2
  present? "${1:-}" && echo "$1"
  exit $rc
}

fewer_than? () {
  (( $1 < $2 ))
}

include? () {
  [[ $IFS$1$IFS == *"$IFS$2$IFS"* ]]
}

map () {
  local item

  while read -r item; do
    $1 $item
  done
}

present? () {
  [[ -n ${1:-} ]]
}

reduce () {
  local -i item result

  read -r result
  while read -r item; do
    result=$result$1$item
  done
  echo $result
}

score () {
  case $1 in
    [aeilnorstu]  ) echo 1  ;;
    [dg]          ) echo 2  ;;
    [bcmp]        ) echo 3  ;;
    [fhvwy]       ) echo 4  ;;
    k             ) echo 5  ;;
    [jx]          ) echo 8  ;;
    [qz]          ) echo 10 ;;
  esac
}

sourced? () {
  [[ ${FUNCNAME[1]} == source ]]
}

split () {
  local i

  for (( i = 0; i < ${#1}; i++ )); do
    echo ${1:i:1}
  done
}

strict_mode () {
  case $1 in
    on  ) set -euo pipefail;;
    off ) set +euo pipefail;;
  esac
}

usage () {
  local rc=0

  present? ${1:-} && {
    echo "$1$IFS"
    rc=2
  }
  die "$Usage" $rc
}

sourced? && return
strict_mode on

args_include? --help  && usage
args?                 || usage "Error: argument required"
fewer_args_than? 2    || usage "Error: wrong number of arguments"

main $*

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?