473,604 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Most elegant way to generate 3-char sequence

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
28 3037
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.lowercas e

def generator(choic es, length):
for a in choices:
if length > 1:
for g in generator(choic es, 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
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.lowercas e

def generator(choic es, length):
length -= 1
for a in choices:
if length:
for g in generator(choic es, 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

"Rob Cowie" <co*******@gmai l.com> wrote in message
news:11******** **************@ h76g2000cwa.goo glegroups.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
On 2006-06-09, Rob Cowie <co*******@gmai l.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
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_lo wercase
.... 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
alpha = string.lowercas e
x=(a+b+c for a in alpha for b in alpha for c in alpha)
Jun 9 '06 #7
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.lowercas e

def generator(choic es, length):
length -= 1
if length:
for a in choices:
for g in generator(choic es, 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
and the winner is... :D
David Isaac wrote:
alpha = string.lowercas e
x=(a+b+c for a in alpha for b in alpha for c in alpha)

Jun 10 '06 #9
SuperHik wrote:
and the winner is... :D
David Isaac wrote:
alpha = string.lowercas e
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

aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa
aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aab
..
..
..
zzzzzzzzzzzzzzz zzzzzzzzzzzzzzz zzzzzzzzzzzzzzz zzzzzzzzzzzzzzz zzy
zzzzzzzzzzzzzzz zzzzzzzzzzzzzzz zzzzzzzzzzzzzzz zzzzzzzzzzzzzzz zzz
James

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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5007
by: Kamilche | last post by:
Is there a more elegant way to change the working directory of Python to the directory of the currently executing script, and add a folder called 'Shared' to the Python search path? This is what I have. It seems like it could be shorter, somehow. # Switch Python to the current directory import os, sys pathname, scriptname = os.path.split(sys.argv) pathname = os.path.abspath(pathname)
3
1977
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function in only objects of class B and skip over the objects of class C. To complicate things, I'm using the vector position (index) as an argument in the invoked member function. It is possible to move the position into the object by adding a data...
3
1290
by: Brent Minder | last post by:
What is the most efficient way to code asp.net pages when you break your page up into user controls? For example: If you have a page with a header (control .ascx), body, and footer (control .ascx) for a given page and you need to access functions within a class (bizUtil say) should you declare the bizUtil object on the header and footer in both the aspx and aspx.vb pages? That's FOUR references! This seems bloatful (my new word).
14
4428
by: Dario de Judicibus | last post by:
I think that is really important to have the «src» attrinbute in tags other than «img» in order to include XHTML fragments without depending on server-side mechanisms as ISS or PHP. For example: <div src="fragment.xhtml" /> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Dr. Dario de Judicibus - Italy (EU) Site: http://www.dejudicibus.it/ Blog: http://lindipendente.splinder.com
4
1508
by: arak123 | last post by:
consider the following oversimplified and fictional code public void CreateInvoices(Invoice invoices) { IDbCommand command=Util.CreateDbCommand(); foreach(Invoice invoice in invoices) //lets say you have 200 invoices { command.CommandText+="INSERT INTO Invoice(Amount)
1
1473
by: Wolfgang Draxinger | last post by:
I'm currently developing vegetation creation scripts for the Blender 3D modelling software. The first attempts work nice, but they're far to inflexible IMHO. First let me introduce how the system works: The user supplies a series of building elements of the plants to be generated (e.g. stem, stipe, leaf, branches, blossom) and associated those with template 3D models. For each of those elements there is a list, which elements can be...
11
1524
by: alexs | last post by:
Hi, I've got a Java stored procedure that I use to "register" systems on our network. In the middle of it is a function that returns the 1st free ip address in a class C network which is used in the registration process. In this process a free ip address is defined as a number between 1 and 254 At the moment I'm fetching an ordered list of ip addresses (which will
32
2016
by: r.z. | last post by:
class vector3 { public: union { float data; struct { float x, y, z; };
2
2147
by: Duk Lee | last post by:
What is the most elegant way to clear all the text fields on asp.net page? I just don't think that txtShortName.Text = "" txtYearFounded.Text = "" txtCompanyCode.Text = "" txtCity.Text = "" txtOwnership.Text = "" txtAssetsUnderManagement.Text = "" txtNumberOfAnalysts.Text = ""
3
1968
by: Thomas Pajor | last post by:
Hey everybody, I got into serious trouble with template programming. I have a class which uses three template arguments, say template<typename Atype, typename Btype, typename Ctype> class some_class { };
0
7929
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8409
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8280
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5882
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3955
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2434
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1526
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.