Scala solution for problem 35.