Avatar of joaofnds

joaofnds's solution

to Scrabble Score in the Common Lisp Track

Published at Oct 28 2019 · 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.

Setup

Check out Installing Common Lisp for instructions to get started or take a look at the guides available in the track's side bar.

Formatting

While Common Lisp doesn't care about indentation and layout of code, nor whether you use spaces or tabs, this is an important consideration for submissions to exercism.io. Excercism.io's code widget cannot handle mixing of tab and space characters well so using only spaces is recommended to make the code more readable to the human reviewers. Please review your editors settings on how to accomplish this. Below are instructions for popular editors for Common Lisp.

VIM

Use the following commands to ensure VIM uses only spaces for indentation:

:set tabstop=2
:set shiftwidth=2
:set expandtab

(or as a oneliner :set tabstop=2 shiftwidth=2 expandtab). This can be added to your ~/.vimrc file to use it all the time.

Emacs

Emacs is very well suited for editing Common Lisp and has many powerful add-on packages available. The only thing that one needs to do with a stock emacs to make it work well with exercism.io is to evaluate the following code:

(setq-default indent-tabs-mode nil)

This can be placed in your ~/.emacs (or ~/.emacs.d/init.el) in order to have it set whenever Emacs is launched.

One suggested add-on for Emacs and Common Lisp is SLIME which offers tight integration with the REPL; making iterative coding and testing very easy.

Source

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

Submitting Incomplete Solutions

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

scrabble-score-test.lisp

(ql:quickload "lisp-unit")
#-xlisp-test (load "scrabble-score")

(defpackage #:scrabble-score-test
  (:use #:cl #:lisp-unit))

(in-package #:scrabble-score-test)

(define-test no-word-has-zero-score
  (assert-equal 0 (scrabble-score:score-word "")))

(define-test whitespace-only-also-has-zero-score
  (assert-equal 0 (scrabble-score:score-word
                   (concatenate 'string '(#\Space #\Newline)))))

(define-test scores-small-word
  (assert-equal 1 (scrabble-score:score-word "a")))

(define-test is-case-insensitive
  (assert-equal 1 (scrabble-score:score-word "A")))

(define-test scores-a-slightly-bigger-word
  (assert-equal 2 (scrabble-score:score-word "at")))

(define-test scores-a-middle-of-the-road-word
  (assert-equal 6 (scrabble-score:score-word "street")))

(define-test scores-a-peculiar-word
  (assert-equal 22 (scrabble-score:score-word "quirky")))

(define-test scores-a-very-long-word
  (assert-equal 27 (scrabble-score:score-word "UNEXCLUSIVENESS")))

#-xlisp-test
(let ((*print-errors* t)
      (*print-failures* t))
  (run-tests :all :scrabble-score-test))
(defpackage #:scrabble-score
  (:use #:cl)
  (:export #:score-word))

(in-package #:scrabble-score)

(defun letter-score (letter)
  (case (char-downcase letter)
    ((#\a #\e #\i #\l #\n #\o #\r #\s #\t #\u) 1)
    ((#\d #\g) 2)
    ((#\b #\c #\m #\p) 3)
    ((#\f #\h #\v #\w #\y) 4)
    ((#\k) 5)
    ((#\j #\x) 8)
    ((#\q #\z) 10)
    (otherwise 0)))

(defun score-word (word)
  (reduce #'+ word :key #'letter-score))

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?