4 ms·
A simple way to do it is to imagine your apples sitting in a row, assign each one a random number, and sort them by those random numbers. Then the first ten app
by sketerpot 16y ago
A simple way to do it is to imagine your apples sitting in a row, assign each one a random number, and sort them by those random numbers. Then the first ten apples go in one crate, the next ten apples go in the next crate, and so on.
But for some reason these guys always seem to want code, not a discussion of how the code might work.
- fleitz 16y agoI think a more flexible way might be to shuffle the boxes, placing each box into a ring data structure, and placing each apple into the next box. As we'd expect each box to contain more than one apple this should reduce shuffling. But yeah, I agree, they always want code not how it might work.
- deleted 16y ago[deleted]
- RodgerTheGreat 16y agoThat assumes they want an even distribution of apples in each crate. My reading of the problem would suggest that for each apple we should choose a random box and insert the apple into said box. The question seems like a poorly phrased discussion of the pigeonhole principle.
- lmkg 16y agoWell, you can extend the parent's solution by throwing in 9 "end of current box" markers with the 100 apples before randomizing the order, rather than declaring them to be every 10 apples after sorting. This is probably not an efficient solution from a programming perspective, but it's similar how you would count the number of distinct such arrangements in combinatorics.
- RodgerTheGreat 16y agoAs you said, not very practical from a programming perspective, but it's an interesting way of looking at the problem. Thanks.