Skip to content

Instantly share code, notes, and snippets.

@akaz00
Created October 28, 2014 16:19
Show Gist options
  • Select an option

  • Save akaz00/e4d3a3372faa3e7a7dfb to your computer and use it in GitHub Desktop.

Select an option

Save akaz00/e4d3a3372faa3e7a7dfb to your computer and use it in GitHub Desktop.
lookandsay seq
def mysplit(target):
tuples = []
prev = target[0]
count = 1
for idx, c in enumerate(target[:-1]):
if prev != target[idx+1]:
tuples.append((prev, count))
count = 1
else:
count += 1
if idx == len(target) - 2:
tuples.append((target[-1], count))
prev = target[idx+1]
return tuples
def lookandsay(target):
if len(target) == 1:
return "11"
ret = ""
for t in mysplit(target):
ret += (str(t[1]) + "" + t[0])
return ret
def lseq(times):
st = "1"
for i in range(times):
ret = lookandsay(st)
print ret
st = ret
lseq(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment