interesting exercise

castironpi at gmail.com castironpi at gmail.com
Wed May 9 22:57:02 EDT 2007


On May 9, 7:49 pm, Charles Sanders <C.delete_this.Sand... at BoM.GOv.AU>
wrote:
> castiro... at gmail.com wrote:
> > On May 9, 1:13 am, Charles Sanders <C.delete_this.Sand... at BoM.GOv.AU>
> > 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 "permute2('abc',2) =", permute2('abc',2)
> >> print "len(permute2('13579',3)) =", len(permute2('13579',3))
>
> >> permute2('abc',2) = ['aa', 'ab', 'ac', 'ba', 'bb', 'bc',
> >>   'ca', 'cb', 'cc']
> >> len(permute2('13579',3)) = 125
>
> >> Charles
>
> > Could you explain, this one, actually?  Don't forget StopIteration.
>
>         As Michael said, simple counting in base n with the
> string as the digits. No attempt at clarity or efficiency (I
> did say it was a "monstrosity").
>
>         Also, as a python beginner I didn't know about divmod,
> and have no idea what you (castironpi) mean by "Don't forget
> StopIteration."
>
> Charles

Please disregard.  I have just learned that:
"If the generator exits without yielding another value, a
StopIteration exception is raised."
"exception StopIteration
Raised by an iterator's next() method to signal that there are no
further values."
Means normal generator termination.




More information about the Python-list mailing list