Published at Aug 20 2018
·
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.

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

```
gem install minitest
```

If you would like color output, you can `require 'minitest/pride'`

in
the test file, or note the alternative instruction, below, for running
the test file.

Run the tests from the exercise directory using the following command:

```
ruby luhn_test.rb
```

To include color from the command line:

```
ruby -r minitest/pride luhn_test.rb
```

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.

```
require 'minitest/autorun'
require_relative 'luhn'
# Common test data version: 1.4.0 4a80663
class LuhnTest < Minitest::Test
def test_single_digit_strings_can_not_be_valid
# skip
refute Luhn.valid?("1")
end
def test_a_single_zero_is_invalid
skip
refute Luhn.valid?("0")
end
def test_a_simple_valid_sin_that_remains_valid_if_reversed
skip
assert Luhn.valid?("059")
end
def test_a_simple_valid_sin_that_becomes_invalid_if_reversed
skip
assert Luhn.valid?("59")
end
def test_a_valid_canadian_sin
skip
assert Luhn.valid?("055 444 285")
end
def test_invalid_canadian_sin
skip
refute Luhn.valid?("055 444 286")
end
def test_invalid_credit_card
skip
refute Luhn.valid?("8273 1232 7352 0569")
end
def test_valid_number_with_an_even_number_of_digits
skip
assert Luhn.valid?("095 245 88")
end
def test_valid_strings_with_a_non_digit_included_become_invalid
skip
refute Luhn.valid?("055a 444 285")
end
def test_valid_strings_with_a_non_digit_added_at_the_end_become_invalid
skip
refute Luhn.valid?("059a")
end
def test_valid_strings_with_punctuation_included_become_invalid
skip
refute Luhn.valid?("055-444-285")
end
def test_valid_strings_with_symbols_included_become_invalid
skip
refute Luhn.valid?("055£ 444$ 285")
end
def test_single_zero_with_space_is_invalid
skip
refute Luhn.valid?(" 0")
end
def test_more_than_a_single_zero_is_valid
skip
assert Luhn.valid?("0000 0")
end
def test_input_digit_9_is_correctly_converted_to_output_digit_9
skip
assert Luhn.valid?("091")
end
def test_strings_with_non_digits_is_invalid
skip
refute Luhn.valid?(":9")
end
end
```

```
class Luhn
def self.valid?(input)
input.delete! ' '
return false if input.match?(/^.{1}$|\D/)
luhn = new(input)
(luhn.sum % 10).zero?
end
def initialize(number_input)
@numbers = number_input.reverse.each_char.map(&:to_i)
multiply_evens
subtract_nine
end
def multiply_evens
@numbers.map!.with_index do |n, i|
i.odd? ? n * 2 : n
end
end
def subtract_nine
@numbers.map! do |n|
n > 9 ? n - 9 : n
end
end
def sum
@numbers.sum
end
end
```

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,388 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments