7
1
Just last night I was reading a book which had a chapter on the Partition Problem; the Partition Problem is basically about splitting a set of numbers into two smaller sets of equal size.
To visualize it, the book contained the following picture:
It shows how, given an input of k boxes, each with height n, you can create two stacks of boxes, each stack with half the height of the original stack (well, in this diagram, pile.)
If you were to write a function to do this, it might look something like:
>>> k = [3,4,3,1,2,3,3,2,1]
[3,4,3,1,2,3,3,2,1]
>>> partitionSolve(k)
[ [3,3,2,3], [1,4,2,3,1] ]
>>> sum(partitionSolve(k)[0]) == sum(partitionSolve(k)[1])
True
But this only solves for two stacks! What if we wanted to split the input into three equal stacks? Then we'd have to write another function!
>>> partitionSolveThree([3,4,3,1,2,3,3,2])
[ [3,4], [3,1,3], [2,3,2] ]
In fact, I want to be able to solve for N stacks:
>>> partitionSolveN(3, [2,1,1,2,3,1,2])
[ [2,2], [1,1,2], [3,1] ]
>>> partitionSolveN(4, [2,1,1,2,3,1,2])
[ [2,1], [1,2], [3], [1,2] ]
But, I'm lazy, and I don't want to have to start some language's shell every time I want to use this function, so I decide to create a program to do it for me.
The problem is, I can't figure out an efficient way to do it!
Your Task
Design a program which can solve the above problem, for any number N.
Your program should take input via stdin, and print its output to stdout.
Example:
C:\> echo 2343123321| solution.X
3323
14231
Input will be a single string of digits; the first specifying how many stacks the code should be broken into, and the rest represent the input stack. All numbers will be single digit, so you can think of it as a list delimited by empty strings (''
).
The first character of input will be the N to solve for, so if the first character is a 6 your program will be expected to output 6 valid subsets, or an error. This number will always be between 1 and 9.
The rest of the input is the starting list, from which the sublists must be created. Each digit in here can be between 1 and 9, and they must all be used once and only once in the outputted sublists.
Output should then be a new-line delimited list of your output stacks, each stack should just be a sequence of the numbers within it.
Another few examples:
C:\> echo 334323321| solution.X
34
313
232
C:\> echo 3912743471352| solution.X
121372
97
4453
C:\> echo 92| solution.X
forty-two
If the given input has no solution, your program can print any error message it wants, as long as there are no digits in the error message.
Fewest number of bytes wins! Good luck!
1Is it acceptable to not print anything at all if there's no solution? – Zgarb – 2015-04-06T10:37:08.367
@Zgarb Yes, but I really hope people put some thought into coming up with hilarious error messages. Upvoted your question for others to see. – theonlygusti – 2015-04-06T10:44:03.843
This problem is NP-complete. You are not going to see algorithms that are faster than brute force. – FUZxxl – 2015-04-06T11:05:32.443
3@FUZxxl Why do you keep saying this? NP-complete means there are no polynomial time solutions, but doesn't exclude algorithms faster than brute force in general. – orlp – 2015-04-06T11:14:59.290
2@FUZxxl Also, since the boxes are of size 1-9, I'm not even sure it's NP complete. – orlp – 2015-04-06T11:16:11.183
@orlp These are valid points. – FUZxxl – 2015-04-06T11:21:39.973
How are we supposed to find out what N to solve for? Is it the first digit in the input? Does that mean that N is 1-9? – orlp – 2015-04-06T11:49:12.943
@orlp Yes. It is the first input digit, between 1-9. Thanks for your valid points FUZxxl's FUZzy logic (<- see what I did there? Hee hee). – theonlygusti – 2015-04-06T12:11:36.857
@orlp the boxes are of size 0-9 – theonlygusti – 2015-04-06T12:29:09.800
4@theonlygusti Please disallow boxes of size 0. It just adds more tedious code while not complicating the problem in any interesting way. – orlp – 2015-04-06T12:52:53.173
@orlp I think it complicates the code; the program must be smart enough to know that boxes of 0 have no sum, and can just be dumped anywhere. It adds barely any code. – theonlygusti – 2015-04-06T13:31:22.017
3@theonlygusti But it's entirely trivial. It doesn't add complexity, only annoyance. – orlp – 2015-04-06T13:38:24.980
1I don't even know what a "size zero" box is. If we're pretending these are actual boxes, it doesn't make much sense. – Geobits – 2015-04-06T14:13:52.380
1@Geobits OK, seems like others agree. No 0 handling is required. – theonlygusti – 2015-04-06T17:20:11.743
@orlp ^^ No zero handling required. – theonlygusti – 2015-04-06T17:20:31.627
The second example doesn't seem correct. There are 3
– Geobits – 2015-04-06T17:34:34.7803
in the output, but only 2 of them in the input (once you exclude the first character, since it's the output count). This answer does the same, but your other examples don't. Clarification?@Geobits well spotted; fixed! As for the answer, that shouldn't be happening. – theonlygusti – 2015-04-06T18:22:44.733
Out of curiosity, in which book did you read about it? – hetzi – 2015-04-08T20:33:18.727
@hetzi The New Turing Omnibus - really, I don't find it to be written brilliantly, but the ideas within do interest me.
– theonlygusti – 2015-04-12T09:06:37.210