How to resolve the algorithm Determine if a string is squeezable step by step in the Raku 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 Raku 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 Raku programming language

Source code in the raku programming language

map {
    my $squeeze = $^phrase;
    sink $^reg;
    $squeeze ~~ s:g/($reg)$0+/$0/;
    printf "\nOriginal length: %d <<<%s>>>\nSqueezable on \"%s\": %s\nSqueezed length: %d <<<%s>>>\n",
      $phrase.chars, $phrase, $reg.uniname, $phrase ne $squeeze, $squeeze.chars, $squeeze
},  
  '', ' ', 
  '"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ', '-',
  '..1111111111111111111111111111111111111111111111111111111111111117777888', '7',
  "I never give 'em hell, I just tell the truth, and they think it's hell. ", '.',
  '                                                    --- Harry S Truman  ', ' ',
  '                                                    --- Harry S Truman  ', '-',
  '                                                    --- Harry S Truman  ', 'r'


  

You may also check:How to resolve the algorithm Substring step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Biorhythms step by step in the Lua programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the REXX programming language
You may also check:How to resolve the algorithm Cut a rectangle step by step in the Python programming language
You may also check:How to resolve the algorithm Tupper's self-referential formula step by step in the Fōrmulæ programming language