Quick find algorithm in python