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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Non-continuous subsequences step by step in the Seed7 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 Seed7 programming language

Source code in the seed7 programming language

$ include "seed7_05.s7i";

const func array bitset: ncsub (in bitset: seq, in integer: s) is func
  result
    var array bitset: subseq is 0 times {};
  local
    var bitset: x is {};
    var bitset: xs is {};
    var bitset: ys is {};
  begin
    if seq <> {} then
      x := {min(seq)};
      xs := seq - x;
      for ys range ncsub(xs, s + 1 - s rem 2) do
        subseq &:= x | ys;
      end for;
      subseq &:= ncsub(xs, s + s rem 2);
    elsif s >= 3 then
      subseq &:= {};
    end if;
  end func;

const proc: main is func
  local
    var bitset: seq is {};
  begin
    for seq range ncsub({1, 2, 3, 4}, 0) do
      writeln(seq);
    end for;
  end func;

  

You may also check:How to resolve the algorithm Simulate input/Keyboard step by step in the VBScript programming language
You may also check:How to resolve the algorithm Doubly-linked list/Traversal step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Algebraic data types step by step in the REXX programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Xojo programming language
You may also check:How to resolve the algorithm Window creation step by step in the M2000 Interpreter programming language