Determine if a string is collapsible

From Rosetta Code
Revision as of 06:01, 18 November 2019 by rosettacode>Gerard Schildberger (Created page with "{{draft task}} Determine if a character string is   ''collapsible''. And if so,   collapse the string   (by removing   ''immediately repeated''   ch...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Determine if a string is collapsible is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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 November 2019)   PL/I   BIF:   collapse).


Examples

In the following character string:


 The better the 4-wheel drive, the further you'll be from help when ya get stuck! 


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:

 The beter the 4-whel drive, the further you'l be from help when ya get stuck! 



Another example: In the following character string:

 headmistressship 


The "collapsed" string would be:

 headmistreship 


Task

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:

  •   the   original string and its length
  •   the resultant string and its length
  •   the above strings should be "bracketed" with   <<<   and   >>>   (to delineate blanks)
  •   «««Guillemets may be used instead for "bracketing" for the more artistic programmers,   shown used here»»»


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

 string
 number
        ╔╗
   1    ║╚═══════════════════════════════════════════════════════════════════════╗   ◄■■■■■■  a null string  (length zero)
   2    ║"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ║
   3    ║..1111111111111111111111111111111111111111111111111111111111111117777888║
   4    ║I never give 'em hell, I just tell the truth, and they think it's hell. ║
   5    ║                                                    --- Harry S Truman  ║   ◄■■■■■■  has many repeated blanks
        ╚════════════════════════════════════════════════════════════════════════╝



Related tasks



REXX

<lang rexx>/*REXX program "collapses" all immediately repeated characters in a string (or strings).*/ @.= /*define a default for the @. array. */ parse arg x /*obtain optional argument from the CL.*/ if x\= then @.1= x /*if user specified an arg, use that. */

         else do;  @.1=
                   @.2= '"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln '
                   @.3=  ..1111111111111111111111111111111111111111111111111111111111111111177788
                   @.4= "I never give 'em hell, I just tell the truth, and they think it's hell. "
                   @.5= '                                                   ---  Harry S Truman  '
              end
    do j=1;    L= length(@.j)                   /*obtain the length of an array element*/
    say copies('═', 105)                        /*show a separator line between outputs*/
    if j>1  &  L==0     then leave              /*if arg is null and  J>1, then leave. */
    say '    length='right(L, 3)     "   input=«««" || @.j || '»»»'
    new= collapse(@.j)
      w= length(new)
    say '    length='right(w, 3)     "  output=«««" || new || '»»»'
    end   /*j*/

exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ collapse: procedure; parse arg y 1 $ 2 /*get a value from arg; get 1st char. */

                    do k=2  to length(y)        /*traipse through almost all the chars.*/
                    _= substr(y, k, 1)                       /*pick a character from  Y*/
                    if _==right($, 1)  then iterate          /*Same character? Skip it.*/
                    $= $ || _                                /*append char., it's diff.*/
                    end     /*j*/
         return $</lang>
output   when using the internal default inputs:
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    length=  0    input=«««»»»
    length=  0   output=«««»»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    length= 72    input=«««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»»
    length= 70   output=«««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    length= 72    input=«««..1111111111111111111111111111111111111111111111111111111111111111177788»»»
    length=  4   output=«««.178»»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    length= 72    input=«««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
    length= 69   output=«««I never give 'em hel, I just tel the truth, and they think it's hel. »»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    length= 72    input=«««                                                   ---  Harry S Truman  »»»
    length= 17   output=««« - Hary S Truman »»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════