How to resolve the algorithm Rep-string step by step in the BCPL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Rep-string step by step in the BCPL programming language

Table of Contents

Problem Statement

Given a series of ones and zeroes in a string, define a repeated string or rep-string as a string which is created by repeating a substring of the first N characters of the string truncated on the right to the length of the input string, and in which the substring appears repeated at least twice in the original. For example, the string 10011001100 is a rep-string as the leftmost four characters of 1001 are repeated three times and truncated on the right to give the original string. Note that the requirement for having the repeat occur two or more times means that the repeating unit is never longer than half the length of the input string.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Rep-string step by step in the BCPL programming language

Source code in the bcpl programming language

get "libhdr"

// Returns the length of the longest rep-string
// (0 if there are none)
let repstring(s) = valof
$(  for i = s%0/2 to 1 by -1 do
    $(  for j = 1 to i
        $(  let k = i
            while j+k <= s%0 do
            $(  unless s%(j+k)=s%j goto next
                k := k + i
            $)
        $) 
        resultis i
        next: loop
    $)
    resultis 0
$)

// Print first N characters of string
let writefirst(s, n) be
$(  let x = s%0
    s%0 := n
    writes(s)
    s%0 := x
$)

// Test string
let rep(s) be
$(  let n = repstring(s)
    writef("%S: ",s)
    test n=0
        do writes("none")
        or writefirst(s,n)
    wrch('*N')
$)

let start() be
$(  rep("1001110011")
    rep("1110111011")
    rep("0010010010")
    rep("1010101010")
    rep("1111111111")
    rep("0100101101")
    rep("0100100")
    rep("101")
    rep("11")
    rep("00")
    rep("1")
$)

  

You may also check:How to resolve the algorithm Fusc sequence step by step in the Dyalect programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Draw a cuboid step by step in the Evaldraw programming language
You may also check:How to resolve the algorithm Memory layout of a data structure step by step in the Ada programming language
You may also check:How to resolve the algorithm Random numbers step by step in the Frink programming language