Published at Apr 01 2020
·
0 comments

Instructions

Test suite

Solution

Given a number determine whether or not it is valid per the Luhn formula.

The Luhn algorithm is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers and Canadian Social Insurance Numbers.

The task is to check if a given string is valid.

Strings of length 1 or less are not valid. Spaces are allowed in the input, but they should be stripped before checking. All other non-digit characters are disallowed.

```
4539 1488 0343 6467
```

The first step of the Luhn algorithm is to double every second digit, starting from the right. We will be doubling

```
4_3_ 1_8_ 0_4_ 6_6_
```

If doubling the number results in a number greater than 9 then subtract 9 from the product. The results of our doubling:

```
8569 2478 0383 3437
```

Then sum all of the digits:

```
8+5+6+9+2+4+7+8+0+3+8+3+3+4+3+7 = 80
```

If the sum is evenly divisible by 10, then the number is valid. This number is valid!

```
8273 1232 7352 0569
```

Double the second digits, starting from the right

```
7253 2262 5312 0539
```

Sum the digits

```
7+2+5+3+2+2+6+2+5+3+1+2+0+5+3+9 = 57
```

57 is not evenly divisible by 10, so this number is not valid.

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

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 `~/.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.

The Luhn Algorithm on Wikipedia http://en.wikipedia.org/wiki/Luhn_algorithm

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 "luhn")
(defpackage #:luhn-test
(:use #:cl #:lisp-unit))
(in-package #:luhn-test)
(define-test single-digit-strings-can-not-be-valid
(assert-false (luhn:is-valid "1")))
(define-test a-single-zero-is-invalid
(assert-false (luhn:is-valid "0")))
(define-test a-simple-valid-sin-that-remains-valid-if-reversed
(assert-true (luhn:is-valid "059")))
(define-test a-simple-valid-sin-that-becomes-invalid-if-reversed
(assert-true (luhn:is-valid "59")))
(define-test a-valid-canadian-sin
(assert-true (luhn:is-valid "055 444 285")))
(define-test invalid-canadian-sin
(assert-false (luhn:is-valid "055 444 286")))
(define-test invalid-credit-card
(assert-false (luhn:is-valid "8273 1232 7352 0569")))
(define-test valid-strings-with-a-non-digit-included-become-invalid
(assert-false (luhn:is-valid "055a 444 285")))
(define-test valid-strings-with-punctuation-included-become-invalid
(assert-false (luhn:is-valid "055-444-285")))
(define-test valid-strings-with-symbols-included-become-invalid
(assert-false (luhn:is-valid "055£ 444$ 285")))
(define-test single-zero-with-space-is-invalid
(assert-false (luhn:is-valid " 0")))
(define-test more-than-a-single-zero-is-valid
(assert-true (luhn:is-valid "0000 0")))
(define-test input-digit-9-is-correctly-converted-to-output-digit-9
(assert-true (luhn:is-valid "091")))
(define-test strings-with-non-digits-is-invalid
(assert-false (luhn:is-valid ":9")))
#-xlisp-test
(let ((*print-errors* t)
(*print-failures* t))
(run-tests :all :luhn-test))
```

```
(in-package #:cl-user)
(defpackage #:luhn
(:use #:cl)
(:export #:is-valid))
(in-package #:luhn)
(defun only-digits-and-space-p (input)
""
(reduce #'(lambda (result character)
(and result
(or (digit-char-p character)
(char= #\ character))))
input))
(defun divides-p (n x)
"predicate, whether n divides x"
(= 0
(mod x n)))
(defun digits (input)
"transforms input string into list of digits"
(reduce #'(lambda (result character)
(if (digit-char-p character)
(append result
(list (digit-char-p character)))
result))
input
:initial-value '()))
(defun input-longer-than-1-p (input)
(> (list-length (digits input))
1))
(defun double-every-second-element (list)
"doubles every second element (from right) in list"
(labels ( (second-element-p (index list)
(divides-p 2
(- (list-length list)
index)))
(double (n)
(if (= n 9)
9
(mod (* n 2) 9))))
(loop for i from 0 to (1- (list-length list))
collect (if (second-element-p i list)
(double (nth i list))
(nth i list)))))
(defun luhn-number (input)
"computes check number of credit card number"
(reduce #'+
(double-every-second-element (digits input))))
(defun is-valid (input)
"predicate, whether input is a valid credit card number"
(if (and (input-longer-than-1-p input)
(only-digits-and-space-p input))
(divides-p 10
(luhn-number input))))
```

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?

Level up your programming skills with 3,373 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments