By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,830 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,830 IT Pros & Developers. It's quick & easy.

Most elegant way to generate 3-char sequence

P: n/a
Hi all,

I wish to generate a sequence of the form 'aaa', 'aab', aac'.... 'aba',
'abb', 'abc' etc. all the way to 'zzz'.

How would you construct a generator to acheive this?

A simple, working but somewhat inelegant solution is...

alpha = ['a','b','c','d'] #shortened for brevity
alpha2 = ['a','b','c','d']
alpha3 = ['a','b','c','d']

def generator():
for char in alpha:
for char2 in alpha2:
for char3 in alpha3:
yield char + char2 + char3

x = generate()
x.next() # etc, etc, etc,

Jun 9 '06 #1
Share this Question
Share on Google+
28 Replies


P: n/a
Rob Cowie wrote:
Hi all,

I wish to generate a sequence of the form 'aaa', 'aab', aac'.... 'aba',
'abb', 'abc' etc. all the way to 'zzz'.

How would you construct a generator to acheive this?

A simple, working but somewhat inelegant solution is...

alpha = ['a','b','c','d'] #shortened for brevity
alpha2 = ['a','b','c','d']
alpha3 = ['a','b','c','d']

def generator():
for char in alpha:
for char2 in alpha2:
for char3 in alpha3:
yield char + char2 + char3

x = generate()
x.next() # etc, etc, etc,


import string
alpha = string.lowercase

def generator(choices, length):
for a in choices:
if length > 1:
for g in generator(choices, length-1):
yield a + g
else:
yield a

for a in generator(alpha, 3):
print a
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jun 9 '06 #2

P: n/a
Rob Cowie wrote:
Hi all,

I wish to generate a sequence of the form 'aaa', 'aab', aac'.... 'aba',
'abb', 'abc' etc. all the way to 'zzz'.

How would you construct a generator to acheive this?

A simple, working but somewhat inelegant solution is...

alpha = ['a','b','c','d'] #shortened for brevity
alpha2 = ['a','b','c','d']
alpha3 = ['a','b','c','d']

def generator():
for char in alpha:
for char2 in alpha2:
for char3 in alpha3:
yield char + char2 + char3

x = generate()
x.next() # etc, etc, etc,


A touch more efficient:

import string
alpha = string.lowercase

def generator(choices, length):
length -= 1
for a in choices:
if length:
for g in generator(choices, length):
yield a + g
else:
yield a

for a in generator(alpha, 3):
print a

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jun 9 '06 #3

P: n/a

"Rob Cowie" <co*******@gmail.com> wrote in message
news:11**********************@h76g2000cwa.googlegr oups.com...
alpha = ['a','b','c','d'] #shortened for brevity
alpha2 = ['a','b','c','d']
alpha3 = ['a','b','c','d']

def generator():
for char in alpha:
for char2 in alpha2:
for char3 in alpha3:
yield char + char2 + char3


Basically fine, but you only need one string. E.g.,
alpha = "abcd"
used three times.

Alan Isaac
Jun 9 '06 #4

P: n/a
On 2006-06-09, Rob Cowie <co*******@gmail.com> wrote:
Hi all,

I wish to generate a sequence of the form 'aaa', 'aab', aac'.... 'aba',
'abb', 'abc' etc. all the way to 'zzz'.

How would you construct a generator to acheive this?

A simple, working but somewhat inelegant solution is...

alpha = ['a','b','c','d'] #shortened for brevity
alpha2 = ['a','b','c','d']
alpha3 = ['a','b','c','d']

def generator():
for char in alpha:
for char2 in alpha2:
for char3 in alpha3:
yield char + char2 + char3


Why three separate lists? Why not

alpha = ['a','b','c','d'] #shortened for brevity

def generator():
for char in alpha:
for char2 in alpha:
for char3 in alpha:
yield char + char2 + char3

--
Grant Edwards grante Yow! I think I am an
at overnight sensation right
visi.com now!!
Jun 9 '06 #5

P: n/a
On 10/06/2006 7:49 AM, Rob Cowie wrote:
Hi all,

I wish to generate a sequence of the form 'aaa', 'aab', aac'.... 'aba',
'abb', 'abc' etc. all the way to 'zzz'.

How would you construct a generator to acheive this?

A simple, working but somewhat inelegant solution is...
You're not wrong.

