SHARE
TWEET
subset sum answer
a guest
Aug 16th, 2017
2,444
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- def closest (Listi,S,r):
- List = Listi
- if r in List:
- List.remove (r)
- l = []
- L = []
- answer = 0
- dic = {}
- if S in List:
- return S
- else:
- for each in List:
- if S > each:
- l.append ((S)- (each))
- dic[(S) - (each)] = each
- if not len(l) == 0:
- answer = (dic[min(l)])
- return (answer)
- else:
- return 'nope'
- def partition(I):
- try:
- orgi = []
- orgi.extend (I)
- b = sum(I)/2
- i = I
- i.sort ()
- i.reverse ()
- c = 0
- try:
- for each in I:
- try:
- i = I
- stop = 0
- B = each
- while len(i) >= 1 and stop == 0:
- if B == b:
- stop = 1
- return ('yes')
- c = closest(i,b - B,each)
- if c == 'nope':
- stop = 1
- else:
- if c == b - B and len(i) > 1:
- return ('yes')
- B += c
- i.remove (c)
- except Exception as e:
- f = 0
- except Exception as e:
- b = sum(orgi)/2
- i = orgi
- i.sort ()
- c = 0
- for each in orgi:
- i = orgi
- stop = 0
- B = each
- while len(i) >= 1 and stop == 0:
- if B == b :
- stop = 1
- return ('yes')
- c = closest(i,b - B,each)
- if c == 'nope':
- stop = 1
- else:
- if c == b - B and len(i) > 1:
- return ('yes')
- B += c
- i.remove (c)
- except Exception as e:
- return ('none')
- def SubsetSum(ListI):
- List = ListI
- s = sum(List)
- NList = []
- S = 0
- List2 = []
- List.append(-s)
- #List.append(S+s)
- for each in List:
- NList.append (abs(each))
- if partition(List) == 'yes':
- return ('yes')
- else:
- return ('no')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

