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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Look-and-say sequence step by step in the OCaml 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 OCaml programming language

Source code in the ocaml programming language

let rec seeAndSay = function
  | [], nys -> List.rev nys
  | x::xs, [] -> seeAndSay(xs, [x; 1])
  | x::xs, y::n::nys when x=y -> seeAndSay(xs, y::1+n::nys)
  | x::xs, nys -> seeAndSay(xs, x::1::nys)


> let gen n =
    let xs = Array.create n [1] in
    for i=1 to n-1 do
      xs.(i) <- seeAndSay(xs.(i-1), [])
    done;
    xs;;
val gen : int -> int list array = <fun>

> gen 10;;
- : int list array =
  [|[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]|]


#load "str.cma";;

let lookandsay =
  Str.global_substitute (Str.regexp "\\(.\\)\\1*")
                        (fun s -> string_of_int (String.length (Str.matched_string s)) ^
                                  Str.matched_group 1 s)

let () =
  let num = ref "1" in
  print_endline !num;
  for i = 1 to 10 do
    num := lookandsay !num;
    print_endline !num;
  done


open Pcre

let lookandsay str =
  let rex = regexp "(.)\\1*" in
  let subs = exec_all ~rex str in
  let ar = Array.map (fun sub -> get_substring sub 0) subs in
  let ar = Array.map (fun s -> String.length s, s.[0]) ar in
  let ar = Array.map (fun (n,c) -> (string_of_int n) ^ (String.make 1 c)) ar in
  let res = String.concat "" (Array.to_list ar) in
  (res)

let () =
  let num = ref(string_of_int 1) in
  for i = 1 to 10 do
    num := lookandsay !num;
    print_endline !num;
  done


(* see http://oeis.org/A005150 *)

let look_and_say s =
let n = String.length s
and buf = Buffer.create 0
and prev = ref s.[0]
and count = ref 0 in
let append () = Buffer.add_char buf (char_of_int (48 + !count));
                Buffer.add_char buf !prev in
String.iter (fun c ->
   if c = !prev then incr count else
   begin
      append ();
      prev := c;
      count := 1
   end
) s;
append ();
Buffer.contents buf;;

(* what about length of successive strings ? *)
let iter f a n =
let rec aux r n v = if n = 0
                    then List.rev(r::v)
                    else aux (f r) (n - 1) (r::v) in
aux a n [];;

let las = iter look_and_say "1";;

(* the first sixty terms *)

List.map (String.length) (las 59);;
(*
   [1; 2; 2; 4; 6; 6; 8; 10; 14; 20; 26; 34; 46; 62; 78; 102; 134; 176; 226;
    302; 408; 528; 678; 904; 1182; 1540; 2012; 2606; 3410; 4462; 5808; 7586;
    9898; 12884; 16774; 21890; 28528; 37158; 48410; 63138; 82350; 107312;
    139984; 182376; 237746; 310036; 403966; 526646; 686646; 894810; 1166642;
    1520986; 1982710; 2584304; 3369156; 4391702; 5724486; 7462860; 9727930;
    12680852]
*)

(* see http://oeis.org/A005341 *)


  

You may also check:How to resolve the algorithm Median filter step by step in the Racket programming language
You may also check:How to resolve the algorithm Abelian sandpile model/Identity step by step in the Rust programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the Julia programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Find common directory path step by step in the Aime programming language