Given a word, compute the scrabble score for that word.
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
"cabbage" should be scored as worth 14 points:
And to total:
3 + 2*1 + 2*3 + 2 + 1
3 + 2 + 6 + 3
5 + 9
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.
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 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
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.
Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
(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) (defvar *LETTER-SCORE-ALIST* '((#\A . 1) (#\E . 1) (#\I . 1) (#\O . 1) (#\U . 1) (#\L . 1) (#\N . 1) (#\R . 1) (#\S . 1) (#\T . 1) (#\D . 2) (#\G . 2) (#\B . 3) (#\C . 3) (#\M . 3) (#\P . 3) (#\F . 4) (#\H . 4) (#\V . 4) (#\W . 4) (#\Y . 4) (#\K . 5) (#\J . 8) (#\X . 8) (#\Q . 10) (#\Z . 10))) (defun score-word (word) (or (loop for letter across (string-upcase word) when (char<= #\A letter #\Z) summing (cdr (assoc letter *LETTER-SCORE-ALIST*))) 0))
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.