Useful algorithms in python