python solution for problem 29