You have 5 minutes to write code that validates credit card numbers, or the crow bites off a limb.
Creditcard numbers use the Luhn algorithm (look it up if confused), which works by doubling every even (2nd) digit starting from the right, then adding up all the odd digits and the resulting digits of the doubled even digits. The sum of all that should be a factor of 10 for a number to be valid. Go!
justify to me why this is an interesting problem because it is not at all by inspection. you should also provide sample input/output and use more precise wording.
Nolan Butler
Slightly harder mode: Generate a valid Luhn-valid card number without using a randomise-and-recheck-until-valid loop.
Jason Gutierrez
Can't be bothered to check if this is the correct regex for creditcards, but this. Checkmate crow.
Tyler Wood
youre a fucking puss op
Gavin Anderson
> They won't catch numbers with incorrect digits. For that, you need to follow the Luhn algorithm, which cannot be done with a regex.
Daniel Peterson
Oh, I overread this part, bc I'm a lazy pos. Still, use the regex to get the input, then validate afterwords. Fuck you anyways, crow.
Jose Peterson
def isValidAccount(accountNumber): nums = [int(n) for n in str(accountNumber)[::-1]] for i in [i for i in range(len(nums)) if i%2]: nums[i] *= 2 if nums[i] > 9: nums[i] -= 9 return not sum(nums) % 10
OP's explanation is wrong so the two solutions above are wrong. When you double a digit in an even place, you should subtract 9 if it is >9 or add up the digits, either way works.
Owen Peterson
func isValid(cc []int) bool { return true }
Sebastian Perry
>or add up the digits >OP: and the resulting digits of the doubled even digits Literally says.
Jason Jones
My solution adds up the digits that result from the doubling, you dingaling.
Owen Thomas
I'm not afraid of satan crow.
ok, maybe a little.
Brayden Gomez
No it would expand to "KYS Your Self urself"
Dominic Hughes
Your explanation was awful OP, I had to look it up to figure out what the hell it was userinput = input("Enter a credit card number: ") length = len(userinput) card = list(userinput) total = 0 for i in range(length): if i%2 == length%2: if int(card[i])*2 > 9: digit = int(card[i])*2-9 else: digit = int(card[i])*2 else: digit = int(card[i]) total += digit if total%10 == 0: print("Valid number") else: print("invalid number")
Nathaniel Parker
import Data.List import Data.Char
main = do putStrLn "Enter your credit card number faggot" ccnumber IO () cardChecker ccnum | isCCValid ccnum = putStrLn "Valid Card" | otherwise = putStrLn "Invalid Card!"
def luhnCheck(cc) even = false sum = 0 cc.to_s.reverse.chars.each do |c| if even (2*c.to_i).to_s.chars.each{|i|sum+=i.to_i} else sum += c.to_i end even=!even end sum%10==0 end
let mut sum = 0; for (i, n) in input.chars().enumerate() { sum += if i % 2 != 0 { n as i32 } else { luhn_doubler(n as i32) } }
println!( "This is a{}alid number", if sum % 10 == 0 { " v" } else { "n inv" } ); }
Jeremiah Jenkins
WOW EPIC
Christian Roberts
def luhn_check(numstr) numstr.chars .map(&:to_i) .reverse .map.with_index{ |e, i| i % 2 != 0 ? e > 4 ? e * 2 - 9 : e * 2 : e }.reduce(&:+) % 10 == 0 end
ruby is A E S T H E T I C
David Campbell
def cc_checker(num): c = 0 b = False for i in range(1, len(num)+1): a = int(num[-i]) if b: a *= 2 while a > 9: a -= 9 c += a b = not b return str(c)[-1] == '0'
Zachary Diaz
Would a crow make a good pet?
Lucas Ward
Crows are actually illegal to keep as pets in many places. Other than that they are incredibly mischievous and curious. They're like hyper-intelligent dogs that can fly.
Connor Ward
seagull has no mercy
Julian Foster
anyone know if this is right?
Ryder Peterson
(defun luhn-list-p (list) (loop for number in (reverse list) for x from 1 if (evenp x) summing (let ((sum (* 2 number))) (if (> sum 9) (- sum 9) sum)) into even-sums else if (oddp x) summing number into odd-sums finally (return (zerop (mod (+ even-sums odd-sums) 10)))))
Pst... you should learn Crystal. It's like Ruby, but it's statically typed and compiled. You lose a few of the odd hacks from Ruby's dynamic nature (e.g. no more method_missing), but you get to run an order of magnitude faster, and have no more dependence on a VM. Also, the GC is *technically* optional if you make freestanding applications, so you could theoretically make an OS out of it (not that you should).
Anthony Parker
def valid_ccn(ccn : String) : Bool return false if ccn.size != 16 ccn.reverse.each_char.map_with_index { |c,i| d = c.to_i i.odd?? d : d * 2 }.sum.divisible_by? 10 end
Crystal.
Juan Edwards
I'd fuck that crow.
Nathaniel Wilson
Sexy
Ian Johnson
>validates credit card numbers >Luhn algorithm
That doesn't validate credit card numbers. It just rules out some invalid ones, not all.
Justin Sanchez
>>> luhn = lambda c: sum([int(n) if (i % 2 == 1) else (int(n)*2 if int(n)*2 >> luhn('4561261212345467') True
one line
Thomas Hill
Doesn't add the digits resulting from the doubling.
Aaron White
You're usually allowed to have an African crow, which is like a regular crow but with a white stripe, because it's not an indigenous bird.
Nicholas Bennett
It failed on first try, with cccode 371449635398431, but when I changed the last digit to 2 it returned a 1. I can't quite decypther the routine to give any more detailed comments as I'm way too rusty on C. I don't even really know what i[xs] does.
Andrew Jackson
Crows are objectively the best bird
Alexander Reed
was it supposed to fail on that code?
Colton Thomas
Well, it failed the test. It was supposed validate it as a correct cc code.