Problem: From a given list, i should return all possible outcomes that give a sum of 0
for a list [-1,0,1,2,-1,-4]
the output should be [[-1,-1,2],[-1,0,1]]
The problem is it's not showing all possible outcomes.
Code:
nums = [-1,0,1,2,-1,-4]
def three_sum():
lst=[]
for i in nums: #take 3 variables
for j in nums:
for k in nums:
if (nums.index(i) != nums.index(j)) and (nums.index(i) != nums.index(k)) and (nums.index(j) != nums.index(k)):
if i+j+k==0:
tmp_lst=[i,j,k]
tmp_lst.sort()
lst.append(tmp_lst)
for m in lst:
while lst.count(m)>1:
lst.remove(m)
lst.sort()
return lst
The expected output should be [[-1,-1,2],[-1,0,1]]
but i get [[-1, 0, 1]]