My own homework 5. Works in Python 2.7