1

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]]

Mourya
  • 75
  • 6

2 Answers2

5

Not an answer to your question, but here's an alternative solution using itertools. The nice thing about it is that you can use it to get any combination size

import itertools

def n_sum(nums, n=3):
    results = set()
    for c in itertools.combinations(nums, n):
        if sum(c) == 0:
            results.add(tuple(sorted(c)))
    return results

print(n_sum([-1,0,1,2,-1,-4], 3))

Output

{(-1, -1, 2), (-1, 0, 1)}

Or if you prefer a shorter version:

def n_sum(nums, n):
    return {tuple(sorted(c)) for c in itertools.combinations(nums, n) if sum(c) == 0}
001
  • 13,291
  • 5
  • 35
  • 66
  • 2
    Very good code, excellent use of `itertools`and also the proper data structures (tuples for the combinations and a set to eliminate duplicates) – Matteo Zanoni Feb 26 '22 at 14:02
  • here `results.add(tuple(sorted(c)))` why did you use tuple and thanks for introducing me to itertools. – Mourya Feb 26 '22 at 14:16
  • 1
    You can't make a `set` of `lists`. [How to make a set of lists](https://stackoverflow.com/a/26783343) – 001 Feb 26 '22 at 14:19
  • @JohnnyMopp c is a tuple how can we `sum(c)` TypeError: unsupported operand type(s) for +: 'int' and 'list' – Mourya Feb 26 '22 at 15:23
  • 1
    @Mourya `sum` works on tuples. I don't get any errors with this code. What are you passing to the function? https://replit.com/@JohnnyMopp/SandybrownConcreteBrace#main.py – 001 Feb 26 '22 at 15:53
  • @JohnnyMopp i sincerely apologize, i was trying to test the code and mistakenly put out a wrong input i.e. [[1,0,-1],[-1,2,-1]] – Mourya Feb 26 '22 at 16:52
3

The problem is when you check (nums.index(i) != nums.index(j)) and (nums.index(i) != nums.index(k)) and (nums.index(j) != nums.index(k)), if you have two equal numbers in the list (like two -1) then nums.index will return the index of the first one and you will discard that combination.

To solve this I changed your i, j and k to be the indexes and force them to not be equal directly in the range function.

nums = [-1, 0, 1, 2, -1, -4]


def three_sum(nums):
    lst = []
    for i in range(len(nums)):
        for j in range(i + 1, len(nums)):
            for k in range(j + 1, len(nums)):
                n1, n2, n3 = nums[i], nums[j], nums[k]
                if n1 + n2 + n3 == 0:
                    tmp_lst = [n1, n2, n3]
                    tmp_lst.sort()
                    lst.append(tmp_lst)

    # remove possible duplicates
    for m in lst:
        while lst.count(m) > 1:
            lst.remove(m)
            lst.sort()

    return lst


print(three_sum(nums))
Matteo Zanoni
  • 3,429
  • 9
  • 27