iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🎰
Enumerating Subset Sums
I learned how to enumerate the subset sums of a set.
*For example, for a set a = {8, 3, 7, 2, 5}, this would result in something like {0, 2, 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 23, 25}.
a = map(int, input().split())
sub = set()
sub.add(0)
for i in a:
for j in list(sub):
sub.add(i+j)
I also learned that it can be written more concisely as follows:
a = map(int, input().split())
sub = {0}
for i in a:
sub |= {i + j for j in sub}
Additionally, to sort the subset sums added to the set:
sorted(list(sub))
I also learned that list(sub) can be written as [*sub].
Discussion