How to resolve the algorithm Non-continuous subsequences step by step in the F# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Non-continuous subsequences step by step in the F# programming language

Table of Contents

Problem Statement

Consider some sequence of elements. (It differs from a mere set of elements by having an ordering among members.) A subsequence contains some subset of the elements of this sequence, in the same order. A continuous subsequence is one in which no elements are missing between the first and last elements of the subsequence. Note: Subsequences are defined structurally, not by their contents. So a sequence a,b,c,d will always have the same subsequences and continuous subsequences, no matter which values are substituted; it may even be the same value.

Task: Find all non-continuous subsequences for a given sequence.

For the sequence   1,2,3,4,   there are five non-continuous subsequences, namely:

There are different ways to calculate those subsequences. Demonstrate algorithm(s) that are natural for the language.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Non-continuous subsequences step by step in the F# programming language

Source code in the fsharp programming language

(*
  A function to generate only the non-continuous subsequences.
  Nigel Galloway July 20th., 2017
*)
let N n =
  let     fn n = Seq.map (fun g->(2<<<n)+g)
  let rec fg n = seq{if n>0 then yield! seq{1..((1<<<n)-1)}|>fn n; yield! fg (n-1)|>fn n}
  Seq.collect fg ({1..(n-2)})


let Ng ng = N ng |> Seq.iter(fun n->printf "%2d -> " n; {0..(ng-1)}|>Seq.iter (fun g->if (n&&&(1<<<g))>0 then printf "%d " (g+1));printfn "")
Ng 4


(*
  A function to filter out continuous subsequences.
  Nigel Galloway July 24th., 2017
*)
let Nonseq n=
  let fn = function
    |((n,0),true )->(n+1,1)
    |((n,_),false)->(n,0)
    |(n,_)        ->n
  {5..(1<<<n)-1}|>Seq.choose(fun i->if fst({0..n-1}|>Seq.takeWhile(fun n->(1<<<(n-1))<i)|>Seq.fold(fun n g->fn (n,(i&&&(1<<<g)>0)))(0,0)) > 1 then Some(i) else None)


  

You may also check:How to resolve the algorithm Sum and product of an array step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm 21 game step by step in the BASIC programming language
You may also check:How to resolve the algorithm Bitcoin/address validation step by step in the Erlang programming language
You may also check:How to resolve the algorithm Loops/For step by step in the LIL programming language
You may also check:How to resolve the algorithm Execute Brain step by step in the Go programming language