r/dailyprogrammer • u/jnazario 2 0 • Jan 31 '18
[2018-01-30] Challenge #349 [Intermediate] Packing Stacks of Boxes
Description
You run a moving truck business, and you can pack the most in your truck when you have stacks of equal size - no slack space. So, you're an enterprising person, and you want to write some code to help you along.
Input Description
You'll be given two numbers per line. The first number is the number of stacks of boxes to yield. The second is a list of boxes, one integer per size, to pack.
Example:
3 34312332That says "make three stacks of boxes with sizes 3, 4, 3, 1 etc".
Output Description
Your program should emit the stack of boxes as a series of integers, one stack per line. From the above example:
331
322
34If you can't make equal sized stacks, your program should emit nothing.
Challenge Input
3 912743471352
3 42137586
9 2
4 064876318535318Challenge Output
9124
7342
7135
426
138
75
(nothing)
0665
4733
8315
881Notes
I posted a challenge a couple of hours ago that turned out to be a duplicate, so I deleted it. I apologize for any confusion I caused.
EDIT Also I fouled up the sample input, it should ask for 3 stacks, not two. Thanks everyone.
4
u/jnazario 2 0 Jan 31 '18
Scala Solution
5
u/GetRekt Jan 31 '18 edited Jan 31 '18
Was that 2 supposed to be a three because the output has 3 lines (stacks)
Also seems weird that the output order in each stack doesn't have to be largest first
3
u/Tarvish_Degroot Jan 31 '18
I think I'm misunderstanding something here. Isn't this supposed to be 2 stacks, not 3?
2
u/Kinglink Jan 31 '18
I have to ask. WTF is this about?
You say
Ok.. but then the output is
Uhhh isn't that 3 stacks?
It's kind of a confusing challenge if that output is correct, but I have a feeling it's incorrect.
1
u/[deleted] Jan 31 '18
Would you say
is a valid answer for number one? Or do we always have to find the largest possible answer?
1
u/[deleted] Jan 31 '18
So all boxes don't need to be used? The example leaves a box out (of size 1), but all of the challenges seem to use all of the boxes
3
u/TheMsDosNerd Jan 31 '18
Backtracking solution in Python 3
3
u/zqvt Jan 31 '18 edited Jan 31 '18
Python3
Output:
3
u/Gprime5 Feb 01 '18
Python 3.5
3
u/zatoichi49 Feb 01 '18 edited Feb 01 '18
Method:
Determine the number of boxes required for each stack layer, and the average layer size (sum(list) // height of stack). Take all the combinations for each layer that sum to the layer size, filtering out any duplicates. Take the product of the combinations for each layer and chain together. If the sorted chain equals the sorted original list, print the layers of the chain.
Python 3:
Output:
3
u/Sharenas Feb 03 '18 edited Feb 03 '18
Python 3.6 Hello everyone, I've been learning Python for 2.5 weeks, and this is my first submission on this site. Any critique is fantastic but please be gentle!
I know I am not using formatting shortcuts or anything, as I haven't learned them yet, but maybe this place is a good place to start. Also, I think certain input would probably break it (for example, if there is no solution, but the sum of the boxes is divisible by the number of stacks). I've just finished the chapter on lists, so thought this was a good question to try out. Also - please forgive me for making the input easier to parse, I wasn't sure how to split a number entered as an integer into a list....
Thanks!
2
u/ShadowedSun Feb 04 '18 edited Feb 04 '18
Does each stack need to contain as-close-as-possible the same number of boxes?
I.e. for input
we could get output
or
Is the former preferable over the latter?
1
u/mdx2 Jan 31 '18 edited Jan 31 '18
Python 3.6
Due to the simple use of permutations, I think this solution scales pretty poorly.
1
u/Robobble Feb 01 '18 edited Feb 02 '18
https://gist.github.com/Robobble/9f529ca5980e0d426ce7028af9a9e25b
Python 2.7
Iterates through input numbers and randomly shuffles them into the same amount of lists as there are boxes. Then it adds the lists together and checks them against the required stack size. Repeat until everything adds up. Works surprisingly well.
Edit: Out of curiosity, I added a counter for the amount of wrong attempts and for the challenge inputs, the count was between 200 and 1000, never above 5,000. I decided to run:
Just for shits and giggles. It ran for well over 2 hours. I was timing it but had to do other things. I came back around 4 hours and it was finished. It found the answer in 18,411,561 attempts. Got a good chuckle out of that, figured I'd share.
1
u/popillol Feb 01 '18
Go / Golang Playground Link. Goes through permutations in lexicographic order until it finds a solution. My solutions aren't the same as challenge output but they still check out.
Output
1
u/TheBlackCat13 Feb 01 '18 edited Feb 01 '18
Python 3.6, a recursive
collections.Counter-based approach, without the use of combinations:And the output (error traceback removed). I get some different answers, but they seem to be valid answers:
1
u/SyonFox Feb 01 '18
Here is my poorly implemented algorithm in python 3 cus im on my phone and dont want to mess with c++ :) I missread the challenge so it takes an input of "3 7 11 3 2 2 2 3" ever number space seperated. I havent used python much but just pretended it was psudo code ;) and it worked ok. Let me know if you break it but it should work for samples with bigger numbers ie. 11 or 40. The idea behind my solution is to:
`
sort(boxes) if(sum(boxes)%numStacks != 0 ) Fail
for each stack { take the largest box and keep adding smaller ones until you cant if you cant add any smaller boxes and the stack isn't at the desired height then take the last box you put on off and put it in another pile then go back and try adding the remaining boxes.
if you take off all the boxes in a pile it is impossible to make a stack of desired height becuse youve tried all remaining boxes. }
` I believe best case is nlogn due to sorting and worst case is n2 before failing to find a stack. but didnt spend much time thinking about it.
any feedback is recommended since im very new to python
`
def main(): stdin = input("Input: ")
main()
`
1
u/Areumdaun Feb 01 '18
Python 3 Much faster than I thought it would be, even the 4th example seems to take less than 1 second. I'm sure there's still a lot of room for optimisation.
1
u/LegendK95 Feb 01 '18
Haskell
1
u/[deleted] Feb 01 '18
Haskell
I am not sure if I understand the question. Is
A valid answer for last one?
1
u/paulosfjunior Feb 02 '18 edited Feb 02 '18
1
u/[deleted] Feb 03 '18
JAVA
A bit long but it works.
1
u/Daanvdk 1 0 Feb 03 '18
Python3
1
u/DEN0MINAT0R Feb 03 '18
Python 3
This is a bit of a strange solution. It returns different (though correct) outputs from most everyone else's programs. I tried to make a version that doesn't use itertools, just for fun. It basically tries to put the largest possible box into each stack, until the next box is too big for the stack, then moves on to the next stack. As a result, each subsequent stack generally contains more, smaller boxes. I also used the time.process_time() function to see exactly how fast it is, and compare to others.
Output:
1
u/[deleted] Feb 04 '18
Python 3.6 Cannot handle the last chal input due to size. Posting late but if anyone stumbles on this and would like to help me solve that issue, please comment/pm.
1
u/PM_ME_YOUR_MASS Feb 04 '18 edited Feb 04 '18
Swift 4.0
Testing and printing the solution:
Solution:
I'm new to Swift, so if someone could point out areas where this could better to conform to the standards, I'd appreciate it. From what I've seen early on, Swift seems to be very particular about what is "proper" Swift naming/structure
1
u/keto166 Feb 05 '18 edited Feb 05 '18
Java
Stops after the first perfect solution, and otherwise it finds the solution with the least average difference between the stacks. //Input file : 3 3 4 3 1 2 3 3 2
Output
1
u/zookeeper_zeke Feb 06 '18 edited Feb 06 '18
This is a highly constrained variant of bin packing given that you know the number of stacks before hand. Here's a basic branch and bound search written in C with symmetry breaking such that items can only be packed with those that follow in a left-to-right order. This reduces the number of permutations tried before a solution is reached.
1
u/[deleted] Feb 19 '18
Python 3.6 - basic logic and solution.
1
u/balzzawade Feb 23 '18
PHP:
1
u/2kofawsome Jul 04 '18
python3.6
Not fast, and I find it kinda gross how I have the program just end when its found a solution, but eh, it works.
1
u/skeeto -9 8 Jan 31 '18
C. Parses the input into a histogram, then does a depth-first search for a solution.
0
u/[deleted] Jan 31 '18
[deleted] — view deleted comment