python solution for question 4