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

MIXED CONTENT(CodeEval)

CHALLENGE DESCRIPTION:

You have a string of words and digits divided by comma. Write a program which separates words with digits. You shouldn't change the order elements.

INPUT SAMPLE:

Your program should accept as its first argument a path to a filename. Input example is the following

8,33,21,0,16,50,37,0,melon,7,apricot,peach,pineapple,17,21
24,13,14,43,41

OUTPUT SAMPLE:

melon,apricot,peach,pineapple|8,33,21,0,16,50,37,0,7,17,21
24,13,14,43,41

As you cas see you need to output the same input string if it has words only or digits only.

My Code

#!/usr/bin/env ruby -w

def separate_digits_words(args)
  words, digits = [], []

  args.each do |arg|
    if digit?(arg)
      digits << arg
    else
      words << arg
    end
  end
  format(words, digits)
end

def format(words, digits)
  res = words.join(',')
  res << '|' if !words.empty? && !digits.empty?
  res << digits.join(',')
end

def digit?(str)
  str.to_i.to_s == str
end

ARGF.each_line do |line|
  args = line.chomp.split(',')
  puts separate_digits_words(args)
end