Find S Algorithm In Python