Added a Python solution using a class.

This solution avoids the state problems that the other function based
solutions have, as evidenced by the examples printed at the bottom.