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

Testing(CodeEval)

文字列の差を求める問題。

CHALLENGE DESCRIPTION:

In many teams, there is a person who tests a project, finds bugs and errors, and prioritizes them. Now, you have the unique opportunity to try yourself as a tester and test a product. Here, you have two strings - the first one is provided by developers, and the second one is mentioned in design. You have to find and count the number of bugs, and also prioritize them for fixing using the following statuses: ‘Low’, ‘Medium’, ‘High’, ‘Critical’ or ‘Done’.

INPUT SAMPLE:

The first argument is a path to a file. Each line includes a test case with two strings separated by a pipeline ‘|’. The first string is the one the developers provided to you for testing, and the second one is from design.

Heelo Codevval | Hello Codeeval
hELLO cODEEVAL | Hello Codeeval
Hello Codeeval | Hello Codeeval

OUTPUT SAMPLE:

Write a program that counts the number of bugs and prioritizes them for fixing using the following statuses:
‘Low’ - 2 or fewer bugs;
‘Medium’ - 4 or fewer bugs;
‘High’ - 6 or fewer bugs;
‘Critical’ - more than 6 bugs;
‘Done’ - all is done;

Low
Critical
Done

CONSTRAINTS:

  1. Strings are of the same length from 5 to 40 characters.
  2. Upper and lower case matters.
  3. The number of test cases is 40.

My Code

#!/usr/bin/env ruby -w

def test(actual, expected)
  wrong_count = 0
  expected.size.times { |i| wrong_count += 1 unless actual[i] == expected[i] }
  case
  when wrong_count == 0 then "Done"
  when wrong_count <= 2 then "Low"
  when wrong_count <= 4 then "Medium"
  when wrong_count <= 6 then "High"
  else "Critical"
  end
end

ARGF.each_line do |line|
  actual, expected = line.chomp.split(" | ").map { |l| l.split("") }
  puts test(actual, expected)
end

Black Card(CodeEval)

ババ抜きのような海賊ゲーム。

CHALLENGE DESCRIPTION:

You must have heard about pirates, their customs, pirates code, and the “black spot”. If a pirate is presented with a “black spot”, he is officially pronounced guilty, meaning he will soon be expulsed from the pirate brotherhood or even be dead.
We don’t have as strict rules as pirates have, and a person who receives a black spot simply leaves the game.
For example, we have a list of three players: John, Tom, Mary, and a number 5. Starting with the first player (in our case, it’s John), we start to count all players: John – 1, Tom – 2, Mary – 3, and then again starting from the first one John – 4, Tom – 5. As Tom gets number 5, he should leave. Now, we have John and Mary and start counting again. John gets number 5, so he leaves. Thus, the winner is Mary.

INPUT SAMPLE:

The first argument is a path to a file. Each line includes a test case with names of players and a number for a “black spot”. Players and a number are separated by a pipeline ‘|’.

John Sara Tom Susan | 3
John Tom Mary | 5

OUTPUT SAMPLE:

Print the name of a winner.

Sara
Mary

CONSTRAINTS:

  1. Always start counting from the first name in a list.
  2. Number of players can be from 3 to 10.
  3. Number of turns can be from 3 to 15.
  4. The number of test cases is 40.

My Code

#!/usr/bin/env ruby -w

def black_card(names, number)
  while names.size > 1
    names.delete_at((number - 1) % names.size)
  end
  names[0]
end

ARGF.each_line do |line|
  name_str, number_str = line.chomp.split(" | ")
  names = name_str.split
  number = number_str.to_i
  puts black_card(names, number)
end