livepython.net

307. Python Program to Print the Look-and-Say Sequence

HardPhase 3 · Challenge

A few steps, and every tool is one you know: for loops over a string, if, a counter and str(). Read the first 5 terms out loud first.

Overview

The look-and-say sequence is a famous puzzle: each term describes the term before it. To make it, you count groups of the same character, a skill you also need to compress text.

Problem Statement

Rania's teacher wrote 1, 11, 21, 1211 on the board and asked what comes next. The rule is "say what you see":

  • Start with the term 1.
  • Read the term from left to right in groups of the same digit.
  • For each group, write how many digits it has, then the digit.
  • The result is the next term.

So 1 is "one 1", which gives 11. 11 is "two 1s", which gives 21. 21 is "one 2, one 1", which gives 1211.

Read a whole number n and print the first n terms, one on each line.

Examples

Example 1. Input 5 gives:

1
11
21
1211
111221

Why: 1211 has the groups "one 1", "one 2" and "two 1s". So the next term is 11, 12 and 21 joined: 111221.

Example 2. Input 1 gives:

1

Why: the first term is always 1, and only 1 term is asked for.

Input

One whole number n (1 to 15). Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

n lines, one term on each. The terms get long quickly: the 15th term has 78 digits. The tests try 7 different inputs.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 10 · For Loops
solution.py
# write your code here

EXPECTED OUTPUT

11
211
321
41211
5111221

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.