Skip to content

Instantly share code, notes, and snippets.

@hpcx82
Created March 5, 2014 05:17
Show Gist options
  • Save hpcx82/9361691 to your computer and use it in GitHub Desktop.
Save hpcx82/9361691 to your computer and use it in GitHub Desktop.
def look_and_say(member):
while True:
num = []
count = []
pre = None
for ele in member:
if len(num) > 0 and pre == ele:
count[len(count)-1] += 1
else:
num += [ele]
count += [1]
pre = ele
member = ''.join([''.join([ str(i) for i in pair]) for pair in zip(count, num)])
yield member
@hpcx82
Copy link
Author

hpcx82 commented Mar 5, 2014

这个作为面试题到也不错~
可以考考思维,代码的组织,以及python的list,yield的运用:

for i in look_and_say('11'):
print i

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment