Determine if a string is collapsible: Difference between revisions

Line 1,498:
new: 14 <<<headmistreship>>>
</pre>
 
=={{header|jq}}==
Here we can use the Unix-like `uniq` function defined on (JSON) arrays to define `collapse` on (JSON) strings:
<lang jq># 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; </lang>
 
The program for the given task can now be written in three lines:
 
<lang jq>def Guillemets: "«««\(.)»»»";
 
"Original: \(Guillemets) has length \(length)",
(collapse | "Collapsed \(Guillemets) has length \(length)")
</lang>
 
Using the following invocation on the five "raw" (i.e. unquoted) strings yields the results as shown:
 
jq -Rrf program.jq raw.txt
 
<lang sh>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</lang>
 
 
=={{header|Julia}}==
2,455

edits