Solve #119 in scala