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

Efficient way of generating original alphabetic strings like unix file "split"

P: n/a
Hi,

I'm looking to generate x alphabetic strings in a list size x. This
is exactly the same output that the unix command "split" generates as
default file name output when splitting large files.

Example:

produce x original, but not random strings from english alphabet, all
lowercase. The length of each string and possible combinations is
dependent on x. You don't want any repeats.

[aaa, aab, aac, aad, .... aax, ...... bbc, bbd, .... bcd]

I'm assumming there is a slick, pythonic way of doing this, besides
writing out a beast of a looping function. I've looked around on
activestate cookbook, but have come up empty handed. Any suggestions?

Thanks,
Conor

Jun 14 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Jun 14, 1:41 pm, py_genetic <conor.robin...@gmail.comwrote:
Hi,

I'm looking to generate x alphabetic strings in a list size x. This
is exactly the same output that the unix command "split" generates as
default file name output when splitting large files.

Example:

produce x original, but not random strings from english alphabet, all
lowercase. The length of each string and possible combinations is
dependent on x. You don't want any repeats.

[aaa, aab, aac, aad, .... aax, ...... bbc, bbd, .... bcd]

I'm assumming there is a slick, pythonic way of doing this, besides
writing out a beast of a looping function. I've looked around on
activestate cookbook, but have come up empty handed. Any suggestions?
If you allow numbers also, you can use Base 36:
>>import gmpy
int('aaa',36)
13330
>>for n in xrange(13330,13330+1000):
print gmpy.digits(n,36),

aaa aab aac aad aae aaf aag aah aai aaj aak aal
aam aan aao aap aaq aar aas aat aau aav aaw aax
aay aaz ab0 ab1 ab2 ab3 ab4 ab5 ab6 ab7 ab8 ab9
aba abb abc abd abe abf abg abh abi abj abk abl
abm abn abo abp abq abr abs abt abu abv abw abx
aby abz ac0 ac1 ac2 ac3 ac4 ac5 ac6 ac7 ac8 ac9
aca acb acc acd ace acf acg ach aci acj ack acl
acm acn aco acp acq acr acs act acu acv acw acx
acy acz ad0 ad1 ad2 ad3 ad4 ad5 ad6 ad7 ad8 ad9
ada adb adc add ade adf adg adh adi adj adk adl
adm adn ado adp adq adr ads adt adu adv adw adx
ady adz ae0 ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 ae9
aea aeb aec aed aee aef aeg aeh aei aej aek ael
aem aen aeo aep ...

>
Thanks,
Conor

Jun 14 '07 #2

P: n/a
py_genetic <co************@gmail.comwrites:
Hi,

I'm looking to generate x alphabetic strings in a list size x. This
is exactly the same output that the unix command "split" generates as
default file name output when splitting large files.

Example:

produce x original, but not random strings from english alphabet, all
lowercase. The length of each string and possible combinations is
dependent on x. You don't want any repeats.

[aaa, aab, aac, aad, .... aax, ...... bbc, bbd, .... bcd]

I'm assumming there is a slick, pythonic way of doing this, besides
writing out a beast of a looping function. I've looked around on
activestate cookbook, but have come up empty handed. Any suggestions?
You didn't try hard enough. :)

http://aspn.activestate.com/ASPN/Coo.../Recipe/190465

--
HTH,
Rob
Jun 14 '07 #3

P: n/a
>
You didn't try hard enough. :)

http://aspn.activestate.com/ASPN/Coo.../Recipe/190465

--
HTH,
Rob
Thanks Rob, "permutation" was the keyword I shcould have used!

Jun 14 '07 #4

P: n/a
On Jun 14, 3:08 pm, Rob Wolfe <r...@smsnet.plwrote:
py_genetic <conor.robin...@gmail.comwrites:
Hi,
I'm looking to generate x alphabetic strings in a list size x. This
is exactly the same output that the unix command "split" generates as
default file name output when splitting large files.
Example:
produce x original, but not random strings from english alphabet, all
lowercase. The length of each string and possible combinations is
dependent on x. You don't want any repeats.
[aaa, aab, aac, aad, .... aax, ...... bbc, bbd, .... bcd]
I'm assumming there is a slick, pythonic way of doing this, besides
writing out a beast of a looping function. I've looked around on
activestate cookbook, but have come up empty handed. Any suggestions?

You didn't try hard enough. :)

http://aspn.activestate.com/ASPN/Coo.../Recipe/190465

Unfortunately, that's a very poor example. The terminaology is
all wrong.

"xpermutations takes all elements from the sequence, order matters."
This ought to be the Cartesian Product, but it's not (no replacement).

