Skip to content

Instantly share code, notes, and snippets.

@danielevans
Created February 26, 2013 05:49
Show Gist options
  • Save danielevans/5036202 to your computer and use it in GitHub Desktop.
Save danielevans/5036202 to your computer and use it in GitHub Desktop.
$stdin.read.lines.each do |line|
number = line.strip
luhn = line.strip.chars.to_a.reverse.each_with_index.map do |v,i|
val = v.to_i
if (i - 1) % 2 == 0
(val * 2).to_s.each_char.reduce(0) { |memo,n| memo + n.to_i }
else
val
end
end.reduce(&:+)
valid = "FALSE"
valid = "TRUE" if luhn % 10 == 0 && ["51", "52", "53", "54", "55", "4"].any? { |s| number.start_with?(s) }
puts "#{number},#{valid}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment