def product_except_self(nums): n = len(nums) res = [1] * n left_product, right_product = 1, 1 l = 0 r = n - 1 while l < n and r > -1: res[l] *= left_product res[r] *= right_product left_product *= nums[l] right_product *= nums[r] l += 1 r -= 1 return res # Driver code def main(): inputList = [[1, 5, 10], [3, 5, 0, -3, 1], [7, 8, 9, 10, 11], [2, -4, -8, -11, 11], [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 3, 4, 5]] # For each input, print the input and its maximum depth for i in range(len(inputList)): print (str(i + 1) + '.\tnums:', inputList[i]) print ('\tres:', product_except_self(inputList[i])) print ('-' * 100) if __name__ == '__main__': main()