Re: Fast generation of permutations

2006-01-29 Thread Anton Vredegoor
Anton Vredegoor wrote: > Paul Rubin wrote: > > > Cool, I'd still like to know why (13**5)-13 = C(52,5) other than > > by just doing the arithmetic and comparing the results. Maybe your > > tkinter script can show that. > > That seems to be very hard :-) Unless I'm missing something. Like a facto

Re: Fast generation of permutations

2006-01-29 Thread Anton Vredegoor
Paul Rubin wrote: > Cool, I'd still like to know why (13**5)-13 = C(52,5) other than > by just doing the arithmetic and comparing the results. Maybe your > tkinter script can show that. That seems to be very hard :-) Unless I'm missing something. Anton def noverk(n,k): return reduce(lambda

Re: Fast generation of permutations

2006-01-28 Thread Paul Rubin
"Anton Vredegoor" <[EMAIL PROTECTED]> writes: > > def deals(): > > for i in xrange(13**5): > > cards = [(i//p) % 13 for p in (1, 13, 169, 2197, 28561)] > > yield cards > > This gives hands like [0,0,0,0,1] and [0,0,0,1,0] which are > permutations of one another.

Re: Fast generation of permutations

2006-01-28 Thread Anton Vredegoor
Paul Rubin wrote: > def deals(): > for i in xrange(13**5): > cards = [(i//p) % 13 for p in (1, 13, 169, 2197, 28561)] > yield cards This gives hands like [0,0,0,0,1] and [0,0,0,1,0] which are permutations of one another. Below is a piece of code that avoids th

Re: Fast generation of permutations

2006-01-25 Thread Paul Rubin
Paul Rubin writes: > Well, maybe not 24x. The exact number is more complicated. I'm still > too sleepy to figure this out right now but may think about it later. Turns out to be 7x, for reasons that are a bit mysterious. Ignoring suits, think of the 5-card hand as a 5-

Re: Fast generation of permutations

2006-01-25 Thread Paul Rubin
Paul Rubin writes: > Note that you're looking at 24x more hands than you really need to, Well, maybe not 24x. The exact number is more complicated. I'm still too sleepy to figure this out right now but may think about it later. -- http://mail.python.org/mailman/listin

Re: Fast generation of permutations

2006-01-25 Thread Paul Rubin
Frode Øijord <[EMAIL PROTECTED]> writes: > > cards = range(52) > > for (hand) in probstat.Combination(card, 5): > > pass > > Takes 1.3 seconds on my laptop instead of 17 seconds for the pure > > python version which is only one order of magnitude faster. > > This is *exactly* what i wanted! I ju

Re: Fast generation of permutations

2006-01-25 Thread Terry Reedy
"Frode Øijord" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > However, this is way too slow for my needs. I try to use this to > generate all possible 5 card poker hands, but this takes around 17 > seconds on an Athlon 2200. That's a 2 orders of magnitude too slow for > my needs.

Re: Fast generation of permutations

2006-01-25 Thread Frode Øijord
Jack Diederich wrote: > You might want to look at a specific purpose library for poker hands: > http://pokersource.sourceforge.net/ Nah, evaluating the poker hands is the FUN part! I want to do that myself :) > If you really want to do combinations a C extension has already > been written (by m

Re: Fast generation of permutations

2006-01-25 Thread Michael Amrhein
Michael Amrhein schrieb: > Frode Øijord schrieb: >> Hi all, >> given a sequence of n elements i need to generate all possible >> permutations of length k <= n. >> >> I found an elegant way to do this recursively: >> >> def comb(items, n): >> if n==0: yield [] >> else: >> for i in x

Re: Fast generation of permutations

2006-01-25 Thread Michael Amrhein
Frode Øijord schrieb: > Hi all, > given a sequence of n elements i need to generate all possible > permutations of length k <= n. > > I found an elegant way to do this recursively: > > def comb(items, n): > if n==0: yield [] > else: > for i in xrange(len(items)): > fo

Re: Fast generation of permutations

2006-01-25 Thread Jack Diederich
On Wed, Jan 25, 2006 at 03:33:48PM +0100, Frode ?ijord wrote: > Hi all, > given a sequence of n elements i need to generate all possible > permutations of length k <= n. > > I found an elegant way to do this recursively: > > def comb(items, n): > if n==0: yield [] > else: > for i

Fast generation of permutations

2006-01-25 Thread Frode Øijord
Hi all, given a sequence of n elements i need to generate all possible permutations of length k <= n. I found an elegant way to do this recursively: def comb(items, n): if n==0: yield [] else: for i in xrange(len(items)): for cc in comb(items[i+1:],n-1):