Add a Python implementation for reference.