alpha = ['a','b','c','d'] #shortened for brevity
Hope you remember the alphabet correctly.
Why type all that punctuation?
Any reason this cruft is global (i.e. not local to the generator)?
alpha2 = ['a','b','c','d']
alpha3 = ['a','b','c','d']
Hope you get the redundant copy/paste right.

def generator():
for char in alpha:
Why stop at two spaces? One-space indentation is syntactically correct :-)
for char2 in alpha2:
for char3 in alpha3:
yield char + char2 + char3

x = generate()
Did you meant "generator"?
x.next() # etc, etc, etc,


|>> def generator():
.... import string
.... alpha = string.ascii_lowercase
.... for char in alpha:
.... for char2 in alpha:
.... for char3 in alpha:
.... yield char + char2 + char3
....
|>> x = generator()
|>> the_lot = list(x)
|>> len(the_lot) == 26 ** 3
True
|>> [the_lot[i] for i in (0, 1, 25, 26, -27, -26, -1)]
['aaa', 'aab', 'aaz', 'aba', 'zyz', 'zza', 'zzz']

Cheers,
John
Jun 9 '06 #6

P: n/a
alpha = string.lowercase
x=(a+b+c for a in alpha for b in alpha for c in alpha)
Jun 9 '06 #7

P: n/a
Rob Cowie wrote:
Hi all,

I wish to generate a sequence of the form 'aaa', 'aab', aac'.... 'aba',
'abb', 'abc' etc. all the way to 'zzz'.

How would you construct a generator to acheive this?

A simple, working but somewhat inelegant solution is...

alpha = ['a','b','c','d'] #shortened for brevity
alpha2 = ['a','b','c','d']
alpha3 = ['a','b','c','d']

def generator():
for char in alpha:
for char2 in alpha2:
for char3 in alpha3:
yield char + char2 + char3

x = generate()
x.next() # etc, etc, etc,


Yet a little more efficient.

import string
alpha = string.lowercase

def generator(choices, length):
length -= 1
if length:
for a in choices:
for g in generator(choices, length):
yield a + g
else:
for a in choices:
yield a

for a in generator(alpha, 3):
print a

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jun 9 '06 #8

P: n/a
and the winner is... :D
David Isaac wrote:
alpha = string.lowercase
x=(a+b+c for a in alpha for b in alpha for c in alpha)

Jun 10 '06 #9

P: n/a
SuperHik wrote:
and the winner is... :D
David Isaac wrote:
alpha = string.lowercase
x=(a+b+c for a in alpha for b in alpha for c in alpha)



Not necessarily vying for winner, but David's solution is highly
specific as it doesn't do so well for something like

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaab
..
..
..
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzy
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzz
James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jun 10 '06 #10

P: n/a
John Machin wrote:
On 10/06/2006 7:49 AM, Rob Cowie wrote:
Hi all,

I wish to generate a sequence of the form 'aaa', 'aab', aac'.... 'aba',
'abb', 'abc' etc. all the way to 'zzz'.

How would you construct a generator to acheive this?

A simple, working but somewhat inelegant solution is...
You're not wrong.

alpha = ['a','b','c','d'] #shortened for brevity


Hope you remember the alphabet correctly.

Not sure I understand your point. Why would I forget the alphabet?
Why type all that punctuation? What punctuation?
Any reason this cruft is global (i.e. not local to the generator)? No
alpha2 = ['a','b','c','d']
alpha3 = ['a','b','c','d']
Hope you get the redundant copy/paste right.

Again, I don't understand what you mean

def generator():
for char in alpha:
Why stop at two spaces? One-space indentation is syntactically correct :-)

As are 3, 4 and 5 space indentation. Yet again, what is your point?
for char2 in alpha2:
for char3 in alpha3:
yield char + char2 + char3

x = generate()
Did you meant "generator"?

Yes, made a mistake
x.next() # etc, etc, etc,


|>> def generator():
... import string
... alpha = string.ascii_lowercase
... for char in alpha:
... for char2 in alpha:
... for char3 in alpha:
... yield char + char2 + char3
...
|>> x = generator()
|>> the_lot = list(x)
|>> len(the_lot) == 26 ** 3
True
|>> [the_lot[i] for i in (0, 1, 25, 26, -27, -26, -1)]
['aaa', 'aab', 'aaz', 'aba', 'zyz', 'zza', 'zzz']

