How to resolve the algorithm Determine if a string is collapsible step by step in the jq programming language
How to resolve the algorithm Determine if a string is collapsible step by step in the jq 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 jq programming language
Source code in the jq programming language
# Input: an array
# Output: a stream
def uniq: foreach .[] as $x ({x:nan, y:.[0]}; {x:$x, y:.x}; select(.x != .y) | .x);
def collapse: explode | [uniq] | implode;
def Guillemets: "«««\(.)»»»";
"Original: \(Guillemets) has length \(length)",
(collapse | "Collapsed \(Guillemets) has length \(length)")
Original: «««»»» has length 0
Collapsed «««»»» has length 0
Original: «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»» has length 72
Collapsed «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»» has length 70
Original: «««..1111111111111111111111111111111111111111111111111111111111111117777888»»» has length 72
Collapsed «««.178»»» has length 4
Original: «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» has length 72
Collapsed «««I never give 'em hel, I just tel the truth, and they think it's hel. »»» has length 69
Original: ««« --- Harry S Truman »»» has length 72
Collapsed ««« - Hary S Truman »»» has length 17
You may also check:How to resolve the algorithm Mutual recursion step by step in the MiniZinc programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the Pascal programming language
You may also check:How to resolve the algorithm Discordian date step by step in the Phix programming language
You may also check:How to resolve the algorithm Modular arithmetic step by step in the Owl Lisp programming language
You may also check:How to resolve the algorithm Lucas-Lehmer test step by step in the Factor programming language