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

SLANG FLAVOR(CODEEVAL)

句読点を変換する問題。
1個飛ばしで変換させるのが面倒。

CHALLENGE DESCRIPTION:

Long serious texts are boring. Write a program that will make texts more informal: replace every other end punctuation mark (period ‘.’, exclamation mark ‘!’, or question mark ‘?’) with one of the following slang phrases, selecting them one after another:

  1. ‘, yeah!’
  2. ‘, this is crazy, I tell ya.’
  3. ‘, can U believe this?’
  4. ‘, eh?’
  5. ‘, aw yea.’
  6. ‘, yo.’
  7. ‘? No way!’
  8. ‘. Awesome!’

INPUT SAMPLE:

Lorem ipsum dolor sit amet. Mea et habeo doming praesent. Te inani utroque recteque has, sea ne fugit verterem!
Usu ei scripta phaedrum, an sed salutatus definiebas? Qui ut recteque gloriatur reformidans. Qui solum aeque sapientem cu.
Eu nam nusquam quaestio principes.

OUTPUT SAMPLE:

Lorem ipsum dolor sit amet. Mea et habeo doming praesent, yeah! Te inani utroque recteque has, sea ne fugit verterem!
Usu ei scripta phaedrum, an sed salutatus definiebas, this is crazy, I tell ya. Qui ut recteque gloriatur reformidans. Qui solum aeque sapientem cu, can U believe this?
Eu nam nusquam quaestio principes.

CONSTRAINTS:

  1. In the input text, end punctuation mark cannot come one after another (consequent).
  2. Input text contains 40 lines.

My Code

#!/usr/bin/env ruby -w
class SlangFlavor
  SLANGS = [
    ", yeah!",
    ", this is crazy, I tell ya.",
    ", can U believe this?",
    ", eh?",
    ", aw yea.",
    ", yo.",
    "? No way!",
    ". Awesome!"
  ]
  SLANGS_SIZE = SLANGS.size.freeze

  def initialize
    @called_count = 0
  end

  def replace(sentence)
    @called_count += 1
    return sentence if @called_count.odd?

    slang_index = (@called_count / 2) % SLANGS_SIZE - 1
    "#{sentence[0..-2]}#{SLANGS[slang_index]}"
  end
end

slang_flavor = SlangFlavor.new
ARGF.each_line do |line|
  sentences = line.chomp.scan(/.+?[.!?]/)
  puts sentences.map { |sentence| slang_flavor.replace(sentence) }.join
end