"xcombinations takes n distinct elements from the sequence, order
matters."
If order matters, it's a PERMUTATION, period.

"xuniqueCombinations takes n distinct elements from the sequence,
order is irrelevant."
No such thing, a Combination is unique by definition.

"xselections takes n elements (not necessarily distinct) from the
sequence, order matters."
Ah, this allows a size operator, so if size = length, we get full
Cartesian Product.

The proper terminology for the Cartesian Product and
its subsets is:

Permutations with replacement
Combinations with replacement
Permutations without replacement
Combinations without replacement

And if the functions were properly labeled, you would get:

permutation without replacement - size 4

Permutations of 'love'
love loev lvoe lveo leov levo olve olev ovle ovel oelv oevl vloe vleo
vole voel velo veol elov elvo eolv eovl evlo evol
permutation without replacement - size 2

Combinations of 2 letters from 'love'
lo lv le ol ov oe vl vo ve el eo ev
combination without replacement - size 2

Unique Combinations of 2 letters from 'love'
lo lv le ov oe ve
permutation with replacement - size 2

Selections of 2 letters from 'love'
ll lo lv le ol oo ov oe vl vo vv ve el eo ev ee
full Cartesian Product, permutations with replacement - size 4

Selections of 4 letters from 'love'
llll lllo lllv llle llol lloo llov lloe llvl llvo llvv llve llel lleo
llev llee loll lolo lolv lole lool looo loov looe lovl lovo lovv love
loel loeo loev loee lvll lvlo lvlv lvle lvol lvoo lvov lvoe lvvl lvvo
lvvv lvve lvel lveo lvev lvee lell lelo lelv lele leol leoo leov leoe
levl levo levv leve leel leeo leev leee olll ollo ollv olle olol oloo
olov oloe olvl olvo olvv olve olel oleo olev olee ooll oolo oolv oole
oool oooo ooov oooe oovl oovo oovv oove ooel ooeo ooev ooee ovll ovlo
ovlv ovle ovol ovoo ovov ovoe ovvl ovvo ovvv ovve ovel oveo ovev ovee
oell oelo oelv oele oeol oeoo oeov oeoe oevl oevo oevv oeve oeel oeeo
oeev oeee vlll vllo vllv vlle vlol vloo vlov vloe vlvl vlvo vlvv vlve
vlel vleo vlev vlee voll volo volv vole vool vooo voov vooe vovl vovo
vovv vove voel voeo voev voee vvll vvlo vvlv vvle vvol vvoo vvov vvoe
vvvl vvvo vvvv vvve vvel vveo vvev vvee vell velo velv vele veol veoo
veov veoe vevl vevo vevv veve veel veeo veev veee elll ello ellv elle
elol eloo elov eloe elvl elvo elvv elve elel eleo elev elee eoll eolo
eolv eole eool eooo eoov eooe eovl eovo eovv eove eoel eoeo eoev eoee
evll evlo evlv evle evol evoo evov evoe evvl evvo evvv evve evel eveo
evev evee eell eelo eelv eele eeol eeoo eeov eeoe eevl eevo eevv eeve
eeel eeeo eeev eeee
And Combinations with replacement seems to be missing.

>
--
HTH,
Rob- Hide quoted text -

- Show quoted text -

Jun 14 '07 #5

P: n/a
On Jun 14, 4:39 pm, py_genetic <conor.robin...@gmail.comwrote:
You didn't try hard enough. :)
http://aspn.activestate.com/ASPN/Coo.../Recipe/190465
--
HTH,
Rob

Thanks Rob, "permutation" was the keyword I shcould have used!
See my other post to see if that is indeed what you mean.

Jun 14 '07 #6

P: n/a
On Jun 14, 3:02 pm, "mensana...@aol.com" <mensana...@aol.comwrote:
On Jun 14, 4:39 pm, py_genetic <conor.robin...@gmail.comwrote:
You didn't try hard enough. :)
>http://aspn.activestate.com/ASPN/Coo.../Recipe/190465
--
HTH,
Rob
Thanks Rob, "permutation" was the keyword I shcould have used!

See my other post to see if that is indeed what you mean.
Thanks, mensanator I see what you are saying, I appreciate you
clarification. I modified the unique version to fit my needs,
sometimes you just want the first x unique combinations and of the
right "width" (A or AA or AAA...) string, so I reworked it a bit to be
more efficient. Isn't this a case of base^n-1 for # unique
combinations, using the alphabet: 26^strlen - 1 or to figure out
strlen from #of combinations needed: ln(26 * #ofcobinations needed)/
ln(26) obviously a float but a pritty good idea of strlen needed when
rounded?

Jun 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.