solution and tests for problem 3