Codility Test Solution in Scala