Cheers,
John


I'm aware the code I posted is not great - it isn't code I would
consider using. It was only intended to serve as an illustration of the
task at hand in case my explanation wasn't sufficient.

I'm grateful to you for using list(generator) in your example. I was
not aware this could be done (I haven't yet fully read the generator
documentation).

Rob C

Jun 10 '06 #11

P: n/a
Rob Cowie wrote:
Why type all that punctuation?


What punctuation?


['','','','']['','','','']['','','','']

(also see david isaac's post)

</F>

Jun 10 '06 #12

P: n/a
On 10/06/2006 7:01 PM, Rob Cowie wrote:
John Machin wrote:
On 10/06/2006 7:49 AM, Rob Cowie wrote:
Hi all,

I wish to generate a sequence of the form 'aaa', 'aab', aac'.... 'aba',
'abb', 'abc' etc. all the way to 'zzz'.

How would you construct a generator to acheive this?

A simple, working but somewhat inelegant solution is... You're not wrong.
alpha = ['a','b','c','d'] #shortened for brevity

Hope you remember the alphabet correctly.

Not sure I understand your point. Why would I forget the alphabet?


.... or mistype it? If you use string.ascii_lowercase, your readers know
what is intended without checking that it is in fact that.
Why type all that punctuation? What punctuation?


Refer to the effbot's post.
Any reason this cruft is global (i.e. not local to the generator)? No


The point being that gizmoids should not be given a wider visibility
than is required. A handy side effect is that it is more efficient to
reference a local than a global (global names have to be looked up in a
dictionary).
alpha2 = ['a','b','c','d']
alpha3 = ['a','b','c','d'] Hope you get the redundant copy/paste right.

Again, I don't understand what you mean


You need only one alpha, not 3. Otherwise your readers have to compare
alpha to alpha2 and alpha2 to alpha3 to ascertain that you haven't typoed.
def generator():
for char in alpha: Why stop at two spaces? One-space indentation is syntactically correct :-)

As are 3, 4 and 5 space indentation. Yet again, what is your point?


4-space indentation is more or less standard. 2-space indentation is
definitely not an aid to swift comprehension of Python code.
for char2 in alpha2:
for char3 in alpha3:
yield char + char2 + char3

x = generate() Did you meant "generator"?

Yes, made a mistake
x.next() # etc, etc, etc,

|>> def generator():
... import string
... alpha = string.ascii_lowercase
... for char in alpha:
... for char2 in alpha:
... for char3 in alpha:
... yield char + char2 + char3
...
|>> x = generator()
|>> the_lot = list(x)
|>> len(the_lot) == 26 ** 3
True
|>> [the_lot[i] for i in (0, 1, 25, 26, -27, -26, -1)]
['aaa', 'aab', 'aaz', 'aba', 'zyz', 'zza', 'zzz']

Cheers,
John


I'm aware the code I posted is not great - it isn't code I would
consider using.


If you wouldn't consider using it, don't drag it out in public. Some
n00b might think it the epitome of good style :-)
It was only intended to serve as an illustration of the
task at hand in case my explanation wasn't sufficient.

I'm grateful to you for using list(generator) in your example. I was
not aware this could be done (I haven't yet fully read the generator
documentation).


A generator is an iterable. list(), sum(), max(), min(), enumerate(),
zip() etc work on iterables. It's that simple, and that powerful.

You might be interested in the itertools standard module as well.

Cheers,
John
Jun 10 '06 #13

P: n/a
James Stroud wrote:
SuperHik wrote:
and the winner is... :D
David Isaac wrote:
alpha = string.lowercase
x=(a+b+c for a in alpha for b in alpha for c in alpha)


Not necessarily vying for winner, but David's solution is highly
specific as it doesn't do so well for something like

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaab
.
.
.
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzy
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzz


Right. But that wasn't the question :p

James

Jun 10 '06 #14

P: n/a
James Stroud wrote:
Rob Cowie wrote:
Hi all,

I wish to generate a sequence of the form 'aaa', 'aab', aac'.... 'aba',
'abb', 'abc' etc. all the way to 'zzz'.

How would you construct a generator to acheive this?

A simple, working but somewhat inelegant solution is...

alpha = ['a','b','c','d'] #shortened for brevity
alpha2 = ['a','b','c','d']
alpha3 = ['a','b','c','d']

