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

mensanator at aol.com mensanator at aol.com
Thu Jun 14 17:54:01 EDT 2007


On Jun 14, 3:08 pm, Rob Wolfe <r... at smsnet.pl> wrote:
> py_genetic <conor.robin... at gmail.com> writes:
> > 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/Cookbook/Python/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 -





More information about the Python-list mailing list