Algorithm exercise in Python!