Re: Fast powerset function

2007-07-18 Thread Arash Arfaee
Hi All With your help I found lots of good method and algorithm. Also I found out if I exchange all for loop with while loop it make the program much faster and also it consumes less memory (almost half!) Just wanna thank you all. Cheers, Arash On 7/13/07, Mark Dickinson <[EMAIL PROTECTED]> wrot

Re: Fast powerset function

2007-07-13 Thread Mark Dickinson
If you don't care about the order of the results, you can use a Gray code (http://en.wikipedia.org/wiki/Gray_code): this has the advantage of only adding or removing a single element to get from one subset to the next. def powerset(s): d = dict(zip( (1<>> list(powerset('abc')) [set

Re: Fast powerset function

2007-07-13 Thread Evan Klitzke
On 7/12/07, Arash Arfaee <[EMAIL PROTECTED]> wrote: > I need a powerset generator function. It's really slow with recursion. Does > anybody have any idea or code(!!) to do it in an acceptable time? > Thanks > -Arash Here's a much simpler (and faster) solution I got from a coworker: s = range(18)

Re: Fast powerset function

2007-07-13 Thread Carsten Haese
On Fri, 2007-07-13 at 17:38 +, Jyotirmoy Bhattacharya wrote: > On Jul 13, 6:34 pm, Carsten Haese <[EMAIL PROTECTED]> wrote: > > def recursive_powerset(s): > > if not s: yield set() > > for x in s: > > s2 = s - set([x]) > > for subset in recursive_powerset(s2): > >

Re: Fast powerset function

2007-07-13 Thread Jyotirmoy Bhattacharya
On Jul 13, 6:34 pm, Carsten Haese <[EMAIL PROTECTED]> wrote: > def recursive_powerset(s): > if not s: yield set() > for x in s: > s2 = s - set([x]) > for subset in recursive_powerset(s2): > yield subset > yield subset.union(set([x])) > Your recursive_

Re: Fast powerset function

2007-07-13 Thread Carsten Haese
On Fri, 2007-07-13 at 08:15 -0400, I wrote: > [...] > def recursive_powerset(s): > if not s: yield set() > for x in s: > s2 = s - set([x]) > for subset in recursive_powerset(s2): > yield subset > for subset in recursive_powerset(s2): > yield s

Re: Fast powerset function

2007-07-13 Thread Carsten Haese
On 13 Jul 2007 02:25:59 -0700, Paul Rubin wrote > Antoon Pardon <[EMAIL PROTECTED]> writes: > > On 7/12/07, Arash Arfaee <[EMAIL PROTECTED]> wrote: > > > I need a powerset generator function. It's really slow with recursion. > > > Does > > > anybody have any idea or code(!!) to do it in an accepta

Re: Fast powerset function

2007-07-13 Thread Paul Rubin
Antoon Pardon <[EMAIL PROTECTED]> writes: > On 7/12/07, Arash Arfaee <[EMAIL PROTECTED]> wrote: > > I need a powerset generator function. It's really slow with recursion. Does > > anybody have any idea or code(!!) to do it in an acceptable time? > My idea would be the following. ... > 3) let n rang

Re: Fast powerset function

2007-07-13 Thread Antoon Pardon
On 7/12/07, Arash Arfaee <[EMAIL PROTECTED]> wrote: > I need a powerset generator function. It's really slow with recursion. Does > anybody have any idea or code(!!) to do it in an acceptable time? > Thanks My idea would be the following. 1) Turn your set into a list: lst 2) let lng be the numbe

Re: Fast powerset function

2007-07-12 Thread Evan Klitzke
On 7/12/07, Evan Klitzke <[EMAIL PROTECTED]> wrote: > On 7/12/07, Arash Arfaee <[EMAIL PROTECTED]> wrote: > > I need a powerset generator function. It's really slow with recursion. Does > > anybody have any idea or code(!!) to do it in an acceptable time? > > Thanks > > -Arash > > I thought that th

Re: Fast powerset function

2007-07-12 Thread Evan Klitzke
On 7/12/07, Arash Arfaee <[EMAIL PROTECTED]> wrote: > I need a powerset generator function. It's really slow with recursion. Does > anybody have any idea or code(!!) to do it in an acceptable time? > Thanks > -Arash I thought that this was a really interesting question, so I wrote up a solution th

Re: Fast powerset function

2007-07-12 Thread Carsten Haese
On Thu, 12 Jul 2007 21:33:11 -0700, Arash Arfaee wrote > I need a powerset generator function. It's really slow with recursion. Does anybody have any idea or code(!!) to do it in an acceptable time? 1) Don't use recursion. 2) Google is your friend. -Carsten -- http://mail.python.org/mailman/

Fast powerset function

2007-07-12 Thread Arash Arfaee
I need a powerset generator function. It's really slow with recursion. Does anybody have any idea or code(!!) to do it in an acceptable time? Thanks -Arash -- http://mail.python.org/mailman/listinfo/python-list