Solved #4 in ruby.