Solving problem 4 using python