2

I'm working on a codewars kata that uses the Luhn Algorithm to validate credit card numbers. Already have my answer, which uses string conversion to split the digits and then reconvert to an integer and then use the rest of the algorithm. All well and good until I test it on an octal number.

The code starts as:

def validate(n)
 n.to_s.split("") #n = 7867 gives me ["7","8","6","7"], which is fine
 n.to_s.split("") #n = 0776 gives me ["5","1","0], when I need ["0","7","7","6"]
 n.to_s.split("") #n = 0100 gives me ["6", "4"] when I need ["0","1","0","0"]
#other code here
end

where the method should be called on whatever n is plugged in.

How do I prevent an octal, binary, or hexidecimal, etc number from converting like that? Is there a way to keep the digits as is so I can use them?

4
  • 2
    Where do you get value of n from? Commented Jan 15, 2016 at 19:02
  • n is just whatever number gets plugged into the method. def validate(n) Commented Jan 15, 2016 at 19:03
  • 1
    I mean how do you get octal number? Are you entering numeric literals in your program? Commented Jan 15, 2016 at 19:04
  • I was just selecting numbers at random to plug in to test my program. I didn't know what was going on whenever I put in a number that stated with 0 until I plugged in 0905 and the error read invalid octal number and I learned octals were even a thing. Commented Jan 15, 2016 at 19:10

2 Answers 2

1

Your problem is that you are obviously storing the number as a number, in n; or else you would not need to say n.to_s.

There must be a place where those numbers enter your program; a user prompt, or by reading a file or whatever. You likely have something like n = user_input.to_i somewhere, forcing ruby to convert the string to a number. Make sure that you treat n as string everywhere, and leading zeroes will stick around.

Sign up to request clarification or add additional context in comments.

3 Comments

That's part of the kata. I edited the question to make that clear. This was just part of a method that messed up the outcome. So, would it work to convert 'n' to an integer, then a string to split up?
That's missing the point. If you want to process leading zeroes, you cannot convert n to a numeric type - none of them keep zeroes around. Your problem is not in the method you are posting here, it is in the place where n is coming from.
Oh I understand. Thanks!
0

If you are using numeric literals in your program, you can explicitly say that they are decimal:

n = 0d0100 # or "0100".to_i
n.to_s.rjust(4,"0").split("")
#=> ["0", "1", "0", "0"]

Also, use rjust to make them 4 digits long in string form as credit/debit cards have four 4-digit numbers


To split credit card into an array of integer, you could do something like below:

cc = "4111 1111 1111 1111".gsub(/ /,"").split("").map(&:to_i)
#=> [4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

2 Comments

OH I see what you meant by numeric literals (I'm not mathematically minded at all) They're not floats or decimals. They're just multidigit integers.
@siouxsie, they're not even integers, they are just strings consisting of digits (and probably spaces would be nice as well). At no point anywhere is it appropriate to convert credit card numbers to or from integer or other number types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.