 # davearonson's solution

## to Luhn in the Elixir Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.

## Running tests

Execute the tests with:

``````\$ elixir luhn_test.exs
``````

### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.

For example:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

For more detailed information about the Elixir track, please see the help page.

## 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.exs

``````if !System.get_env("EXERCISM_TEST_EXAMPLES") do
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule LuhnTest do
use ExUnit.Case

test "single digit strings can not be valid" do
refute Luhn.valid?("1")
end

@tag :pending
test "A single zero is invalid" do
refute Luhn.valid?("0")
end

@tag :pending
test "a simple valid SIN that remains valid if reversed" do
assert Luhn.valid?("059")
end

@tag :pending
test "a simple valid SIN that becomes invalid if reversed" do
assert Luhn.valid?("59")
end

@tag :pending
test "a valid Canadian SIN" do
assert Luhn.valid?("055 444 285")
end

@tag :pending
refute Luhn.valid?("055 444 286")
end

@tag :pending
test "invalid credit card" do
refute Luhn.valid?("8273 1232 7352 0569")
end

@tag :pending
test "valid strings with a non-digit included become invalid" do
refute Luhn.valid?("055a 444 285")
end

@tag :pending
test "valid strings with punctuation included become invalid" do
refute Luhn.valid?("055-444-285")
end

@tag :pending
test "valid strings with symbols included become invalid" do
refute Luhn.valid?("055£ 444\$ 285")
end

@tag :pending
test "single zero with space is invalid" do
refute Luhn.valid?(" 0")
end

@tag :pending
test "more than a single zero is valid" do
assert Luhn.valid?("0000 0")
end

@tag :pending
test "input digit 9 is correctly converted to output digit 9" do
assert Luhn.valid?("091")
end
end``````
``````defmodule Luhn do
@doc """
Calculates the total checksum of a number
"""
@spec checksum(String.t()) :: integer
def checksum(number_as_string) do
number_as_string
|> String.graphemes
|> Enum.map(&String.to_integer &1)
|> Enum.reverse
|> do_checksum(0)
end

defp do_checksum([single|[double|more]], acc) do
do_checksum(more, acc + double_value(double) + single)
end
defp do_checksum([single], acc), do: acc + single
defp do_checksum([],       acc), do: acc

defp double_value(digit) when digit < 5, do: digit * 2
defp double_value(digit)               , do: digit * 2 - 9

@doc """
Checks if the given number is valid via the luhn formula
"""
@spec valid?(String.t()) :: boolean
def valid?(number_as_string) do
rem(checksum(number_as_string), 10) == 0
end

@doc """
Creates a valid number by adding the correct
checksum digit to the end of the number
"""
@spec create(String.t()) :: String.t()
def create(number_as_string) do
"#{number_as_string}#{check_digit(number_as_string)}"
end

defp check_digit(number_as_string) do
make_non_negative(-rem(checksum("#{number_as_string}0"), 10))
end

defp make_non_negative(0), do: 0
defp make_non_negative(n), do: n + 10

end``````