Problem 31 (python): solved