
How (not) to distribute 100 apples in 10 crates with python.  - bgraves
http://mail.python.org/pipermail/europython/2010-April/007589.html
======
hugh3
It seems like an overengineered solution. A robot arm with image analysis? Why
not just stick the apples in a hopper, put in a little door to release one
apple at a time, and then just move the hopper back and forth (using a Python
script) on a track along the line of crates?

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.

------
zekel
I'm not sure how they're going to stop the pythons from eating the Apples, but
if I had to do his homework this is what I'd probably do.

    
    
        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)]

~~~
aohtsab
why do you not recommend a list comprehension?

~~~
JeremyBanks
List comprehensions are wonderful, but in his example it's not being used to
generate an actual list (each .append() will return None), just as a strange
alternate format for a for loop.

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)

~~~
sketerpot
If you're responding to a poorly-disguised lazy homework solution request,
then the really strange version is the one you should give. Any student who
tries to turn it in will arouse the suspicion of the grader, and they'll
deserve it.

~~~
zekel
I had assumed the student would find a decent solution long before this hn
thread.

------
Seiwynn
This is a pretty awesome response to a not so well disguised homework problem.

------
sketerpot
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.

~~~
RodgerTheGreat
That 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
Well, 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
As you said, not very practical from a programming perspective, but it's an
interesting way of looking at the problem. Thanks.

------
leif
[http://developer.amazonwebservices.com/connect/entry.jspa?ca...](http://developer.amazonwebservices.com/connect/entry.jspa?categoryID=24&externalID=446)
may be of service to help ensure true randomness. It may be necessary to
create a voting protocol to prevent malicious mechanical turkers from removing
entropy from your boxes, and that should also make it suitably self-policing
that you will minimize risk of Apple theft. Considering that Apples are among
the most expensive consumer electronics, this would be among any poor
student's highest concerns.

------
adelevie
Am I missing the joke here?

~~~
yetanotherlogin
I don't think the OP was talking about real, physical apples. Paragraph 3 off
the response is the payoff, basically.

------
d0mine

      M, N = 10, 100
      apples = xrange(N)
      L = random.sample(apples, N)
      crates = [L[i:i+M] for i in xrange(0, N, M)]

~~~
metamemetics
You are not randomly placing apples in crates. You are putting sequential
groups of 10 in crates. 10 is the average number of apples that will be in
each crate, but it is possible no crate has 10 apples in it.

