Algorithm on python