how to make a generator use the last yielded value when it regains control
John Salerno
johnjsal at NOSPAMgmail.com
Thu Apr 6 23:33:18 EDT 2006
John Salerno wrote:
> 2. just make it a function that takes a second argument, that being the
> number of times you want it to repeat itself and create numbers in the
> sequence
Here's what I've come up with so far. Probably not the most elegant
solution because of the nested function, but it does work! :)
def morris(seed, limit):
num = seed
numberSet = []
def nextNum(num):
num = list(str(num))
grouping = []
nextNum = []
for i in range(len(num)):
try:
if num[i] == num[i + 1]:
grouping.append(num[i])
else:
grouping.append(num[i])
nextNum.append(str(len(grouping)) + num[i])
grouping = []
except IndexError:
grouping.append(num[i])
nextNum.append(str(len(grouping)) + num[i])
return ''.join(nextNum)
for x in range(limit):
numberSet.append(int(num))
num = nextNum(num)
return numberSet
More information about the Python-list
mailing list