solution for problem 4 + tests