brundleti's solution

to Luhn in the Common Lisp Track

Published at Feb 25 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.

Validating a Number

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.

Example 1: valid credit card number

``````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!

Example 2: invalid credit card number

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

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

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

Submitting Incomplete Solutions

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

luhn-test.lisp

``````(ql:quickload "lisp-unit")

(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")))

(assert-true (luhn:is-valid "055 444 285")))

(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-integers-p (value)
(every #'(lambda (x) (digit-char-p x)) value))

(defun rescale (n)
(if (> n 9)
(- n 9)
n))

(defun is-valid (input)
;;we first remove spaces
(let* ((value (remove-if-not #'(lambda(x) (char/= x #\Space)) input))
(size (length value)))
;;test all characters of the string can be interpreded as an integer and a length >1
(when (and (only-integers-p value) (> size 1))
(do ((cnt (1- size) (1- cnt))
(tmp nil))
((minusp cnt) (zerop (rem (reduce #'+ tmp) 10)))
(if (oddp (- (1- size) cnt))
(push  (rescale (* 2 (digit-char-p (elt value cnt)))) tmp)
(push (digit-char-p (elt value cnt)) tmp))))))``````