interesting exercise

Charles Sanders C.delete_this.Sanders at BoM.GOv.AU
Wed May 9 20:49:44 EDT 2007


castironpi 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



More information about the Python-list mailing list