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

Published on 12 May 2024 09:40 PM
#J

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

Source code in the j programming language

   dlb
}.~ (=&' ' (i.) 0:)
   dtb
#~ ([: +./\. ' '&~:)
   deb
#~ ((+.) (1: |. (> </\)))@(' '&~:)


   'x' squeeze 'xabxxcxdxxx'  NB. example
xabxcxdx


   ,.' -7. -r' main&.> <;._2 to_be_squished
┌──────────────────────────────────────────────────────────────────────────────┐
│squeeze ' ' reduces the length from 0 to 0
<<<>>>
<<<>>>
├──────────────────────────────────────────────────────────────────────────────┤
│squeeze '-' reduces the length from 71 to 69
<<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln>>>
<<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln>>>
├──────────────────────────────────────────────────────────────────────────────┤
│squeeze '7' reduces the length from 72 to 69
<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
<<<..1111111111111111111111111111111111111111111111111111111111111117888>>>
├──────────────────────────────────────────────────────────────────────────────┤
│squeeze '.' reduces the length from 71 to 71
<<<I never give 'em hell, I just tell the truth, and they think it's hell.>>>
<<<I never give 'em hell, I just tell the truth, and they think it's hell.>>>
├──────────────────────────────────────────────────────────────────────────────┤
│squeeze ' ' reduces the length from 70 to 19
<<<                                                    --- Harry S Truman>>>
<<< --- Harry S Truman>>>
├──────────────────────────────────────────────────────────────────────────────┤
│squeeze '-' reduces the length from 70 to 68
<<<                                                    --- Harry S Truman>>>
<<<                                                    - Harry S Truman>>>
├──────────────────────────────────────────────────────────────────────────────┤
│squeeze 'r' reduces the length from 70 to 69
<<<                                                    --- Harry S Truman>>>
<<<                                                    --- Hary S Truman>>>
└──────────────────────────────────────────────────────────────────────────────┘


NB. Note |.!.1 here instead of the APL version's 1 , }. to more elegantly handle the null case in J
sq =: ] #~ ~: +. _1 |.!.1 ] ~: 1 |. ]


require'format/printf'  NB.  For formatting the output
'C I' =: <"1 |: P =: ; ;"0 1^:(0<#@[)&.>/"1 (({.~ ; (}.~>:)) i.&'|')&> LF cut noun define
|
-|"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln 
7|..1111111111111111111111111111111111111111111111111111111111111117777888
.|I never give 'em hell, I just tell the truth, and they think it's hell. 
 -r|                                                    --- Harry S Truman  
)
S =: sq&.>/"1 P
smoutput 'chr: ''%s''\nin:  %d «««%s»»»\nout: %d «««%s»»»\n' sprintf C ,. (#&.>I),.I ,. (#&.>S),.S


  

You may also check:How to resolve the algorithm Sudan function step by step in the Haskell programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the Kotlin programming language
You may also check:How to resolve the algorithm MD5/Implementation step by step in the RPG programming language
You may also check:How to resolve the algorithm Variadic function step by step in the Euler Math Toolbox programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the K programming language