solved problem 7 in ruby