Last active
December 9, 2015 23:32
-
-
Save dbernheisel/584cce196d1b903659b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 1. From the rightmost digit, which is the check digit, moving left, double the | |
| # value of every second digit; if the product of this doubling operation is | |
| # greater than 9 (e.g., 8 × 2 = 16), then sum the digits of the products | |
| # (e.g., 16: 1 + 6 = 7, 18: 1 + 8 = 9). | |
| # 2. Take the sum of all the digits. | |
| # 3. If the total modulo 10 is equal to 0 (if the total ends in zero) then the | |
| # number is valid according to the Luhn formula; else it is not valid. | |
| def lunh_valid?(num) | |
| numbers = num.to_s.chars.map(&:to_i) | |
| i = 0 | |
| sum = 0 | |
| # 1. From the rightmost digit, which is the check digit, moving left, | |
| numbers.reverse.each do |n| | |
| # double the value of every second digit; | |
| n *= 2 if i.odd? | |
| # if the product of this doubling operation is greater than 9, then | |
| # sum the digits of the products | |
| n = 1 + (n - 10) if n >= 10 | |
| # 2. Take the sum of all the digits. | |
| sum += n | |
| i += 1 | |
| end | |
| # 3. If the total modulo 10 is equal to 0, then it's valid. | |
| sum % 10 == 0 | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require './lunh.rb' | |
| def get_number | |
| gets.chomp! | |
| end | |
| puts "Please enter a number to check using the Luhn Algorithm" | |
| num = get_number | |
| puts("Usage: ruby lunh.rb {thenumber}") && exit if num.nil? | |
| valid = lunh_valid?(num) | |
| puts "\e[1A#{num} is #{"not " unless valid}valid" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'minitest' | |
| require 'minitest/autorun' | |
| require './lunh.rb' | |
| class TestAlgorithm < Minitest::Test | |
| describe "Lunh Algorithm" do | |
| good_test_credit_card_numbers = [ | |
| 378282246310005, | |
| 371449635398431, | |
| 378734493671000, | |
| 5610591081018250, | |
| 30569309025904, | |
| 38520000023237, | |
| 3530111333300000, | |
| 5105105105105100, | |
| 4222222222222 | |
| ] | |
| bad_test_credit_card_numbers = [ | |
| 6011111111111110, | |
| 3566002020360500, | |
| 4111111111111110, | |
| 76009244561, | |
| 5019717010103740, | |
| 6331101999990010, | |
| 6011000990139420, | |
| 5555555555554440, | |
| 4012888888881880 | |
| ] | |
| it "returns true for valid numbers" do | |
| good_test_credit_card_numbers.each do |n| | |
| assert lunh_valid?(n) == true | |
| end | |
| end | |
| it "returns false for invalid numbers" do | |
| bad_test_credit_card_numbers.each do |n| | |
| assert lunh_valid?(n) == false | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment