Hurry!

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!

Other urls found in this thread:

youtu.be/vwGwgpGnRoQ
twitter.com/NSFWRedditVideo

Can I use php?

import creditcard
Decode()

Done.

kys urslef

Yes, but the crow will then bite off a finger anyway.

Just use the regex:

^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$

fite me crow

Don't bring a knife to a beak fight, user crow.

LETS FUCKING GO

>kill yourself yourself

We're not doing your HW op.

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.

Slightly harder mode: Generate a valid Luhn-valid card number without using a randomise-and-recheck-until-valid loop.

Can't be bothered to check if this is the correct regex for creditcards, but this.
Checkmate crow.

youre a fucking puss op

> They won't catch numbers with incorrect digits. For that, you need to follow the Luhn algorithm, which cannot be done with a regex.

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.

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

youtu.be/vwGwgpGnRoQ

Post some raven pics pls

My favorite raven

Magpie is best corvid. Your taste is shit.

thats not even a raven

You shut your fucking mouth.

I think I made the most retarded and inefficient version.
>Muh funcshional programmen

function validateLuhn(num) {
const sum = (a,b) => a + b;
const makeNumArray = (number) => Array.from(number.toString()).map(Number);
const numArray = makeNumArray(num).reverse();

const doubleEvens = numArray.filter((val, index) => index % 2).map((val) => val * 2);
const odds = numArray.filter((val, index) => index % 2 === 0).map(Number);
const checksum = odds.reduce(sum) + doubleEvens.map((doubleEven) => makeNumArray(doubleEven).reduce(sum)).reduce(sum);

return Number.isInteger(checksum / 10);
}

:(){ :|: & };:

Is this thread here because people are seeing a lot of Crows? In the Pittsburgh area there were like 6 that past by my house.

its just me posting the crows

int isValid(char *xs)
{
int sum = 0; for(int i=0; i[xs];
sum+=atoi(xs+i)*(++i%2?1:2));
return !(sum%10);
}

const validateCC = (ccString) => {
const sum = (nums) => nums.reduce((acc, cur) => acc + cur, 0);
const splitIntoNumbers = (num) => String(num).split('').map(Number);
return splitIntoNumbers(ccString).reverse().reduce(
(total, current, index) => total + (
index % 2 === 0 ? current : sum(splitIntoNumbers(num * 2))
)
, 0) % 10 === 0;
}

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.

func isValid(cc []int) bool {
return true
}

>or add up the digits
>OP: and the resulting digits of the doubled even digits
Literally says.

My solution adds up the digits that result from the doubling, you dingaling.

I'm not afraid of satan crow.

ok, maybe a little.

No it would expand to "KYS Your Self urself"

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")

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!"

isCCValid :: [Char] -> Bool
isCCValid ccnum = luhn `mod` 10 == 0
where
ccint = reverse $ map digitToInt ccnum
luhn = sum $ luhnAlgorithm ccint

luhnAlgorithm :: [Int] -> [Int]
luhnAlgorithm [] = []
luhnAlgorithm [x] = [x]
luhnAlgorithm (x : y : xs) = x : (2*y `mod` 10) + (2*y `div` 10) : luhnAlgorithm xs


Ok w/e

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

valids = [5105105105105100,
5555555555554444,
4222222222222,
4111111111111111,
4012888888881881,
378282246310005,
371449635398431,
378734493671000,
38520000023237,
3530111333300000,
3566002020360505]

valids.each {|cc|puts "#{cc} #{luhnCheck(cc)}"}

invalids = [510510514510510,
5553453455554444,
45523423944322]

invalids.each {|cc|puts "#{cc} #{luhnCheck(cc)}"}
Grabbed some test CC numbers from inet, and put a quick test together too.

Took me more than 5 minutes actually. I'm new to Rust
use std::io;

fn luhn_doubler(x: i32) -> i32
{
let mut result: i32 = 2 * x;
loop {
result = result % 10 + (result - result % 10) / 10;
if result < 10 {
break;
}
}
result
}


fn main()
{
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap_or_default();
input = input.trim().into();

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" }
);
}

WOW EPIC

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

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'

Would a crow make a good pet?

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.

seagull has no mercy

anyone know if this is right?

(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)))))

(defun number-sequence (digits)
(let ((number-string (format nil "~d" digits)))
(map 'list
#'(lambda (d)
(- (char-int d) 48))
number-string)))

(defun luhnp (number)
(luhn-list-p (number-sequence number)))

(luhnp 79927398713) ;; T

brainlets.

--[----->+---.-[--->+----.-------------.-.+++++.+++++++++++.[---->++++.+[->+++.--.--[--->+---.--------------.-[--->+-.+[----->++.+++++++.--------.-----------.+++.+++++++++++++.[-->++++++++.--[->++++--.+[->+++.+++++++++++++.---------.---.+++.----.+++++++++++++.

>doesn't even work

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

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.

I'd fuck that crow.

Sexy

>validates credit card numbers
>Luhn algorithm

That doesn't validate credit card numbers. It just rules out some invalid ones, not all.

>>> luhn = lambda c: sum([int(n) if (i % 2 == 1) else (int(n)*2 if int(n)*2 >> luhn('4561261212345467')
True

one line

Doesn't add the digits resulting from the doubling.

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.

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.

Crows are objectively the best bird

was it supposed to fail on that code?

Well, it failed the test. It was supposed validate it as a correct cc code.