Fork me on GitHub

look and say sequence

The look and say sequence is a sequence of integerse that is defined from the previus sequence by noting how many times an integer is occuring in the prevoius sequence.

For example, [1] → [11] → [21] → [1211] → [111221] → [312211] ...

In the above example 1 is occuring one time giving [11], the next one is occuring two times giving [21] and so on.

Originaly this sequence was discovered by John Conway, also famous for the game of life.

This is an implementation of the above look and see sequence in python:

#!/usr/bin/env python

def las(start,seqs):
    prev=[start]
    print prev

    for i in range(seqs):

        cnt=0
        current=[0]
        s=prev[0]

        for j in range(len(prev)):

            if s==prev[j]:
                current[cnt]+=1
                if(current[cnt]==1):
                    current.append(s)
            else:
               s=prev[j]
               cnt=len(current)
               current.append(1)
               current.append(s)

        print current
        prev=current

Comments !