How to resolve the algorithm Determine if a string is squeezable step by step in the BCPL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Determine if a string is squeezable step by step in the BCPL programming language

Table of Contents

Problem Statement

Determine if a character string is   squeezable. And if so,   squeeze the string   (by removing any number of a   specified   immediately repeated   character).

This task is very similar to the task     Determine if a character string is collapsible     except that only a specified character is   squeezed   instead of any character that is immediately repeated.

If a character string has a specified   immediately repeated   character(s),   the repeated characters are to be deleted (removed),   but not the primary (1st) character(s).

A specified   immediately repeated   character is any specified character that is   immediately   followed by an identical character (or characters).   Another word choice could've been   duplicated character,   but that might have ruled out   (to some readers)   triplicated characters   ···   or more.

{This Rosetta Code task was inspired by a newly introduced   (as of around November 2019)   PL/I   BIF:   squeeze.}

In the following character string with a specified   immediately repeated   character of   e:

Only the 2nd   e   is an specified repeated character,   indicated by an underscore (above),   even though they (the characters) appear elsewhere in the character string.

So, after squeezing the string, the result would be:

Another example: In the following character string,   using a specified immediately repeated character   s:

The "squeezed" string would be:

Write a subroutine/function/procedure/routine···   to locate a   specified immediately repeated   character and   squeeze   (delete)   them from the character string.   The character string can be processed from either direction.

Show all output here, on this page:

Use (at least) the following five strings,   all strings are length seventy-two (characters, including blanks),   except the 1st string:

Note:   there should be seven results shown,   one each for the 1st four strings,   and three results for the 5th string.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Determine if a string is squeezable step by step in the BCPL programming language

Source code in the bcpl programming language

get "libhdr"

// Squeeze a string
let squeeze(in, ch, out) = valof
$(  out%0 := 0
    for i=1 to in%0
        if i=1 | in%i~=ch | in%(i-1)~=ch
        $(  out%0 := out%0 + 1
            out%(out%0) := in%i
        $)
    resultis out
$)

// Print string with brackets and length
let brackets(s) be
    writef("%N: <<<%S>>>*N", s%0, s)

// Print original and collapsed version
let show(s, ch) be
$(  let v = vec 1+255/BYTESPERWORD
    writef("Character: '%C'*N", ch)
    brackets(s)
    brackets(squeeze(s, ch, v))
    wrch('*N')
$)

let start() be
$(  let s1=""
    let s2="*"If I were two-faced, would I be wearing this one?*" --- Abraham Lincoln "
    let s3="..1111111111111111111111111111111111111111111111111111111111111117777888"
    let s4="I never give 'em hell, I just tell the truth, and they think it's hell. "
    let s5="                                                     --- Harry S Truman "

    show(s1, ' ')
    show(s2, '-')
    show(s3, '7')
    show(s4, '.')
    show(s5, ' ')
    show(s5, '-')
    show(s5, 'r')
$)

  

You may also check:How to resolve the algorithm Percolation/Site percolation step by step in the Fortran programming language
You may also check:How to resolve the algorithm GUI/Maximum window dimensions step by step in the Sidef programming language
You may also check:How to resolve the algorithm Floyd-Warshall algorithm step by step in the Java programming language
You may also check:How to resolve the algorithm Boolean values step by step in the ACL2 programming language
You may also check:How to resolve the algorithm Fork step by step in the NewLISP programming language