def generator():
for char in alpha:
for char2 in alpha2:
for char3 in alpha3:
yield char + char2 + char3

x = generate()
x.next() # etc, etc, etc,

A touch more efficient:

import string
alpha = string.lowercase

def generator(choices, length):
length -= 1
for a in choices:
if length:
for g in generator(choices, length):
yield a + g
else:
yield a

for a in generator(alpha, 3):
print a

Frankly, this doesn't seem more elegant than the original, particularly
once it uses a single string.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jun 10 '06 #15

P: n/a
James Stroud wrote:
SuperHik wrote:
and the winner is... :D
David Isaac wrote:

alpha = string.lowercase
x=(a+b+c for a in alpha for b in alpha for c in alpha)



Not necessarily vying for winner, but David's solution is highly
specific as it doesn't do so well for something like

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaab
.
.
.
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzy
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzz

You can't justify your solution by requirements gold-plating ... see the
subject line :-)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jun 10 '06 #16

P: n/a
SuperHik wrote:
James Stroud wrote:
SuperHik wrote:
and the winner is... :D
David Isaac wrote:

alpha = string.lowercase
x=(a+b+c for a in alpha for b in alpha for c in alpha)


Not necessarily vying for winner, but David's solution is highly
specific as it doesn't do so well for something like

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaab
.
.
.
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzy
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzz

Right. But that wasn't the question :p


James


highly specific
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jun 10 '06 #17

P: n/a
Steve Holden wrote:
James Stroud wrote:
Rob Cowie wrote:
Hi all,

I wish to generate a sequence of the form 'aaa', 'aab', aac'.... 'aba',
'abb', 'abc' etc. all the way to 'zzz'.

How would you construct a generator to acheive this?

A simple, working but somewhat inelegant solution is...

alpha = ['a','b','c','d'] #shortened for brevity
alpha2 = ['a','b','c','d']
alpha3 = ['a','b','c','d']

def generator():
for char in alpha:
for char2 in alpha2:
for char3 in alpha3:
yield char + char2 + char3

x = generate()
x.next() # etc, etc, etc,

A touch more efficient:

import string
alpha = string.lowercase

def generator(choices, length):
length -= 1
for a in choices:
if length:
for g in generator(choices, length):
yield a + g
else:
yield a

for a in generator(alpha, 3):
print a

Frankly, this doesn't seem more elegant than the original, particularly
once it uses a single string.

regards
Steve


Are you sure you understand what each do?

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jun 10 '06 #18

P: n/a
Steve Holden wrote:
James Stroud wrote:
SuperHik wrote:
and the winner is... :D
David Isaac wrote:
alpha = string.lowercase
x=(a+b+c for a in alpha for b in alpha for c in alpha)


Not necessarily vying for winner, but David's solution is highly
specific as it doesn't do so well for something like

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaab
.
.
.
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzy
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzz

You can't justify your solution by requirements gold-plating ... see the
subject line :-)

regards
Steve

Not necessarily vying for winner

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jun 10 '06 #19

P: n/a
James Stroud wrote:
SuperHik wrote:
James Stroud wrote:
SuperHik wrote:

and the winner is... :D
David Isaac wrote:

> alpha = string.lowercase
> x=(a+b+c for a in alpha for b in alpha for c in alpha)


Not necessarily vying for winner, but David's solution is highly
specific as it doesn't do so well for something like

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaab
.
.
.
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzy
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzz


Right. But that wasn't the question :p


James


highly specific


The only question mark was in this sentence, which I believe I answered.

How would you construct a generator to acheive this?

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jun 10 '06 #20

P: n/a
Steve Holden wrote:
James Stroud wrote:
SuperHik wrote:
and the winner is... :D
David Isaac wrote:
alpha = string.lowercase
x=(a+b+c for a in alpha for b in alpha for c in alpha)


Not necessarily vying for winner, but David's solution is highly
specific as it doesn't do so well for something like

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaab
.
.
.
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzy
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzz

You can't justify your solution by requirements gold-plating ... see the
subject line :-)

regards
Steve


See the actual question:
How would you construct a generator to acheive this?


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jun 10 '06 #21

P: n/a
I'm not sure if you ever got a good answer. I for one am too lazy to
look up the pages I found... but - check out
http://aspn.activestate.com/ASPN/sea...ype=Subsection

