-5

I have created a random list of 60 numbers, I don't know the numbers contained in the list. I'm asked to find any combination of three numbers from the list that sum to zero. What can I do? That's my code:

import random
import itertools
result = []

for x in range (-30, 30):
   num = random.randint(-30, 30)
   while num in result:
     num = random.randint(-30, 30)
     result.append(num)
        result = [seq for i in range(len(result), 0, -1) for seq in itertools.combinations(result, i) if sum(seq) == 0]
print result
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • What are your attempts so far? – Vasilis G. Jan 30 '18 at 17:53
  • Please include the code that you have already tried. (How the list is made, what you have already tried to make them add.) – fin444 Jan 30 '18 at 17:54
  • My sum code is not here as it was not working! – user9290537 Jan 30 '18 at 17:59
  • 1
    Posting code that isn't working is sort of the point of stackoverflow. We can look at it and tell you what you did wrong. – Chris Martin Jan 30 '18 at 18:01
  • Please take care to [format your question](https://stackoverflow.com/help/formatting) so that code displays as code. – Zero Piraeus Jan 30 '18 at 18:06
  • Since `result` is initially empty and you never add anything to it outside of the while loop, the code in `while num in result:` will never be reached. All your code does is generate 60 random numbers, discards each one in turn, and then prints an empty list at the end. – John Coleman Jan 30 '18 at 18:06

2 Answers2

1

For the purpose of demonstration I'll define a particular example value for result that we can test with and see what happens.

result = [1, 2, -2, -3, 4]

You can use itertools.combinations to list all combinations of three numbers.

import itertools

>>> list(itertools.combinations(result, 3))
[(1, 2, -2),
 (1, 2, -3),
 (1, 2, 4),
 (1, -2, -3),
 (1, -2, 4),
 (1, -3, 4),
 (2, -2, -3),
 (2, -2, 4),
 (2, -3, 4),
 (-2, -3, 4)]

And you can use filter with lambda c: sum(c) == 0 as the predicate to select combinations that sum to zero.

>> list(filter(lambda c: sum(c) == 0, itertools.combinations(result, 3)))
[(1, 2, -3)]
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
  • I don't know the numbers in the list! what can I do? – user9290537 Jan 30 '18 at 17:56
  • What do you mean? Why do you need to know them? – Chris Martin Jan 30 '18 at 17:56
  • You use numbers 1, 2, -2 etc. i don't know the numbers from my list – user9290537 Jan 30 '18 at 17:58
  • I defined `result` to be a particular set of numbers for the purpose of showing you an example of what happens. But the code works for any set of numbers, not just for the example. – Chris Martin Jan 30 '18 at 17:59
  • I just added my code above, what can I do to get any combination from the list i created, that sums to zero? – user9290537 Jan 30 '18 at 18:02
  • The code you posted defines a variable called `result` that contains a list of numbers. The expression at the end of my answer, `list(filter(lambda c: sum(c) == 0, itertools.combinations(result, 3)))`, is the combinations from that list that sum to zero. – Chris Martin Jan 30 '18 at 18:03
0

There is a function combinations in itertools and it can be used to generate combinations.

import random
import itertools
# Generate a random list of 30 numbers
mylist = random.sample(range(-50,50), 30)

combins = itertools.combinations(mylist, 3)
interested = list(filter(lambda combin: sum(combin) == 0, combins))
print(interested)

Note that the results from filter() and itertools.combinations() are iterables and list() is needed to convert the iterable to a list.

lambda expression lambda combin: sum(combin) == 0 is used to keep the combinations which the sum is zero from combins

itertools.combinations(): https://docs.python.org/3.6/library/itertools.html#itertools.combinations

filter(): https://docs.python.org/3.6/library/functions.html?highlight=filter#filter

Aaron
  • 1,255
  • 1
  • 9
  • 12
  • :) The list is generated randomly so the list `interested` may be empty if lucky enough. Then re-run the script. – Aaron Jan 30 '18 at 18:17