Last active
December 18, 2015 22:38
-
-
Save leeacto/5855619 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
| # Don't forget to check on intialization for a card length | |
| # of exactly 16 digits | |
| class CreditCard | |
| def initialize(cc) | |
| @cc_int = [] | |
| raise ArgumentError.new("CC Number is not 16 Digits") if cc.to_s.length != 16 | |
| cc.to_s.each_char { |ch| @cc_int << ch.to_i if ch != ' ' } | |
| end | |
| def check_card | |
| @cc_int.each_with_index { |val, index| index % 2 == 0 ? @cc_int[index]=val*2:@cc_int[index]=val } | |
| (@cc_int.inject(0) { |result, value| value >= 10 ? result + value.to_s[1].to_i + 1:(result + value) }) % 10 == 0 | |
| end | |
| end | |
| #VERSUS | |
| # Don't forget to check on intialization for a card length | |
| # of exactly 16 digits | |
| class CreditCard | |
| def initialize(cc) | |
| @cc_str = cc.to_s | |
| @cc_int = [] | |
| for i in 0...@cc_str.length | |
| @cc_int << @cc_str[i].to_i if @cc_str[i] != ' ' | |
| end | |
| raise ArgumentError.new("CC Number is not 16 Digits") if @cc_str.length != 16 | |
| end | |
| def check_card | |
| step_1 = @cc_int | |
| for i in 0..15 | |
| step_1[i] = step_1[i]*2 if i % 2 == 0 | |
| end | |
| runsum = 0 | |
| for i in 0..15 | |
| if step_1[i] >= 10 | |
| runsum += step_1[i] % 10 + 1 | |
| else | |
| runsum += step_1[i] | |
| end | |
| end | |
| runsum % 10 == 0 | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment