Write Recursive Algorithm Python