Credit Card のcheck tool。
実際のものではなく、架空のもの。グループごとに1, 3 番目の数字を倍にして、足し上げた結果10で割り切れれば本物と判定する。
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.
9999 9999 9999 9999 9999 9999 9999 9993
- The credit card number is 16 digits in length.
- The number of test cases is 40.
#!/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 + numbers end total % 10 == 0 end ARGF.each_line do |line| puts real_card?(line.chomp) ? "Real" : "Fake" end