Re: interesting exercise

2007-05-09 Thread castironpi
On May 9, 7:49 pm, Charles Sanders <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > On May 9, 1:13 am, Charles Sanders <[EMAIL PROTECTED]> > > wrote: > [snip] > >> or even this monstrosity ... > > >> def permute2( s, n ): > >>return [ ''.join([ s[int(i/len(s)**j)%len(s)] > >> for

Re: interesting exercise

2007-05-09 Thread Charles Sanders
[EMAIL PROTECTED] wrote: > On May 9, 1:13 am, Charles Sanders <[EMAIL PROTECTED]> > wrote: [snip] >> or even this monstrosity ... >> >> def permute2( s, n ): >>return [ ''.join([ s[int(i/len(s)**j)%len(s)] >> for j in range(n-1,-1,-1)]) >>for i in range(len(s)**n) ] >> >> print "pe

Re: interesting exercise

2007-05-09 Thread Michael Tobis
On May 9, 2:41 pm, [EMAIL PROTECTED] wrote: > On May 9, 1:13 am, Charles Sanders <[EMAIL PROTECTED]> > wrote: > > or even this monstrosity ... > > > def permute2( s, n ): > >return [ ''.join([ s[int(i/len(s)**j)%len(s)] > > for j in range(n-1,-1,-1)]) > >for i in range(len(s)**n)

Re: interesting exercise

2007-05-09 Thread castironpi
On May 9, 1:13 am, Charles Sanders <[EMAIL PROTECTED]> wrote: > Michael Tobis wrote: > > Here is the bloated mess I came up with. I did see that it had to be > > recursive, and was proud of myself for getting it pretty much on the > > first try, but the thing still reeks of my sorry old fortran-add

Re: interesting exercise

2007-05-08 Thread Charles Sanders
Michael Tobis wrote: > Here is the bloated mess I came up with. I did see that it had to be > recursive, and was proud of myself for getting it pretty much on the > first try, but the thing still reeks of my sorry old fortran-addled > mentality. Recursion is not necessary, but is much, much cleare

Re: interesting exercise

2007-05-08 Thread Michael Tobis
Thanks castironpi and alex, for this: def p(a,b): if not b: return [''] return [i+j for i in a for j in p(a,b-1)] That is a thing of beauty! As usual you guys didn't disappoint. (Validity check of alphabet removed; you didn't check for duplicate characters.) Here is the bloated

Re: interesting exercise

2007-05-08 Thread castironpi
On May 8, 4:55 pm, [EMAIL PROTECTED] wrote: > On May 8, 3:55 pm, James Stroud <[EMAIL PROTECTED]> wrote: > > > > > Steven D'Aprano wrote: > > > On Tue, 08 May 2007 10:22:05 +, James Stroud wrote: > > > >>This takes annoying past annoying to some new level of hell to which > > >>even satan himse

Re: interesting exercise

2007-05-08 Thread castironpi
On May 8, 3:55 pm, James Stroud <[EMAIL PROTECTED]> wrote: > Steven D'Aprano wrote: > > On Tue, 08 May 2007 10:22:05 +, James Stroud wrote: > > >>This takes annoying past annoying to some new level of hell to which > >>even satan himself wouldn't venture. > > > And thank you for sharing that pi

Re: interesting exercise

2007-05-08 Thread James Stroud
Steven D'Aprano wrote: > On Tue, 08 May 2007 10:22:05 +, James Stroud wrote: > > >>This takes annoying past annoying to some new level of hell to which >>even satan himself wouldn't venture. > > > And thank you for sharing that piece of spam with us again. It was so much > less enjoyable t

Re: interesting exercise

2007-05-08 Thread Steven D'Aprano
On Tue, 08 May 2007 10:22:05 +, James Stroud wrote: > This takes annoying past annoying to some new level of hell to which > even satan himself wouldn't venture. And thank you for sharing that piece of spam with us again. It was so much less enjoyable to see it the second time. Seriously Ja

Re: interesting exercise

2007-05-08 Thread Carsten Haese
On Mon, 2007-05-07 at 22:20 -0700, sherry wrote: > On May 8, 9:31 am, Steven D'Aprano > <[EMAIL PROTECTED]> wrote: > > [snip Steven's response about a programming exercise...] > > [snip Sherrybove's spam about physical exercise...] And today's award for the most conspicuous failure of the Turing

Re: interesting exercise

2007-05-08 Thread James Stroud
sherry wrote: > On May 8, 9:31 am, Steven D'Aprano > <[EMAIL PROTECTED]> wrote: >> On Mon, 07 May 2007 20:45:52 -0700, Michael Tobis wrote: >>> I have a reasonably elegant solution but it's a bit verbose (a couple >>> dozen lines which I'll post later if there is interest). Is there some >>> clever

Re: interesting exercise

2007-05-07 Thread Christopher Cormie
Michael Tobis wrote: > I want a list of all ordered permutations of a given length of a set > of tokens. Each token is a single character, and for convenience, they > are passed as a string in ascending ASCII order. > > For example > > permute("abc",2) > > should return ["aa","ab","ac","ba","bb"

Re: interesting exercise

2007-05-07 Thread castironpi
On May 8, 12:24 am, [EMAIL PROTECTED] (Alex Martelli) wrote: > <[EMAIL PROTECTED]> wrote: > >... > > > def p(a,b): > > if list( a ) != sorted( list( a ) ): raise ValueError, "String not > > ordered." > > if not b: return [''] > > return [i+j for i in list(a) for j in p(a,b-1)]

Re: interesting exercise

2007-05-07 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote: ... > def p(a,b): > if list( a ) != sorted( list( a ) ): raise ValueError, "String not > ordered." > if not b: return [''] > return [i+j for i in list(a) for j in p(a,b-1)] No need for 2/3 of the list(...) calls. sorted(a) and sorted(list(a)) will A

Re: interesting exercise

2007-05-07 Thread sherry
On May 8, 9:31 am, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Mon, 07 May 2007 20:45:52 -0700, Michael Tobis wrote: > > I have a reasonably elegant solution but it's a bit verbose (a couple > > dozen lines which I'll post later if there is interest). Is there some > > clever Pythonism I didn't

Re: interesting exercise

2007-05-07 Thread castironpi
On May 7, 11:42 pm, [EMAIL PROTECTED] wrote: > On May 7, 11:34 pm, [EMAIL PROTECTED] wrote: > > > > > On May 7, 10:45 pm, Michael Tobis <[EMAIL PROTECTED]> wrote: > > > > I want a list of all ordered permutations of a given length of a set > > > of tokens. Each token is a single character, and for

Re: interesting exercise

2007-05-07 Thread castironpi
On May 7, 11:34 pm, [EMAIL PROTECTED] wrote: > On May 7, 10:45 pm, Michael Tobis <[EMAIL PROTECTED]> wrote: > > > > > I want a list of all ordered permutations of a given length of a set > > of tokens. Each token is a single character, and for convenience, they > > are passed as a string in ascendi

Re: interesting exercise

2007-05-07 Thread Gabriel Genellina
En Tue, 08 May 2007 01:33:51 -0300, Steven D'Aprano <[EMAIL PROTECTED]> escribió: > On Tue, 08 May 2007 01:21:37 -0300, Gabriel Genellina wrote: > >> if not sorted(values): raise ValueError("unsorted values") > > sorted() doesn't return a flag telling if the values are sorted, it > returns a new

Re: interesting exercise

2007-05-07 Thread castironpi
On May 7, 10:45 pm, Michael Tobis <[EMAIL PROTECTED]> wrote: > I want a list of all ordered permutations of a given length of a set > of tokens. Each token is a single character, and for convenience, they > are passed as a string in ascending ASCII order. > > For example > > permute("abc",2) > > sh

Re: interesting exercise

2007-05-07 Thread Steven D'Aprano
On Tue, 08 May 2007 01:21:37 -0300, Gabriel Genellina wrote: > if not sorted(values): raise ValueError("unsorted values") sorted() doesn't return a flag telling if the values are sorted, it returns a new list containing values sorted. So this line will raise an exception only on an empty sequen

Re: interesting exercise

2007-05-07 Thread Steven D'Aprano
On Mon, 07 May 2007 20:45:52 -0700, Michael Tobis wrote: > I have a reasonably elegant solution but it's a bit verbose (a couple > dozen lines which I'll post later if there is interest). Is there some > clever Pythonism I didn't spot? Peering into my crystal ball, I see that your algorithm does

Re: interesting exercise

2007-05-07 Thread Gabriel Genellina
En Tue, 08 May 2007 00:45:52 -0300, Michael Tobis <[EMAIL PROTECTED]> escribió: > I want a list of all ordered permutations of a given length of a set > of tokens. Each token is a single character, and for convenience, they > are passed as a string in ascending ASCII order. This is what I come,

Re: interesting exercise

2007-05-07 Thread James Stroud
Michael Tobis wrote: > I want a list of all ordered permutations of a given length of a set > of tokens. Each token is a single character, and for convenience, they > are passed as a string in ascending ASCII order. > > For example > > permute("abc",2) > > should return ["aa","ab","ac","ba","bb"

interesting exercise

2007-05-07 Thread Michael Tobis
I want a list of all ordered permutations of a given length of a set of tokens. Each token is a single character, and for convenience, they are passed as a string in ascending ASCII order. For example permute("abc",2) should return ["aa","ab","ac","ba","bb","bc","ca","cb","cc"] and permute("135