Trick or Treat(CodeEval)

一定ルールで数値計算して平均値を求める問題。

CHALLENGE DESCRIPTION:

Everyone knows what Halloween is and how children love it. Children in costumes travel from house to house asking for treats with a phrase “Trick or treat”. After that, they divide the treats equally among all. This year, they collected tons of candies, and need your help to share everything equally. You know that children receive different number of candies depending on their costume: vampire gets 3 candies from one house, zombie – 4 candies, and witch – 5 candies. That is, three children in three different costumes get 3+4+5=12 candies from one house.

INPUT SAMPLE:

The first argument is a path to a file. Each line includes a test case with number of vampires, zombies, witches, and houses that they visited.

Vampires: 1, Zombies: 1, Witches: 1, Houses: 1
Vampires: 3, Zombies: 2, Witches: 1, Houses: 10

OUTPUT SAMPLE:

You need to print number of candies that each child will get. If the number is not integer, round it to the lower: for example, if the resulting number is 13.666, round it to 13.

4
36

CONSTRAINTS:

  1. Number of vampires, zombies, witches, and houses can be from 0 to 100.
  2. If the final number of candies is not integer, round it to the lower.
  3. The number of test cases is 40.

My Code

#!/usr/bin/env ruby -w

ARGF.each_line do |line|
  line.chomp.split(",").map { |pair| pair.split(":") }
  v, z, w, h = line.chomp.split(",").map { |pair| pair.split(":")[1].strip.to_i }
  children_number = (v + z + w)
  trick_number = h * (v * 3 + z * 4 + w * 5)
  puts trick_number / children_number
end

Time to eat(CODEEVAL)

猫の餌やり時間を逆順でソートする問題。
凝っているのは問題文だけで、実装はつまらない。

CHALLENGE DESCRIPTION:

It’s amazing how fast time flies by and we don’t even realize it. As we are getting older, time seems to move so much faster than it did in the past. Perception of time is relative to how fast our heart rate is. An unborn baby’s heart rate is incredibly fast and as we age, it slows. Metabolic rate of an organism determines its perception of time. Organisms like cats and dogs have a very high metabolic rate; thus, the breaks between their meals seem to be shorter than humans have. At codeeval.com, we have a cat called Kitty. She loves eating, and it seems like she can eat anything any time. We want to keep Kitty fit, so we feed her according to a schedule. Planning her daily meals, we need to see when Kitty will eat starting with the latest meal in the evening to the earliest morning snack. So, your task is to sort timestamps in the schedule in a reverse chronological order.

INPUT SAMPLE:

The first argument is a path to a file. Each line includes a test case: a schedule containing unsorted timestamps in HH:MM:SS format.

02:26:31 14:44:45 09:53:27
05:33:44 21:25:41

OUTPUT SAMPLE:

Sort timestamps in each schedule from the biggest to the smallest one.

14:44:45 09:53:27 02:26:31
21:25:41 05:33:44

CONSTRAINTS:

  1. Each schedule may have from 2 to 20 timestamps.
  2. Timestamp 23:59:59 - biggest and 00:00:01 the smallest one.
  3. The number of test cases is 40.

My Code

#!/usr/bin/env ruby -w

ARGF.each_line do |line|
  timestamps = line.chomp.split.sort.reverse
  puts timestamps.join(" ")
end

CHARDONNAY OR CABERNET (CODEEVAL)

だんだん問題文が物語風になってきました。
文字列の中に文字列の断片が含まれるかどうかを探索する問題。
当初正規表現で書きましたが、どうもscoreが100にならないので
1文字ずつ探索しては置換する方法で実装。

CHALLENGE DESCRIPTION:

Your good friend Tom is admirer of tasting different types of fine wines. What he loves even more is to guess their names. One day, he was sipping very extraordinary wine. Tom was sure he had tasted it before, but what was its name? The taste of this wine was so familiar, so delicious, so pleasant… but what is it exactly? To find the answer, Tom decided to taste the wines we had. He opened wine bottles one by one, tasted different varieties of wines, but still could not find the right one. He was getting crazy, “No, it’s not that!” desperately breaking a bottle of wine and opening another one. Tom went off the deep end not knowing what this wine was. Everything he could say is just several letters of its name. You can no longer look at it and decided to help him. Your task is to write a program that will find the wine name, containing all letters that Tom remembers.

INPUT SAMPLE:

The first argument is a path to a file. Each line includes a test case, which contains names of wines and letters that Tom remembers. Names and letters are separated by a vertical bar ‘|’.

Cabernet Merlot Noir | ot
Chardonnay Sauvignon | ann
Shiraz Grenache | o

OUTPUT SAMPLE:

You should print wine names, containing all letters that Tom remembered. Letters can be anywhere in wine names. If there is no name with all letters, print False.

Merlot
Chardonnay Sauvignon
False

CONSTRAINTS:

  1. Wine name length can be from 2 to 15 characters.
  2. Number of letters that Tom remembered does not exceed 5.
  3. Number of wine names in a test case can be from 2 to 10.
  4. If there is no wine name containing all letters, print False.
  5. The number of test cases is 40.

My Code

#!/usr/bin/env ruby -w

def find_candidates(wines, keyword)
  # 正規表現で実装したが、score 100 にならなかった
  # pattern = /#{keyword.split("").join("(.*?)")}/i
  # wines.find_all { |wine| wine =~ pattern }

  wines.inject([]) do |candidates, wine|
    wine_str = wine.dup.downcase
    candidates << wine if keyword.each_char.all? { |char| wine_str.sub!(char, "") }
    candidates
  end
end

ARGF.each_line do |line|
  str_wines, keyword = line.chomp.split(" | ")
  wines = str_wines.split
  candidates = find_candidates(wines, keyword)
  puts (candidates.empty? ? "False" : candidates.join(" "))
end