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

LETTERCASE PERCENTAGE RATIO(CodeEval)

Your goal is to find the percentage ratio of lowercase and uppercase letters in line below.

INPUT SAMPLE

Your program should accept as its first argument a path to a filename. Each line of input contains a string with uppercase and lowercase letters E.g.:

thisTHIS
AAbbCCDDEE
N
UkJ

OUTPUT SAMPLE

lowercase: 50.00 uppercase: 50.00
lowercase: 20.00 uppercase: 80.00
lowercase: 0.00 uppercase: 100.00
lowercase: 33.33 uppercase: 66.67

My Code

#!/usr/bin/env ruby -w

def letteracase_percentage_ratio(letters)
  total_size = letters.size
  lower_size = 0
  letters.each_char do |char|
    lower_size += 1 if char.match(/[a-z]/)
  end

  lower_percent = (lower_size / total_size.to_f) * 100.0
  upper_percent = 100 - lower_percent
  "lowercase: %.2f uppercase: %.2f" % [lower_percent, upper_percent]
end

ARGF.each_line do |line|
  puts letteracase_percentage_ratio(line.chomp)
end