Sollution for problem 4 in Python