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

Roller Coaster(CodeEval)

CHALLENGE DESCRIPTION:

You are given a text. Your job is to write a program to set the case of text characters based on the following:

  1. First letter of the line should be upper case.
  2. Next letter should be lower case.
  3. Next letter should be upper case and so on...
  4. Any characters, except the letters, are ignored during determination of letters case.

INPUT SAMPLE:

The first argument will be a path to a filename containing sentences, one per line. You can assume all characters are from the english language. E.g.:

To be, or not to be: that is the question.
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
And by opposing end them? To die: to sleep.

OUTPUT SAMPLE:

Print to stdout, the RoLlErCoAsTeR case version of the string. E.g.:

To Be, Or NoT tO bE: tHaT iS tHe QuEsTiOn.
WhEtHeR 'tIs NoBlEr In ThE mInD tO sUfFeR
ThE sLiNgS aNd ArRoWs Of OuTrAgEoUs FoRtUnE,
Or To TaKe ArMs AgAiNsT a SeA oF tRoUbLeS,
AnD bY oPpOsInG eNd ThEm? To DiE: tO sLeEp.

Constraints:
The length of each utterance does not exceed 1000 characters

My Code

#!/usr/bin/env ruby -w

def roller_coaster(line)
  is_capital = true

  line.each_char.map { |c|
    next c unless c.match(/[a-zA-Z]/)
    c.upcase! if is_capital
    is_capital = !is_capital
    c
  }.join
end

ARGF.each_line do |line|
  print roller_coaster(line)
end