Solution code for problem 4