fork download
  1. def no_subset_sum(c):
  2. n = len(c)
  3. if n == 0: return True #case: set length is 0
  4. if (c[0] > 0 or c[n-1] < 0): return True #case: all positive or all negative numbers in set, no zeroes
  5.  
  6. def subset_zero(a):
  7. #Given a sorted list of distinct integers, write a function that returns whether there are two integers in the list that add up to 0
  8. if no_subset_sum(a): return False
  9. n, i = len(a), 0
  10. while (a[i] <= 0):
  11. if a[i] == 0: return True
  12. for t in range(n-1,i,-1):
  13. if (a[i] + a[t] == 0): return True
  14. i = i+1
  15. return False
  16.  
  17. #tests
  18. t1 = [1,2,3]
  19. t2 = [-5,-3,-1,2,4,6]
  20. t3 = []
  21. t4 = [-1,1]
  22. t5 = [-97364, -71561, -69336, 19675, 71561, 97863]
  23. t6 = [-53974, -39140, -36561, -23935, -15680, 0]
Success #stdin #stdout 0.02s 27712KB
stdin
Standard input is empty
stdout
Standard output is empty