Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/usr/bin/env bash
  2. # tivasyk <[email protected]>
  3.  
  4. show_help() {
  5.     local NAME
  6.     NAME=$(basename "$0")
  7.     cat <<EOF
  8. $NAME will calculate the Scrabble score for a word
  9.    
  10. Usage: $NAME <word>
  11. EOF
  12. }
  13.  
  14. main () {
  15.     # One argument only (not in the excercise description, but WTH)
  16.     if [[ "$#" -ne 1 ]]; then
  17.         show_help
  18.         exit 1
  19.     fi
  20.  
  21.     local word ; word="$1"
  22.     local table=( "1:AEIOULNRST" "2:DG" "3:BCMP" "4:FHVWY" "5:K" "8:JX" "10:QZ" )
  23.     local tablelen=${#table[@]}
  24.     local score=0
  25.  
  26.     # Check every $letter in the $word against the $table[] for points
  27.     for ((i=0; i<${#word}; i++)); do
  28.         local letter=${word:$i:1}
  29.         local points=0
  30.         # Look for the line in table[] containing the $letter
  31.         for ((j=0; j<$tablelen; j++)); do
  32.             local letterset=${table[$j]/#[0-9]*:/}
  33.             # Does the $letterset contain the (uppercased) current $letter of the word?
  34.             if [[ "$letterset" == *"${letter^^}"* ]]; then
  35.                 points=${table[$j]/%:[A-Z]*/}
  36.             fi
  37.         done
  38.         score=$(( score + points ))
  39.     done
  40.  
  41.     echo "${score}"
  42. }
  43.  
  44. main "$@"