On Mon, 2011-12-26 at 10:01 +0530, Pratap Chakravarthy wrote:
> # Initialize variables
> holes, bk = HOLES[:], {} ;
> random.shuffle( holes )
>
> # Make buckets
> [ bk.setdefault(y, []).append((x,y)) for x, y in holes ]
>
> # Result
> print [ bk[3].pop(0), bk[4].pop(0), bk[5].pop(0) ] + random.sample(
> bk[3] + bk[4] + bk[5], 3 )
>
> Some times the crude method is the best method ?
>
> A crude analysis on the above logic and number of passes made on the
> sample list.
> 1. To copy HOLES to holes
> 2. To shuffle holes using random.shuffle()
> 3. To make buckets
> 4. random.sample() is another crude algorithm which seems to be in
> between O(n) and O(n^2) - a wild guess.
I am pasting Gora's and G0sub's solutions here as pastebins expire and
the next golfer who searches this list will get confused.
Gora:
import random
ALL_HOLES=[(1, 5),
(2, 3),
(3, 4),
(4, 4),
(5, 5),
(6, 4),
(7, 3),
(8, 4),
(9, 3),
(10, 4),
(11, 3),
(12, 4),
(13, 4),
(14, 3),
(15, 4),
(16, 5),
(17, 4),
(18, 4)]
PAR3_HOLES=[i for i in ALL_HOLES if i[1]==3]
PAR4_HOLES=[i for i in ALL_HOLES if i[1]==4]
PAR5_HOLES=[i for i in ALL_HOLES if i[1]==5]
# Must allow at least 2 holes of par 4, and 5
n3 = random.randint( 1, 4 )
SEL_PAR3_HOLES = random.sample( PAR3_HOLES, n3 )
# Must allow at least 1 hole of par 5
n4 = random.randint( 1, 5 - n3 )
SEL_PAR4_HOLES = random.sample( PAR4_HOLES, n4 )
n5 = 6 - n4 - n3
SEL_PAR5_HOLES = random.sample( PAR5_HOLES, n5 )
# Selected values are in SEL_PAR3_HOLES, SEL_PAR4_HOLES, SEL_PAR5_HOLES
print len( SEL_PAR3_HOLES ), len( SEL_PAR4_HOLES ), len( SEL_PAR5_HOLES
)
G0sub:
import random
ALL_HOLES=[(1, 5),
(2, 3),
(3, 4),
(4, 4),
(5, 5),
(6, 4),
(7, 3),
(8, 4),
(9, 3),
(10, 4),
(11, 3),
(12, 4),
(13, 4),
(14, 3),
(15, 4),
(16, 5),
(17, 4),
(18, 4)]
PAR3_HOLES=[i for i in ALL_HOLES if i[1]==3]
PAR4_HOLES=[i for i in ALL_HOLES if i[1]==4]
PAR5_HOLES=[i for i in ALL_HOLES if i[1]==5]
# Must allow at least 2 holes of par 4, and 5
n3 = random.randint( 1, 4 )
SEL_PAR3_HOLES = random.sample( PAR3_HOLES, n3 )
# Must allow at least 1 hole of par 5
n4 = random.randint( 1, 5 - n3 )
SEL_PAR4_HOLES = random.sample( PAR4_HOLES, n4 )
n5 = 6 - n4 - n3
SEL_PAR5_HOLES = random.sample( PAR5_HOLES, n5 )
# Selected values are in SEL_PAR3_HOLES, SEL_PAR4_HOLES, SEL_PAR5_HOLES
print len( SEL_PAR3_HOLES ), len( SEL_PAR4_HOLES ), len( SEL_PAR5_HOLES
)
--
regards
Kenneth Gonsalves
_______________________________________________
BangPypers mailing list
[email protected]
http://mail.python.org/mailman/listinfo/bangpypers