Tons of nice recipes... Personally I liked the one that dynamically
generated a nested function - just cuz it was cool - total overkill for
a 3 element permutation. otherwise a simple nested generator is
perfect for this solution. Actually you could use the generator
function generator to show you what the generator function should look
like - try sayin that five times fast.
Rob Cowie wrote:
Hi all,

I wish to generate a sequence of the form 'aaa', 'aab', aac'.... 'aba',
'abb', 'abc' etc. all the way to 'zzz'.

How would you construct a generator to acheive this?

A simple, working but somewhat inelegant solution is...

alpha = ['a','b','c','d'] #shortened for brevity
alpha2 = ['a','b','c','d']
alpha3 = ['a','b','c','d']

def generator():
for char in alpha:
for char2 in alpha2:
for char3 in alpha3:
yield char + char2 + char3

x = generate()
x.next() # etc, etc, etc,


Jun 11 '06 #22

P: n/a
2006/6/10, SuperHik <ju************@gmail.com>:
Not necessarily vying for winner, but David's solution is highly
specific as it doesn't do so well for something like

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaab


Right. But that wasn't the question :p


The question was about elegance, and elegance is when someone asks "do
something 4 times for 5 strings of length 3" and you solve it for "do
something n times for m strings of length p" :)
--
Roberto Bonvallet
Jun 11 '06 #23

P: n/a
James Stroud wrote:
See the actual question:
>How would you construct a generator to acheive this?


if you don't think the context provided by the subject line and the
sentence before the question is important, how come you're so sure what
"this" refers to ?

</F>

Jun 11 '06 #24

P: n/a
Fredrik Lundh wrote:
James Stroud wrote:
See the actual question:
>How would you construct a generator to acheive this?

if you don't think the context provided by the subject line and the
sentence before the question is important, how come you're so sure what
"this" refers to ?

</F>


I'm getting the feeling that "correct" answers have nothing to do with
correctness (i.e. working code) but on some vague notion of "elegance".
Please point me to the page where code elegance is precisely defined so
that I may construct my answers appropriately. And yes, I am too lazy to
google "code elegance".

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jun 11 '06 #25

P: n/a
sam
I have found that the more elegant the code is, the harder it is for me
to understand what it is trying to accomplish. It is my opinion that
"Keep It Simple" wins over elegance. When I have had the urge to get
elegant, I make sure I comment the elegance so my less elegant
co-workers can figure out what I was trying to accomplish.

Sam Schulenburg
James Stroud wrote:
Fredrik Lundh wrote:
James Stroud wrote:
See the actual question:

>How would you construct a generator to acheive this?

if you don't think the context provided by the subject line and the
sentence before the question is important, how come you're so sure what
"this" refers to ?

</F>


I'm getting the feeling that "correct" answers have nothing to do with
correctness (i.e. working code) but on some vague notion of "elegance".
Please point me to the page where code elegance is precisely defined so
that I may construct my answers appropriately. And yes, I am too lazy to
google "code elegance".

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/


Jun 11 '06 #26

P: n/a
sam wrote:
I have found that the more elegant the code is, the harder it is for me
to understand what it is trying to accomplish. It is my opinion that
"Keep It Simple" wins over elegance. When I have had the urge to get
elegant, I make sure I comment the elegance so my less elegant
co-workers can figure out what I was trying to accomplish.

Sam Schulenburg

+1

Petr Jakes

Jun 12 '06 #27

P: n/a
John Machin a écrit :
On 10/06/2006 7:49 AM, Rob Cowie wrote:

(snip)

def generator():
for char in alpha:

Why stop at two spaces? One-space indentation is syntactically correct :-)


I very often uses 2-spaces indent when posting here, to avoid problems
with wrapping.

Jun 12 '06 #28

P: n/a
James Stroud a écrit :
SuperHik wrote:
and the winner is... :D
David Isaac wrote:
alpha = string.lowercase
x=(a+b+c for a in alpha for b in alpha for c in alpha)



Not necessarily vying for winner, but David's solution is highly
specific as it doesn't do so well for something like

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaab
.
.
.
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzy
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzz


And ? Was it asked for ?

I'd personaly start with David's solution - IOW : simplest thing that
could possibly work -, and only bother making it more generic if/when I
have a use case for a more generic solution.

Premature generalisation is the root of all evil !-)
Jun 12 '06 #29

This discussion thread is closed

Replies have been disabled for this discussion.