One zero, two zeros(CodeEval)

2進数変換して、0の数を数える問題。少し凝った内容。
マネーロンダリングする悪のネットワークを見つけたとか、問題文はすごい方向へ向かっている。

CHALLENGE DESCRIPTION:

Our agent uncovered a global criminal money-laundering network that used offshore companies to defraud international organizations of total $1,000,000,000! The agent changes his location each hour, but he manages to send us the code that we need to decipher. Deciphering code includes many stages, and you are taking part in one of them. Therefore, your task is the following: you have two numbers – the first one is the number of zeros in a binary code and the second one shows the range from 1 to this number, where you have to find these zeros. For example, for the given numbers 2 and 4, you convert all numbers from 1 to 4 inclusive into the binary system. As a result, you get 1, 10, 11, and 100. As the first given number is 2, this means that we are looking for numbers with two zeros, so only 100 suits us. Hence, the result will be 1: there is only one number with two zeros.

INPUT SAMPLE:

The first argument is a path to a file. Each line includes a test case with two numbers: the first one is the number of zeros in a binary code that we need to find and the second one is the range from 1 to this number where you have to find these zeros.

1 8
2 4

OUTPUT SAMPLE:

Print the total number of numerals that contain the needed amount of zeros in a binary system.

3
1

CONSTRAINTS:

  1. Range can be from 5 to 1000.
  2. Number of zeros does not exceed the length of binary code number.
  3. The number of test cases is 40.

My Code

#!/usr/bin/env ruby -w

def binary_zero_count(number, max_decimal)
  bins = (1..max_decimal).map { |n| n.to_s(2) }
  bins.find_all { |b| b.scan("0").size == number }.size
end

ARGF.each_line do |line|
  number, max_decimal = line.chomp.split.map(&:to_i)
  puts binary_zero_count(number, max_decimal)
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