 paulfioravanti's solution

to Luhn in the Ruby Track

Published at Jul 13 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.

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.

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

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

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

skip
assert Luhn.valid?("055 444 285")
end

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

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
# frozen_string_literal: true

module Luhn
TWO_OR_MORE_DIGITS_ONLY = ->(string) { string.match?(/\A\d{2,}\z/) }
private_constant :TWO_OR_MORE_DIGITS_ONLY
CHECK_IF_EVENLY_DIVISIBLE_BY_10 = ->(number) { (number % 10).zero? }
private_constant :CHECK_IF_EVENLY_DIVISIBLE_BY_10
# NOTE: to_i here will ensure that any potential `nil` values convert to 0
DOUBLE = ->(number) { number.to_i * 2 }
private_constant :DOUBLE
STRIP_SPACES = ->(string) { string.gsub(/\s/, "") }
private_constant :STRIP_SPACES
SUBTRACT_9_IF_GREATER_THAN_9 = ->(number) { number > 9 ? number - 9 : number }
private_constant :SUBTRACT_9_IF_GREATER_THAN_9

module_function

def valid?(string)
stripped_string = STRIP_SPACES.call(string)
return false unless TWO_OR_MORE_DIGITS_ONLY.call(stripped_string)

stripped_string
.then(&method(:convert_string_to_reversed_numbers))
.each_slice(2)
.sum(&method(:calculate_pair))
.then(&CHECK_IF_EVENLY_DIVISIBLE_BY_10)
end

def convert_string_to_reversed_numbers(string)
string.reverse.chars.map(&:to_i)
end
private_class_method :convert_string_to_reversed_numbers

def calculate_pair((left_value, right_value))
right_luhn_value =
right_value
.then(&DOUBLE)
.then(&SUBTRACT_9_IF_GREATER_THAN_9)

left_value + right_luhn_value
end
private_class_method :calculate_pair
end