def lookNsay():
'''Generate the "look and say" sequence:
1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211, ...
Note that strings are returned
'''
s = '1' # s is the previous result, start with 1
while True:
yield s
c,n, result = s[0], 1, ''
for t in s[1:]:
if t==c:
n += 1
else:
result += str(n) + c
c,n = t, 1
result += str(n) + c
s = result
def saythis(prev): # Supporting function
'''Generate characters of a "look-and-say" number given a generator of
characters for the previous number.
'''
c,n = next(prev), 1
for t in prev:
if t==c:
n += 1
else:
yield str(n)
yield c
c,n = t, 1
yield str(n)
yield c
def lookNsayNth(n = 1):
'''Generate characters of the n-th value of the "look-and-say" sequence
recursively
'''
if n == 1:
yield '1'
else:
yield from saythis(lookNsayNth(n-1))