Determine if a string is squeezable

From Rosetta Code
Revision as of 00:19, 22 November 2019 by rosettacode>Gerard Schildberger (created a new (draft) task, added the computer programming language REXX.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Determine if a string is squeezable 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   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.}


Examples

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


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


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:

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



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

 headmistressship 


The "squeezed" string would be:

 headmistreship 


Task

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:

  •   the   specified repeated character   (to be searched for and possibly squeezed):
  •   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:

                                                                                  immediately
 string                                                                            repeated
 number                                                                            character
                                                                                     ( ↓   a blank,  a minus,  a seven,  a period)
        ╔╗
   1    ║╚═══════════════════════════════════════════════════════════════════════╗    ' '    ◄■■■■■■  a null string  (length zero)
   2    ║"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ║    '-'
   3    ║..1111111111111111111111111111111111111111111111111111111111111117777888║    '7'
   4    ║I never give 'em hell, I just tell the truth, and they think it's hell. ║    '.'
   5    ║                                                    --- Harry S Truman  ║  (below)  ◄■■■■■■  has many repeated blanks
        ╚════════════════════════════════════════════════════════════════════════╝     ↑
                                                                                       │
                                                                                       │
        For the 5th string  (Truman's signature line),  use each of these  specified immediately  repeated characters:
                                  •  a blank
                                  •  a minus
                                  •  a lowercase  r


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


Related tasks



REXX

<lang rexx>/*REXX program "squeezes" all immediately repeated characters in a string (or strings). */ @.= /*define a default for the @. array. */

  1. .1= ' '; @.1=
  2. .2= '-'; @.2= '"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln '
  3. .3= '7'; @.3= ..1111111111111111111111111111111111111111111111111111111111111111177788
  4. .4= . ; @.4= "I never give 'em hell, I just tell the truth, and they think it's hell. "
  5. .5= ' '; @.5= ' --- Harry S Truman '
  6. .6= '-'; @.6= @.5
  7. .7= 'r'; @.7= @.5
    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 '    specified immediate repeatable chararacter='    #.j     "   ('"c2x(#.j)"'x)"
    say '    length='right(L, 3)     "   input=«««" || @.j || '»»»'
    new= squeeze(@.j, #.j)
      w= length(new)
    say '    length='right(w, 3)     "  output=«««" || new || '»»»'
    end   /*j*/

exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ squeeze: procedure; parse arg y 1 $ 2,z /*get string; get immed. repeated char.*/

        if pos(z || z, y)==0  then return y     /*No repeated immediate char?  Return Y*/
                                                /* [↑]  Not really needed;  a speed─up.*/
                    do k=2  to length(y)        /*traipse through almost all the chars.*/
                    _= substr(y, k, 1)                      /*pick a character from  Y */
                    if _==right($, 1) & _==z then iterate   /*Same character?  Skip it.*/
                    $= $ || _                               /*append char., it's diff. */
                    end     /*j*/
        return $</lang>
output   when using the internal default inputs:
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    specified immediate repeatable chararacter=      ('20'x)
    length=  0    input=«««»»»
    length=  0   output=«««»»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    specified immediate repeatable chararacter= -    ('2D'x)
    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 »»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    specified immediate repeatable chararacter= 7    ('37'x)
    length= 72    input=«««..1111111111111111111111111111111111111111111111111111111111111111177788»»»
    length= 70   output=«««..11111111111111111111111111111111111111111111111111111111111111111788»»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    specified immediate repeatable chararacter= .    ('2E'x)
    length= 72    input=«««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
    length= 72   output=«««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    specified immediate repeatable chararacter=      ('20'x)
    length= 72    input=«««                                                   ---  Harry S Truman  »»»
    length= 20   output=««« --- Harry S Truman »»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    specified immediate repeatable chararacter= -    ('2D'x)
    length= 72    input=«««                                                   ---  Harry S Truman  »»»
    length= 70   output=«««                                                   -  Harry S Truman  »»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    specified immediate repeatable chararacter= r    ('72'x)
    length= 72    input=«««                                                   ---  Harry S Truman  »»»
    length= 71   output=«««                                                   ---  Hary S Truman  »»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════