solved problem 4 in ruby; kind of sloppy could use improvement