How (not) to distribute 100 apples in 10 crates with python.
mail.python.org
mail.python.org
You could also attempt to build a purely physical apparatus sans code to distribute them randomly, but it would take a lot of work to prove it was unbiased. The best solution might be a roulette-wheel style arrangement where the apples are emptied into the middle and move out towards the edges as the wheel spins (you might want to make the crates counter-rotate as you did this). Not bruising the apples might be a challenge.
import random
boxes = [[] for x in range(10)]
for apple in range(100):
random.choice(boxes).append(apple)
## don't do it like this
# [random.choice(boxes).append(apple) for apple in range(100)]It would be really strange to do
[random.choice(boxes).append(apple) for apple in range(100)]
when even if you wanted to do it on one line the normal syntax works and would be less likely to confuse: for apple in range(100): random.choice(boxes).append(apple)But for some reason these guys always seem to want code, not a discussion of how the code might work.
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.
The question seems like a poorly phrased discussion of the pigeonhole principle.
M, N = 10, 100
apples = xrange(N)
L = random.sample(apples, N)
crates = [L[i:i+M] for i in xrange(0, N, M)]