Incrementing a string -- puzzing "solution"...
Pierre Fortin
pfortin at pfortin.com
Thu Sep 16 00:44:38 EDT 2004
On Wed, 15 Sep 2004 15:08:20 -0700 John wrote:
> This conveniently steps through the alphabet, then goes on to aa, ab,ac,
Puzzling... I hacked at this problem as a flexibility learning exercise
and actually got this to work; but... does 'while True' in a generator
have side-effects...?? Or generator recursion?
Maybe I'm just tired -- I don't see how the string actually _grows_ inside
the while.... :^?
I was just expecting the prefix to be '', then 'a'...'z' giving a..z,
aa..az, ba..bz, ... za..zz -- not continuing through aaa...azz and
onwards... It's cool; but boggles my mind at the moment... not that
that's a stretch... :^)
def ascinc(start='a',end='z'):
g = ascinc(start,end)
prefix = ''
while True:
for i in range(ord(start[-1]),ord(end[-1])+1):
yield (prefix + chr(i))
prefix = g.next()
print "what you wanted..."
g = ascinc('a')
for i in range(100):
print g.next(),
print
print "including some flexibility..."
g = ascinc('0','1')
for i in range(100):
print g.next(),
More information about the Python-list
mailing list