Given a list of space-delimited integers as input, output all unique non-empty subsets of these numbers that each subset sums to 0.
For example:
Input:
8 −7 5 −3 −2
Output:-3 -2 5
Language of your choice - shortest possible code.
For example:
Language of your choice - shortest possible code. |
||||
GolfScript, 41 characters
If you do not care about the specific output format you can shorten the code to 33 characters.
Example (see online):
|
|||
|
|
Python, 119 chars
Enumerates all 2^n subsets recursively and checks each one. |
|||
|
|
J,
|
Python, 120I'm a character worse than Keith's solution. But... this is too close to not post. One of my favorite features of code-golf is how dissimilar similar-length solutions can be.
|
|||
|
|
Mathematica
|
SWI-Prolog 90The empty subset is still there, though. I have no idea how to get rid of it.
Input method
|
|||
|
|
Python (
|
I'm confused about how you're getting integers instead of strings. split() makes a list of strings, but then later you're calling sum on the subsets of that split. – Keith Randall Oct 10 '12 at 4:10 |
|
@KeithRandall: facepalm I was in a rush, so I didn't test my code. Thank you for pointing that out. – beary605 Oct 10 '12 at 5:01 |
|
@gnibbler: That would make a lot more sense when running `'hello'`. Thanks! – beary605 Oct 10 '12 at 23:51 |
C# – 384 charactersOK, functional-style programming in C# is not that short, but I love it! (Using just a brute-force enumeration, nothing better.)
Formatted and commented for more readability:
|
|||
|
|
3 3 -3 -3? – Keith Randall Oct 10 '12 at 4:03