Program to find nth term in Look and Say Sequence in Python

The Look and Say Sequence is a famous sequence where each term describes the previous term by counting consecutive identical digits. Starting with "1", each subsequent term is generated by reading the previous term aloud.

Understanding the Sequence

The first few terms of the Look and Say sequence are:

  • Term 1: 1
  • Term 2: 11 (one 1)
  • Term 3: 21 (two 1s)
  • Term 4: 1211 (one 2, one 1)
  • Term 5: 111221 (one 1, one 2, two 1s)

Each term is formed by counting consecutive identical digits in the previous term and stating the count followed by the digit.

Algorithm

To generate the nth term, we follow these steps:

  • Start with the first term "1"
  • For each iteration from 2 to n, process the current string
  • Count consecutive identical characters
  • Build the next term by appending count + character

Implementation

class Solution:
    def solve(self, n):
        s = "1"
        
        if n == 1:
            return s
            
        for i in range(2, n + 1):
            j = 0
            temp = ""
            curr = ""
            count = 0
            
            while j < len(s):
                if curr == "":
                    curr = s[j]
                    count = 1
                    j += 1
                elif curr == s[j]:
                    count += 1
                    j += 1
                else:
                    temp += str(count) + curr
                    curr = ""
                    count = 0
            
            # Add the last group
            temp += str(count) + curr
            s = temp
            
        return s

# Test the solution
ob = Solution()
n = 5
result = ob.solve(n)
print(f"The {n}th term in Look and Say sequence is: {result}")
The 5th term in Look and Say sequence is: 111221

Step-by-Step Example

Let's trace through generating the 5th term:

def trace_look_and_say(n):
    s = "1"
    print(f"Term 1: {s}")
    
    for i in range(2, n + 1):
        next_term = ""
        j = 0
        
        while j < len(s):
            current_char = s[j]
            count = 1
            
            # Count consecutive identical characters
            while j + 1 < len(s) and s[j + 1] == current_char:
                count += 1
                j += 1
            
            next_term += str(count) + current_char
            j += 1
        
        s = next_term
        print(f"Term {i}: {s}")
    
    return s

# Trace the first 5 terms
trace_look_and_say(5)
Term 1: 1
Term 2: 11
Term 3: 21
Term 4: 1211
Term 5: 111221

Optimized Version

Here's a cleaner implementation using Python's itertools.groupby:

from itertools import groupby

def look_and_say_optimized(n):
    s = "1"
    
    for _ in range(n - 1):
        s = ''.join(str(len(list(group))) + key for key, group in groupby(s))
    
    return s

# Test with different values
for i in range(1, 6):
    result = look_and_say_optimized(i)
    print(f"Term {i}: {result}")
Term 1: 1
Term 2: 11
Term 3: 21
Term 4: 1211
Term 5: 111221

Conclusion

The Look and Say sequence generates each term by counting consecutive identical digits in the previous term. The algorithm iterates through each character, counts occurrences, and builds the next term by concatenating count and digit pairs.

Updated on: 2026-03-25T12:37:46+05:30

697 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements