Avatar of soerenbnoergaard

soerenbnoergaard's solution

to Raindrops in the Common Lisp Track

Published at Nov 10 2019 · 0 comments
Instructions
Test suite
Solution

Convert a number to a string, the contents of which depend on the number's factors.

  • If the number has 3 as a factor, output 'Pling'.
  • If the number has 5 as a factor, output 'Plang'.
  • If the number has 7 as a factor, output 'Plong'.
  • If the number does not have 3, 5, or 7 as a factor, just pass the number's digits straight through.

Examples

  • 28's factors are 1, 2, 4, 7, 14, 28.
    • In raindrop-speak, this would be a simple "Plong".
  • 30's factors are 1, 2, 3, 5, 6, 10, 15, 30.
    • In raindrop-speak, this would be a "PlingPlang".
  • 34 has four factors: 1, 2, 17, and 34.
    • In raindrop-speak, this would be "34".

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

A variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com

Submitting Incomplete Solutions

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

raindrops-test.lisp

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

(defpackage #:raindrops-test
  (:use #:common-lisp #:lisp-unit))

(in-package #:raindrops-test)

(define-test test-1
    (assert-equal "1"
                  (raindrops:convert 1)))

(define-test test-3
    (assert-equal "Pling"
                  (raindrops:convert 3)))

(define-test test-5
    (assert-equal "Plang"
                  (raindrops:convert 5)))

(define-test test-7
    (assert-equal "Plong"
                  (raindrops:convert 7)))

(define-test test-6
    (assert-equal "Pling"
                  (raindrops:convert 6)))

(define-test test-9
    (assert-equal "Pling"
                  (raindrops:convert 9)))

(define-test test-10
    (assert-equal "Plang"
                  (raindrops:convert 10)))

(define-test test-15
    (assert-equal "PlingPlang"
                  (raindrops:convert 15)))

(define-test test-21
    (assert-equal "PlingPlong"
                  (raindrops:convert 21)))

(define-test test-25
    (assert-equal "Plang"
                  (raindrops:convert 25)))

(define-test test-35
    (assert-equal "PlangPlong"
                  (raindrops:convert 35)))

(define-test test-49
    (assert-equal "Plong"
                  (raindrops:convert 49)))

(define-test test-52
    (assert-equal "52"
                  (raindrops:convert 52)))

(define-test test-105
    (assert-equal "PlingPlangPlong"
                  (raindrops:convert 105)))

(define-test test-12121
    (assert-equal "12121"
                  (raindrops:convert 12121)))

#-xlisp-test
(let ((*print-errors* t)
      (*print-failures* t))
  (run-tests :all :raindrops-test))
(defpackage #:raindrops
  (:use #:common-lisp)
  (:export #:convert))

(in-package #:raindrops)

(defun empty-string-alternative (s alternative)
  (if (zerop (length s)) alternative s)) 

(defun factor-p (n factor)
  (zerop (mod n factor)))

(defun convert (n)
  (empty-string-alternative
   (format nil "~A~A~A"
           (if (factor-p n 3) "Pling" "")
           (if (factor-p n 5) "Plang" "")
           (if (factor-p n 7) "Plong" ""))
   (format nil "~D" n)))

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?