Skip to main content

Look and Say

Objectives

  • Practice, practice, practice!

In this exercise, you will implement the look-and-say sequence. The sequence starts as follows:

1, 11, 21, 1211, 111221, 312211, 13112221, …

The sequence is constructed starting with 1 and reading out quantities of digits left-to-right to determine the subsequent numbers.

   1 is read as "one 1"                    which yields     11
  11 is read as "two 1s"                   which yields     21
  21 is read as "one 2 and one 1"          which yields   1211
1211 is read as "one 1, one 2, and two 1s" which yields 111221

Problem 1 Write the next function. next takes one argument (term, an integer) and returns the number generated by applying the rules of the look-and-say sequence.

Here is some example output:

>>> next(21)
1211

>>> next(2222)
42

>>> next(312211)
13112221
Note

Your next function should return an integer!

Problem 2 Write the readit function. readit takes one argument (seq_length, an integer) and prints out the first seq_length items in the Look-and-Say sequence.