Determine if a string is squeezable: Difference between revisions

Content added Content deleted
Line 1,396:
new: 14 <<<headmistreship>>>
</pre>
 
=={{header|jq}}==
 
===Preamble===
The PL/I documentation makes it clear that all runs of the specified character are to
be squeezed:
 
* "SQUEEZE returns a string that reduces all multiple occurrences of a [specified] character to one ..."
* The example given is: ' abc : def gh '
** i.e. 'SabcSS:SSdefSSSghSS' where S signifies a single space character.
 
=== jq program ===
<lang jq># Assume $c is specified as a single character correctly
def squeeze($c): gsub("[\($c)]+"; $c);
 
def Guillemets: "«««\(.)»»»";
 
def Squeeze(s; $c):
"Squeeze character: \($c)",
(s | "Original: \(Guillemets) has length \(length)",
(squeeze($c) | "result is \(Guillemets) with length \(length)")),
"";</lang>
=== Demo ===
<lang jq> Squeeze("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "; "-"),
Squeeze ("..1111111111111111111111111111111111111111111111111111111111111117777888"; "7"),
Squeeze ("I never give 'em hell, I just tell the truth, and they think it's hell. "; "."),
Squeeze (" --- Harry S Truman "; " "),
Squeeze (" --- Harry S Truman "; "-"),
Squeeze (" --- Harry S Truman "; "r"),
 
Squeeze("SabcSS:SSdefSSSghSS";"S")</lang>
{{out}}
<lang sh>Squeeze character: -
Original: «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»» has length 72
result is «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»» with length 70
 
Squeeze character: 7
Original: «««..1111111111111111111111111111111111111111111111111111111111111117777888»»» has length 72
result is «««..1111111111111111111111111111111111111111111111111111111111111117888»»» with length 69
 
Squeeze character: .
Original: «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» has length 72
result is «««.»»» with length 1
 
Squeeze character:
Original: ««« --- Harry S Truman »»» has length 72
result is ««« --- Harry S Truman »»» with length 20
 
Squeeze character: -
Original: ««« --- Harry S Truman »»» has length 72
result is ««« - Harry S Truman »»» with length 70
 
Squeeze character: r
Original: ««« --- Harry S Truman »»» has length 72
result is ««« --- Hary S Truman »»» with length 71
 
Squeeze character: S
Original: «««SabcSS:SSdefSSSghSS»»» has length 19
result is «««SabcS:SdefSghS»»» with length 14</lang>
 
=={{header|Julia}}==