How to resolve the algorithm Determine if a string is collapsible step by step in the JavaScript programming language

Published on 12 May 2024 09:40 PM

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

The provided JavaScript code defines a method called collapse() that extends the String prototype, enabling strings to collapse consecutive duplicate characters into a single instance. Here's a detailed breakdown of the code:

1. Extending the String Prototype:

String.prototype.collapse = function() {

This code adds a new method named collapse() to the String prototype. This means that any string object can now call the collapse() method to perform the collapsing operation.

2. Iterating Over Characters:

 let str = this;
 for (let i = 0; i < str.length; i++) {

The code starts by assigning the current string to the str variable and initializes a loop that iterates over each character in the string. The loop uses an index i to traverse the characters.

3. Collapsing Consecutive Duplicates:

   while (str[i] == str[i+1]) str = str.substr(0,i) + str.substr(i+1);

Inside the loop, there's a nested while loop that checks if the current character (str[i]) is the same as the next character (str[i+1]). If consecutive duplicates are found, the code enters the inner loop.

Within the inner loop, the str variable is reassigned to a new string that consists of the substring before the current position (substr(0,i)) and the substring after the duplicate character (substr(i+1)). This effectively removes the consecutive duplicate characters.

4. Returning the Collapsed String:

 return str;
};

After iterating over all the characters and collapsing consecutive duplicates, the modified str variable, which now contains the collapsed string, is returned.

Testing the Collapse Function:

The code includes a section for testing the collapse() function. It provides an array of input strings (strings) and iterates over them:

// testing
let strings = [
 '',
 '"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 each input string, it calls the collapse() method and prints the original string along with its length, followed by the collapsed string and its length.

This extended collapse() method provides a convenient way to remove consecutive duplicate characters from any string, making it useful for tasks like data cleaning, text processing, or string manipulation in general.

Source code in the javascript programming language

String.prototype.collapse = function() {
  let str = this;
  for (let i = 0; i < str.length; i++) {
    while (str[i] == str[i+1]) str = str.substr(0,i) + str.substr(i+1);
  }
  return str;
}

// testing
let strings = [
  '',
  '"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 (let i = 0; i < strings.length; i++) {
  let str = strings[i], col = str.collapse();
  console.log(`«««${str}»»» (${str.length})`);
  console.log(`«««${col}»»» (${col.length})`);
}


  

You may also check:How to resolve the algorithm Terminal control/Coloured text step by step in the Forth programming language
You may also check:How to resolve the algorithm Sierpinski triangle step by step in the CLU programming language
You may also check:How to resolve the algorithm Knapsack problem/Continuous step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Vector step by step in the Lua programming language
You may also check:How to resolve the algorithm Anagrams step by step in the FreeBASIC programming language