+ 1
Stuck in the thinking process - Distribute Evenly
Hi There, I am having an issue wrapping my head around a task. It is about evenly distributing an amount N of candies among the K amount of boxes. I have found similar tasks and solution on the internet, but none of them matches my problem. Here Is the code bit I have already started, which includes the details of the task in the comments. https://www.sololearn.com/compiler-playground/ctBssOtRWBrT I don't need the coding solution, but just a little help on the approach. Thanks to everyone.
28 Answers
+ 2
If "amount_candy_types = 20", shouldn't length of "candy" is 20 too?
+ 1
I think floor division and modulo are in part of the problem. (Just discovery divmod() method and looks great.)
The challenge is how we do with the modulo, and how to continue in the next type of candy.
Also as JaScript said list is beneficial if combining with comprehension, then we can handle different number of boxes.
I don’t have a working solution yet, but this question is fun and I will take a try later.
+ 1
Thanks for the help.
At this point, I have already created a list containing all the candy.
It is looking like this:
['c1', 'c1', 'c1', 'c1', 'c2', 'c2', 'c2', 'c2', 'c3', 'c3']
Now I somehow have to figure out how to distribute them among the amount of boxes.
+ 1
You are right, sorry.
I didn't think it through.
I will try to rethink the problem again.
THANKS!
+ 1
Bob_Li, I tried your code.
If I change the candy = [2, 1, 4], the result is unexpected.
+ 1
Bob_Li, the first time a saw was like below.
Example result
Box 01: [2, 1, 1]
Box 02: [1, 2]
Box 03: [1, 1, 1]
Somehow OP's changed the code and becomes:
Box 01: [c1, c1, c2, c3]
Box 02: [c1, c2, c2]
Box 03: [c1, c2, c3]
Basically they are the same. Using A, B and C for different type of candies will look like below.
Box 01: A, A, B, C
Box 02: A, B, B
Box 03: A, B, C
+ 1
Bob_Li, the first time I run it the random test case was empty.
I didn't take a look on the code and click run again and again until i found another error. And then I take a look and find what is wrong.
Your code is much compact than mine and pythonic. I did had a hard time to follow the code.
+ 1
Bob_Li
Oh? I only focused on the random case and think randint is to blame (starting from 0). Didn't notice about the indexing as it was hard to track for me...
+ 1
Bob_Li
When I saw you create the flat list numpy came to my mind, and realised the list can't be transformed properly.
I would go for range with steps to fill in one box at a time. It is more readable to me.
+ 1
Bob_Li
First time I viewed the expected output was represented in number, so I go for number approach. But underneath I used the ABC approach to figure out how to get the expected output and made the divmod() comment.
+ 1
Bob_Li, the way of padding produces lots of empty spaces. If changed to:
candy_arr.resize(bx * len(candy_arr) // bx + 1)
It can reduce the size significantly.
And I found it intriguing to put ZERO into candy list.
When I first read the description it says:
# As input it receives the num. of boxes, candy types and a list with different amount of candy types
Meanwhile Blerim's code also include:
boxes = 3
amount_candy_types = 3
candy = [4, 4, 2]
I thought the program takes 3 inputs, amount_candy_types is a int, but it also can obtain from "candy" which length is 3.
I think we were derailed. Type of candy would be ["Chocolate", "Gummy", "Macaron"]
If that is the case 0 amount of candy does make sense, but I prefer the min unit is 1 as it is more reasonable.
I think we had to let OP to explain in more details...
0
Thanks for your code, but unfortunately it is not working when changing the parameters, such as the amount of boxes and the amount of candy ☹
I was able to produce a list containing all the candy:
['c1', 'c1', 'c1', 'c1', 'c2', 'c2', 'c2', 'c2', 'c3', 'c3']
and also was able to create a loop which produces the numbers 0, 3, 6 & 9.
I just need somehow to loop through each number of the boxes and punts the amount of the candies based on the above numbers (0,3,6,9) in each box the result would be:
Box1: ['c1', 'c1', 'c2', 'c3']
Box2: ['c1', 'c2', 'c2']
Box3: ['c1', 'c2', 'c3']
For my point of view, this solution would be scalable, when the parameters change.
THANKS
0
Sorry for the confusion, but as mentioned in the playground comments, the boxes, the type of candy and the amount of the candy amount are variable, that is why I had mentioned that the provided numbers are just examples.
Your code works with the give example numbers perfectly, but when the amounts are changed I get an error.
For example:
boxes = 5
amount_candy_types = 20
candy = [4, 4, 2, 4, 6]
Error message:
Traceback (most recent call last):
File "./Playground/file0.py", line 47, in <module>
while candy[i] > 0: # as long as the candy type is empty
IndexError: list index out of range
I had similar issues, that is why I have tried a new approach.
Thanks you for taking your time with this 👍
0
Blerim
here is my attempt. The code could probably be optimized further...
I defaulted the last candy type to 0, since your code seems to be failing that case.
https://code.sololearn.com/c9wCfzanMpWx/?ref=app
0
Blerim, here is my attempt. And I further expanded to include 2 test cases and 1 random test case.
Although I said divmod() maybe useful, I found I didn't use it because the way of my coding.
The result is a list of numbers, not your c1, c2 convention, because I don't know is that the question really asked for.
However it should be easy to convert into c1, c2 because the numbers are calculated.
Solving this puzzle force me to use other function I read about but didn't put in use. And the process is fun.
You can take a look if you wish, or run it multiple times to see different result.
I also left the debug code for further study.
https://code.sololearn.com/cx06f0VXc1f5/?ref=app
0
Wong Hei Ming
what is the expected result?
0
if the candies are distributed in order, shouldn't the bottom result be:
Box 01: A A B B C
Box 02: A B C
Box 03: A B
?
oh, I am interpreting the question wrong....
Ok, I think I understand now.
I missed this part:
"# If possible, there should be the same total number of candy in each box; "
The total amount is supposed to be as even as possible.😅
That was why you were tallying the min and max len!
I interpreted it as an orderly distribution of candy types.
ok, I will work on this.
0
Wong Hei Ming
ok, I think it's fixed.
0
Bob_Li, How strange...
candy: [0, 2, 16, 3, 16, 19, 7, 13] # first type has zero candy
boxes: 6
candy_types = ['c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8']
b1: c1 c2 c2 c3 c4 c4 c4 c5 c5 c5 c6 c7 c7 # why c1 in both b1 and b2 ???
b2: c1 c2 c2 c3 c4 c4 c5 c5 c5 c5 c6 c7 c7
b3: c2 c2 c2 c3 c4 c4 c5 c5 c5 c6 c6 c7 c7
b4: c2 c2 c2 c4 c4 c4 c5 c5 c5 c6 c7 c7 c7
b5: c2 c2 c2 c4 c4 c4 c5 c5 c5 c6 c7 c7
b6: c2 c2 c2 c4 c4 c4 c5 c5 c5 c6 c7 c7 # and where are c8 ???
lengths:
b1: 13
b2: 13
b3: 13
b4: 13
b5: 12
b6: 12
0
I updated the code to include the total candies in box, it should have an easier time to read the output.
Also modify the code to reduce redundant prints statement.
https://code.sololearn.com/cmxxJ5pgQlV9/?ref=app