# 3.Given the set of positive integers S, partition this set into two subsets S1 and S2 so that the difference between 
# the sum of the elements in S1 and S2 is minimal. For example, for set S = { 1, 2, 3, 4, 5 }, 
# the two subsets could be S1 = { 1, 2, 4 } and S2 = { 3, 5 }. Display at least one of the solutions.

def naive(s,s1,s2):
    if len(s)==0:
        return [s1,s2]
    x=s.pop()
    x1=naive(s.copy(),s1+[x],s2)
    x2=naive(s.copy(),s1,s2+[x])
    if abs(sum(x1[0])-sum(x1[1]))<abs(sum(x2[0])-sum(x2[1])):
        return x1
    else:
        return x2

def naive_imp(s):
    return naive(s.copy(),[],[])

def dynamic(s):
    half_sum=sum(s)//2+1
    data=[[False,[]] for _ in range(half_sum)]
    data[0]=[True,[]]
    for i in s:
        for j in range(half_sum):
            if data[j][0] and j+i<half_sum and (i not in data[j][1]):
                data[j+i]=[True,data[j][1]+[i]]
    for i in range(half_sum-1,0,-1):
        if data[i][0]:
            return [data[i][1],[j for j in s if j not in data[i][1]]]


if __name__ == "__main__":
    s=[1,2,3,4,5]
    print("Naive: {}".format(naive_imp(s)))
    print("Dynamic: {}".format(dynamic(s)))