'I have an assignment where I need to check whether I can partition a set into two subset that are equal in sum
I have an assignment where I need to check whether I can partition a set into two subsets that are equal in sum and output the subset as a result. For example
[[[1,1,2,2,2,3,5,5,9]
gives you a result of
[[1, 2, 2, 2, 3, 5], [1, 5, 9]], [[1, 2, 2, 5, 5], [1, 2, 3, 9]],
[[1, 1, 3, 5, 5], [2, 2, 2, 9]], [[2, 3, 5, 5], [1, 1, 2, 2, 9]]]
Currently my output is the same but the order is different
[[[1, 2, 2, 2, 3, 5], [1, 5, 9]], [[2, 2, 2, 9], [1, 1, 3, 5, 5]],
[[1, 2, 3, 9], [1, 2, 2, 5, 5]], [[2, 3, 5, 5], [1, 1, 2, 2, 9]]]
They say is possible to achieve the same order result but till now I still cannot get the same order
# Returns true if there exists a sublist of list `nums[0…n]` with the given total
def subsetSum(nums, n, total,secondSubset):
# return true if subsets are equal
if sum(nums) == sum(secondSubset):
return (True,nums,secondSubset)
# base case: no items left or sum becomes negative
if n < 0 or total < 0:
return (False,nums,secondSubset)
# Number to include/exclude
numConsider = nums[n]
# Case 1. Include the current item `nums[n]` in the subset and recur
# for remaining items `n-1` with the remaining sum `total-nums[n]`
# Remove it from first subset and adding it to the second
nums.remove(numConsider)
secondSubset.append(numConsider)
include,first,second = subsetSum(nums, n - 1, total - numConsider,secondSubset)
# return true if we get subset by including the current item
if include:
return (True,nums,secondSubset)
# Since number does not need to be included, revert back to the same subset
nums.append(numConsider)
secondSubset.remove(numConsider)
# Case 2. Exclude the current item `nums[n]` from the subset and recur for
# remaining items `n-1`
exclude,first,second = subsetSum(nums, n - 1, total,secondSubset)
# return true if we get subset by excluding the current item
return (exclude,nums,secondSubset)
# Returns true if given list `nums[0…n-1]` can be divided into two
# sublists with equal sum
def partition(nums):
total = sum(nums)
# check all the nums
state,first,second = subsetSum(nums, len(nums) - 1, total/2,[])
newTotal = sum(first)
return state,first,second
def partitionSet(collection):
if len(collection) == 1:
yield [ collection ]
return
first = collection[0]
for smaller in partitionSet(collection[1:]):
# insert `first` in each of the subpartition's subsets
for n, subset in enumerate(smaller):
yield smaller[:n] + [[ first ] + subset] + smaller[n+1:]
# put `first` in its own subset
yield [ [ first ] ] + smaller
if __name__ == '__main__':
# Input: a set of items
# nums = [7, 3, 1, 5.3, 4.5, 8.2]
# nums = [7,3,1,5,4,8]
nums = [ 1,1,2,2,2,3,5,5,9]
# Get the state, first and second subset
# state,first,second = partition(nums)
# if state:
# print('Set can be partitioned')
# print('The first subset : ' , first)
# print('The second subset : ' , second)
# else:
# print('Set cannot be partitioned')
# newTotal = sum(first)
# totalNum = first+second
newSet = []
# Find all possible set of the input
for n, p in enumerate(partitionSet(nums), 1):
# Only consider those with 2 partition
if(len(p)==2):
# Only consider if the total match
if(sum(p[0]) == sum(p[1])):
# Sort the partition
newP = [sorted(p[0]),sorted(p[1])]
# Ensure new partition is new not added before
if(newP not in newSet):
insertState = True
for j in newSet:
# Ensure new partition is not part of another enumeration
if(newP[0]==j[1]):
insertState = False
if(insertState):
# print(newP)
newSet.append(newP)
print(newSet)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
