Skip to content

Instantly share code, notes, and snippets.

@GuoJing
Created March 14, 2016 18:46
Show Gist options
  • Save GuoJing/721bcb6f4c36ba42fbac to your computer and use it in GitHub Desktop.
Save GuoJing/721bcb6f4c36ba42fbac to your computer and use it in GitHub Desktop.
import sys
from itertools import chain, combinations
def find_partition(int_list):
A = set()
B = set()
for n in sorted(int_list, reverse=True):
if sum(A) < sum(B):
A.add(n)
else:
B.add(n)
return (A, B)
def powerset(iterable):
xs = list(iterable)
return chain.from_iterable( combinations(xs,n) for n in range(len(xs)+1) )
def loop(A, O):
t = []
a_lists = list(powerset(A))
for a in a_lists:
if sum(a) in O:
if a:
t.append(a)
return t
if __name__ == '__main__':
n = sys.argv[1:][0]
n = int(n)
print '-' * 40 + ' in ' + '-' * 40
print n
l = range(1, n + 1)
if n % 2 != 0:
l.insert(0, 0)
r = find_partition(l)
A, B = r
A, B = sorted(list(A)), sorted(list(B))
A_equals_B = sum(A) == sum(B)
total = 0
s = set()
if not A:
total = 0
elif len(l) < 3:
total = 0
elif not A_equals_B:
total = 0
else:
t = loop(A, B)
print '-' * 40 + ' para ' + '-' * 40
print A, B
print sum(A), sum(B)
print '-' * 40 + ' restult ' + '-' * 40
print t
print '-' * 40 + ' total ' + '-' * 40
total = len(t) + 1
print total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment