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

AGE DESTRIBUTION(codeeval)

codeeval ruby

久しぶりにCodeEval のぞいたら、問題が増えていたのでやってみました。

CHALLENGE DESCRIPTION:

In this challenge, we are analyzing the ages of people to determine their status.
If the age is from 0 to 2 the child should be with parents at home, print : 'Home' 
If the age is from 3 to 4 the child should visit preschool, print : 'Preschool' 
If the age is from 5 to 11 the child should visit elementary school, print : 'Elementary school' 
From 12 to 14: 'Middle school' 
From 15 to 18: 'High school' 
From 19 to 22: 'College'
From 23 to 65: 'Work'
From 66 to 100: 'Retirement'
If the age of the person less than 0 or more than 100 - it might be an alien - print: "This program is for humans"

INPUT SAMPLE:

0
19

OUTPUT SAMPLE:

Home
College

回答

#!/usr/bin/env ruby -w
def age_destribution(age)
  case age
  when 0..2 then 'Home'
  when 3..4 then 'Preschool'
  when 5..11 then 'Elementary school'
  when 19..22 then 'College'
  when 23..65 then 'Work'
  when 66..100 then 'Retirement'
  else 'This program is for humans'
  end
end

ARGF.each_line do |line|
  puts age_destribution(line.chomp.to_i)
end