Finished problem 6 in Scala