How to resolve the algorithm Balanced brackets step by step in the Seed7 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Balanced brackets step by step in the Seed7 programming language

Table of Contents

Problem Statement

Task:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Balanced brackets step by step in the Seed7 programming language

Source code in the seed7 programming language

$ include "seed7_05.s7i";

const func string: generateBrackets (in integer: count) is func
  result
    var string: stri is "";
  local
    var integer: index is 0;
    var integer: pos is 0;
    var char: ch is ' ';
  begin
    stri := "[" mult count & "]" mult count;
    for index range 1 to length(stri) do
      pos := rand(1, length(stri));
      ch := stri[index];
      stri @:= [index] stri[pos];
      stri @:= [pos] ch;
    end for;
  end func;

const func boolean: checkBrackets (in string: test) is func
  result
    var boolean: okay is TRUE;
  local
    var char: ch is ' ';
    var integer: open is 0;
  begin
    for ch range test do
      if ch = '[' then
        incr(open);
      elsif ch = ']' then
        if open = 0 then
          okay := FALSE;
        else
          decr(open);
        end if;
      end if;
    end for;
    okay := open = 0;
  end func;

const proc: main is func
  local
    var integer: n is 0;
    var integer: count is 0;
    var string: stri is "";
  begin
    for n range 0 to 4 do
      for count range 1 to 3 do
        stri := generateBrackets(n);
        writeln(stri <& ": " <& checkBrackets(stri));
      end for;
    end for;
  end func;

  

You may also check:How to resolve the algorithm Ramer-Douglas-Peucker line simplification step by step in the Julia programming language
You may also check:How to resolve the algorithm Amicable pairs step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the Wren programming language
You may also check:How to resolve the algorithm Copy a string step by step in the Quackery programming language
You may also check:How to resolve the algorithm Create a two-dimensional array at runtime step by step in the Logo programming language