How to resolve the algorithm Determine if a string is collapsible step by step in the ALGOL 68 programming language
How to resolve the algorithm Determine if a string is collapsible step by step in the ALGOL 68 programming language
Table of Contents
Problem Statement
Determine if a character string is collapsible. And if so, collapse the string (by removing immediately repeated characters).
If a character string has immediately repeated character(s), the repeated characters are to be deleted (removed), but not the primary (1st) character(s).
An immediately repeated character is any 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: collapse.}
In the following character string:
Only the 2nd t, e, and l are repeated characters, indicated by underscores (above), even though they (those characters) appear elsewhere in the character string.
So, after collapsing the string, the result would be:
Another example: In the following character string:
The "collapsed" string would be:
Write a subroutine/function/procedure/routine··· to locate repeated characters and collapse (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:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Determine if a string is collapsible step by step in the ALGOL 68 programming language
Source code in the algol programming language
BEGIN
# returns a collapsed version of s #
# i.e. s with adjacent duplicate characters removed #
PROC collapse = ( STRING s )STRING:
IF s = ""
THEN "" # empty string #
ELSE # non-empty string #
[ LWB s : UPB s ]CHAR result;
INT r pos := LWB result;
result[ r pos ] := s[ LWB s ];
FOR s pos FROM LWB s + 1 TO UPB s DO
IF result[ r pos ] /= s[ s pos ] THEN
r pos +:= 1;
result[ r pos ] := s[ s pos ]
FI
OD;
result[ LWB result : r pos ]
FI # callapse # ;
# task test cases #
[]STRING tests = ( ""
, """If I were two-faced, would I be wearing this one?"" --- Abraham Lincoln "
, "..1111111111111111111111111111111111111111111111111111111111111117777888"
, "I never give 'em hell, I just tell the truth, and they think it's hell. "
, " --- Harry S Truman "
);
FOR t pos FROM LWB tests TO UPB tests DO
STRING s = tests[ t pos ];
STRING c = collapse( s );
print( ( " <<<", s, ">>> (length ", whole( ( UPB s + 1 ) - LWB s, 0 ), ")", newline ) );
print( ( "result <<<", c, ">>> (length ", whole( ( UPB c + 1 ) - LWB c, 0 ), ")", newline ) )
OD
END
You may also check:How to resolve the algorithm Ray-casting algorithm step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Prime decomposition step by step in the Fortran programming language
You may also check:How to resolve the algorithm SHA-1 step by step in the Scala programming language
You may also check:How to resolve the algorithm Idoneal numbers step by step in the Pascal programming language
You may also check:How to resolve the algorithm Sorting algorithms/Radix sort step by step in the Haskell programming language