Solved second problem in scala