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 score=0
  23.  
  24.     # Check every $letter in the $word against the $table[] for points
  25.     for ((i=0; i<${#word}; i++)); do
  26.         local letter=${word:$i:1}
  27.  
  28.         # Simple solution with case
  29.         case ${letter^^} in
  30.             [AEIOULNRST] ) ((score+=1)) ;;
  31.             [DG] ) ((score+=2)) ;;
  32.             [BCMP] ) ((score+=3)) ;;
  33.             [FHVWY] ) ((score+=4)) ;;
  34.             [K] ) ((score+=5)) ;;
  35.             [JX] ) ((score+=8)) ;;
  36.             [QZ] ) ((score+=10)) ;;
  37.             * ) ;;
  38.         esac
  39.     done
  40.  
  41.     echo "${score}"
  42. }
  43.  
  44. main "$@"