solved problem 27 in ruby