Improve #7 Python solution