WITHOUT REPETITIONS(CODEEVAL)

すごく久しぶりにcodeevalやって見た。
繰り返し文字列を削除する問題。
愚直にできなくはないのですが、なんか便利メソッドないかと調べて見たら
そのものズバリの instance method String#squeeze (Ruby 2.4.0) というものがありました。

CHALLENGE DESCRIPTION:

In a given text, if there are two or more identical characters in sequence, delete the repetitions and leave only the first character.

For Example:

Shellless mollusk lives in wallless house in wellness. Aaaarrghh!

↓

Sheles molusk lives in wales house in welnes. Aargh!

INPUT SAMPLE:

But as he spake he drew the good sword from its scabbard, and smote a heathen knight, Jusssstin of thee Iron Valley.
No matttter whom you choose, she deccccclared, I will abide by your decision.
Wwwhat is your will?
At his magic speech the ground oppened and he began the path of descent.
I should fly away and you would never see me again.

OUTPUT SAMPLE:

But as he spake he drew the god sword from its scabard, and smote a heathen knight, Justin of the Iron Valey.
No mater whom you chose, she declared, I wil abide by your decision.
Wwhat is your wil?
At his magic spech the ground opened and he began the path of descent.
I should fly away and you would never se me again.

CONSTRAINTS:

  1. The text is case-sensitive: ‘a’ and ‘A’ are different characters.
  2. The input consists of 40 text lines.
  3. The maximum size of the text is 10 KB.

My Code

#!/usr/bin/env ruby -w

ARGF.each_line do |line|
  puts line.chomp.split(" ").map(&:squeeze).join(" ")
end

これだけ。