読者です 読者をやめる 読者になる 読者になる

Real Fake(CodeEval)

Credit Card のcheck tool。
実際のものではなく、架空のもの。グループごとに1, 3 番目の数字を倍にして、足し上げた結果10で割り切れれば本物と判定する。

CHALLENGE DESCRIPTION:

The police caught a swindler with a big pile of credit cards. Some of them are stolen and some are fake. It would take too much time to determine which ones are real and which are fake, so you need to write a program to check credit cards.
To determine which credit cards are real, double every third number starting from the first one, add them together, and then add them to those figures that were not doubled. If the total sum of all numbers is divisible by 10 without remainder, then this credit card is real.

INPUT SAMPLE:

9999 9999 9999 9999
9999 9999 9999 9993

OUTPUT SAMPLE:

Fake
Real

CONSTRAINTS:

  1. The credit card number is 16 digits in length.
  2. The number of test cases is 40.

My Code

#!/usr/bin/env ruby -w

def real_card?(card_number)
  total = card_number.split.inject(0) do |sum, grouped_number|
    numbers = grouped_number.split("").map(&:to_i)
    sum += numbers.inject(&:+) + numbers[0] + numbers[2]
  end
  total % 10 == 0
end

ARGF.each_line do |line|
  puts real_card?(line.chomp) ? "Real" : "Fake"
end