Generate the Look-and-say Sequence featured in QuizKnock in Python

QuizKnock's video posted the other day [University of Tokyo] Challenge the Google entrance exam at the Google headquarters! featured ** Look-and-say Sequence **. This is a sequence that changes according to the rule that the first term is 1 and the numbers read out from the previous term are arranged in the next term, as shown below.

1   =1 is 1(One 1)  → 1 1
1 1 =2 1s(Two 1s) → 2 1
2 1 =2 is 1,1 is 1(One 2, One 1) → 1 2 1 1
1 2 1 1 =1 is 1,2 is 1,2 1s...

I first learned about the existence of this sequence in this video and found it interesting. I wrote a program in Python that generates this sequence.

def lookAndSay(initialValues, maxIteration=None):
    x = initialValues
    yield x

    iteration = 1

    while True:
        if maxIteration is not None and iteration >= maxIteration:
            break

        new_x = []
        prev = x[0]
        count = 1

        for n in x[1:]:
            if n == prev:
                count += 1
            else:
                new_x.append(count)
                new_x.append(prev)
                prev = n
                count = 1
        new_x.append(count)
        new_x.append(prev)

        x = new_x
        yield x
        
        iteration += 1

If you do as below, Look-and-say Sequence with the first term as 1 will be generated up to the 10th term and output.

>>> for li in lookAndSay([1], 10):
...     print(li)
...
[1]
[1, 1]
[2, 1]
[1, 2, 1, 1]
[1, 1, 1, 2, 2, 1]
[3, 1, 2, 2, 1, 1]
[1, 3, 1, 1, 2, 2, 2, 1]
[1, 1, 1, 3, 2, 1, 3, 2, 1, 1]
[3, 1, 1, 3, 1, 2, 1, 1, 1, 3, 1, 2, 2, 1]
[1, 3, 2, 1, 1, 3, 1, 1, 1, 2, 3, 1, 1, 3, 1, 1, 2, 2, 1, 1]

Let us know in the comments if there is a better way!

Recommended Posts

Generate the Look-and-say Sequence featured in QuizKnock in Python
Generate rounded thumbnails in Python
Download the file in Python
Find the difference in Python
Generate U distribution in Python
Generate QR code in Python
Generate 8 * 8 (64) cubes in Blender Python
Getting the arXiv API in Python
[Python] Generate QR code in memory
Python in the browser: Brython's recommendation
Save the binary file in Python
Hit the Sesami API in Python
Get the desktop path in Python
Generate Jupyter notebook ".ipynb" in Python
Get the script path in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
Hit the web API in Python
I wrote the queue in Python
Calculate the previous month in Python
Examine the object's class in python
Get the desktop path in Python
Get the host name in Python
Access the Twitter API in Python
The first step in Python Matplotlib
I wrote the stack in Python
Master the weakref module in Python
Generate a first class collection in Python
Learn the design pattern "Prototype" in Python
Learn the design pattern "Builder" in Python
Load the remote Python SDK in IntelliJ
Try using the Wunderlist API in Python
Check the behavior of destructor in Python
Generate AWS-S3 signed (time-limited) URLs in Python
Learn the design pattern "Flyweight" in Python
Try using the Kraken API in Python
Learn the design pattern "Observer" in Python
Learn the design pattern "Memento" in Python
Learn the design pattern "Proxy" in Python
Write the test in a python docstring
Learn the design pattern "Command" in Python
OR the List in Python (zip function)
Display Python 3 in the browser with MAMP
Tweet using the Twitter API in Python
Learn the design pattern "Visitor" in Python
Learn the design pattern "Bridge" in Python
Check if the URL exists in Python
Learn the design pattern "Mediator" in Python
Automatically generate Python Docstring Comment in Emacs
Associate the table set in python models.py
Learn the design pattern "Decorator" in Python
Run the Python interpreter in a script
The result of installing python in Anaconda
What is "mahjong" in the Python library? ??
Read the file line by line in Python
Read the file line by line in Python
Module to generate word N-gram in Python
MongoDB for the first time in Python
Learn the design pattern "Iterator" in Python
The basics of running NoxPlayer in Python
Learn the design pattern "Strategy" in Python