Here was a fun little exerscise on Reddit’s daily programmer.
#Description The Look and Say sequence is an interesting sequence of numbers where each term is given by describing the makeup of the previous term. The 1st term is given as 1. The 2nd term is 11 (‘one one’) because the first term (1) consisted of a single 1. The 3rd term is then 21 (‘two one’) because the second term consisted of two 1s. The first 6 terms are: 1 11 21 1211 111221 312211 #Formal Inputs & Outputs ##Input On console input you should enter a number N ##Output The Nth Look and Say number. #Bonus Allow any ‘seed’ number, not just 1. Can you find any interesting cases?
Instead of getting the input from the console, I will just be including it in the script. And instead of returning the Nth Look and Say number, I return the entire sequence up to N.
I used the groupby function in Python’s itertools module to return consecutive keys and groups from the term. For example, with the term ‘233311’: