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

String Mask(CODEEVAL)

単語内の指定された文字だけ大文字に変更する問題。

CHALLENGE DESCRIPTION:

You’ve got a binary code which has to be buried among words in order to unconsciously pass the cipher. Create a program that would cover the word with a binary mask. If, while covering, a letter finds itself as 1, you have to change its register to the upper one, if it’s 0, leave it as it is. Words are always in lower case and in the same row with the binary mask.

INPUT SAMPLE:

The first argument is a path to a file. Each row contains a test case with a word and a binary code separated with space, inside of it. The length of each word is equal to the length of the binary code.

hello 11001
world 10000
cba 111

OUTPUT SAMPLE:

HEllO
World
CBA

CONSTRAINTS:

  1. Words are from 1 to 20 letters long.
  2. The number of test cases is 40.

My Code

#!/usr/bin/env ruby -w
def decode(str, binary_mask)
  binary_mask.each_char.with_index do |b, i|
    str[i] = str[i].upcase if b == "1"
  end
  str
end

ARGF.each_line do |line|
  str, binary_mask = line.chomp.split
  puts decode(str, binary_mask)
end