Non Recursive Implementation Of Recursive Algorithm Python