Drako already mentioned, that this is not free coding service. Show some effort yourself and tell us, what you already tried and how it didn't work. also provide output to help us analyze the problem. I'll help anyway; also welcome to StackOverflow.
What you are looking for is a "power set". An exhaustive set M
of sets s
, where each s
is a subset of M
and there is in fact no subset of M
which is not already contained in a s
. As sets are unordered by definition, you don't have to care about duplicates because of different ordering. Looking at this question
There is a nice answer to get the power set of a given set. Assuming your input list/set/iterable is i
, you can use the following code to get the power set list.
from itertools import chain, combinations
def get_power_set(i):
i.sort() # Sorted input --> sorted output
ps_iterable = chain.from_iterable(combinations(i, r) for r in range(len(i)+1))
return list(ps_iterable)
def get_highest_sum(input_list):
maximum = 0
for s in input_list:
sum_of_set = sum(s)
if sum_of_set > maximum:
maximum = sum_of_set
return maximum
i = [1,2,3] # Assign your input list here
power_set = get_power_set(i)
highest_sum = get_highest_sum(power_set)
print(power_set)
# >>> [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
print("Highest sum is {}".format(highest_sum))
# >>> 6