Add solution for problem 8 in Python