How to resolve the algorithm Look-and-say sequence step by step in the Smalltalk programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Look-and-say sequence step by step in the Smalltalk programming language

Table of Contents

Problem Statement

The   Look and say sequence   is a recursively defined sequence of numbers studied most notably by   John Conway.

The   look-and-say sequence   is also known as the   Morris Number Sequence,   after cryptographer Robert Morris,   and the puzzle   What is the next number in the sequence 1,   11,   21,   1211,   111221?   is sometimes referred to as the Cuckoo's Egg,   from a description of Morris in Clifford Stoll's book   The Cuckoo's Egg.

Sequence Definition

An example:

Write a program to generate successive members of the look-and-say sequence.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Look-and-say sequence step by step in the Smalltalk programming language

Source code in the smalltalk programming language

String extend [
  lookAndSay [ |anElement nextElement counter coll newColl|
     coll := (self asOrderedCollection).
     newColl := OrderedCollection new.
     counter := 0.
     anElement := (coll first).
     [ coll size > 0 ]
     whileTrue: [
        nextElement := coll removeFirst.
	( anElement == nextElement ) ifTrue: [
           counter := counter + 1.
        ] ifFalse: [
	  newColl add: (counter displayString).
	  newColl add: (anElement asString).
	  anElement := nextElement.
	  counter := 1.
        ]
     ].
     newColl add: (counter displayString).
     newColl add: (anElement asString).
     ^(newColl join)
  ]
].

|r|
r := '1'.
10 timesRepeat: [
  r displayNl.
  r := r lookAndSay.
]

String compile:
  'lookAndSay |anElement nextElement counter coll newColl|
     coll := (self asOrderedCollection).
     newColl := OrderedCollection new.
     counter := 0.
     anElement := (coll first).
     [ coll size > 0 ]
     whileTrue: [
        nextElement := coll removeFirst.
	( anElement == nextElement ) ifTrue: [
           counter := counter + 1.
        ] ifFalse: [
	  newColl add: (counter displayString).
	  newColl add: (anElement asString).
	  anElement := nextElement.
	  counter := 1.
        ]
     ].
     newColl add: (counter displayString).
     newColl add: (anElement asString).
     ^('''' join: newColl)'
  classified: 'toys'.
 
result := OrderedCollection new.
r := '1'.
result add: r.
result addAll: ((1 to: 10) collect: [ :i |
  r := r lookAndSay.
]).
result.

  

You may also check:How to resolve the algorithm Kronecker product step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Legendre prime counting function step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Comments step by step in the Elena programming language
You may also check:How to resolve the algorithm Circles of given radius through two points step by step in the Rust programming language
You may also check:How to resolve the algorithm Scope modifiers step by step in the Wren programming language