Solve problem_2 in Python