Made solution #3 more pythonic