Determine if a string is squeezable: Difference between revisions

Content added Content deleted
(Added C#)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 102:
:*   [https://rosettacode.org/wiki/Determine_if_a_string_is_collapsible determine if a string is collapsible].
<br><br>
 
 
=={{header|AWK}}==
Line 173 ⟶ 172:
new: 14 <<<headmistreship>>>
</pre>
 
=={{header|C}}==
Identical implementation as in [[Determine_if_a_string_is_collapsible]], as both tasks are very similar. The Lincoln quote contains backslashes to accommodate the double quotes via the command line. strcmpi is not part of the C Standard Library, thus comment out the definition in the code if testing on a system where it is already included.
Line 436:
Final ««« --- Hary S Truman »»»
Length : 71
</pre>
 
=={{header|C sharp}}==
<lang csharp>using System;
using static System.Linq.Enumerable;
 
public class Program
{
static void Main()
{
SqueezeAndPrint("", ' ');
SqueezeAndPrint("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ", '-');
SqueezeAndPrint("..1111111111111111111111111111111111111111111111111111111111111117777888", '7');
SqueezeAndPrint("I never give 'em hell, I just tell the truth, and they think it's hell. ", '.');
string s = " --- Harry S Truman ";
SqueezeAndPrint(s, ' ');
SqueezeAndPrint(s, '-');
SqueezeAndPrint(s, 'r');
}
 
static void SqueezeAndPrint(string s, char c) {
Console.WriteLine($"squeeze: '{c}'");
Console.WriteLine($"old: {s.Length} «««{s}»»»");
s = Squeeze(s, c);
Console.WriteLine($"new: {s.Length} «««{s}»»»");
}
 
static string Squeeze(string s, char c) => string.IsNullOrEmpty(s) ? "" :
s[0] + new string(Range(1, s.Length - 1).Where(i => s[i] != c || s[i] != s[i - 1]).Select(i => s[i]).ToArray());
}</lang>
{{out}}
<pre>
squeeze: ' '
old: 0 «««»»»
new: 0 «««»»»
squeeze: '-'
old: 72 «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»»
new: 70 «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»»
squeeze: '7'
old: 72 «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
new: 69 «««..1111111111111111111111111111111111111111111111111111111111111117888»»»
squeeze: '.'
old: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
new: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
squeeze: ' '
old: 72 ««« --- Harry S Truman »»»
new: 20 ««« --- Harry S Truman »»»
squeeze: '-'
old: 72 ««« --- Harry S Truman »»»
new: 70 ««« - Harry S Truman »»»
squeeze: 'r'
old: 72 ««« --- Harry S Truman »»»
new: 71 ««« --- Hary S Truman »»»
</pre>
 
Line 504 ⟶ 557:
original: <<< --- Harry S Truman >>>, length: 72
result: <<< --- Hary S Truman >>>, length: 71
</pre>
 
=={{header|C sharp}}==
<lang csharp>using System;
using static System.Linq.Enumerable;
 
public class Program
{
static void Main()
{
SqueezeAndPrint("", ' ');
SqueezeAndPrint("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ", '-');
SqueezeAndPrint("..1111111111111111111111111111111111111111111111111111111111111117777888", '7');
SqueezeAndPrint("I never give 'em hell, I just tell the truth, and they think it's hell. ", '.');
string s = " --- Harry S Truman ";
SqueezeAndPrint(s, ' ');
SqueezeAndPrint(s, '-');
SqueezeAndPrint(s, 'r');
}
 
static void SqueezeAndPrint(string s, char c) {
Console.WriteLine($"squeeze: '{c}'");
Console.WriteLine($"old: {s.Length} «««{s}»»»");
s = Squeeze(s, c);
Console.WriteLine($"new: {s.Length} «««{s}»»»");
}
 
static string Squeeze(string s, char c) => string.IsNullOrEmpty(s) ? "" :
s[0] + new string(Range(1, s.Length - 1).Where(i => s[i] != c || s[i] != s[i - 1]).Select(i => s[i]).ToArray());
}</lang>
{{out}}
<pre>
squeeze: ' '
old: 0 «««»»»
new: 0 «««»»»
squeeze: '-'
old: 72 «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»»
new: 70 «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»»
squeeze: '7'
old: 72 «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
new: 69 «««..1111111111111111111111111111111111111111111111111111111111111117888»»»
squeeze: '.'
old: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
new: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
squeeze: ' '
old: 72 ««« --- Harry S Truman »»»
new: 20 ««« --- Harry S Truman »»»
squeeze: '-'
old: 72 ««« --- Harry S Truman »»»
new: 70 ««« - Harry S Truman »»»
squeeze: 'r'
old: 72 ««« --- Harry S Truman »»»
new: 71 ««« --- Hary S Truman »»»
</pre>
 
Line 823:
Squeezable on "LATIN SMALL LETTER R": True
Squeezed length: 71 <<< --- Hary S Truman >>></pre>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2019.07.1}}
 
<lang perl6>map {
my $squeeze = $^phrase;
sink $^reg;
$squeeze ~~ s:g/($reg)$0+/$0/;
printf "\nOriginal length: %d <<<%s>>>\nSqueezable on \"%s\": %s\nSqueezed length: %d <<<%s>>>\n",
$phrase.chars, $phrase, $reg.uniname, $phrase ne $squeeze, $squeeze.chars, $squeeze
},
'', ' ',
'"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ', '-',
'..1111111111111111111111111111111111111111111111111111111111111117777888', '7',
"I never give 'em hell, I just tell the truth, and they think it's hell. ", '.',
' --- Harry S Truman ', ' ',
' --- Harry S Truman ', '-',
' --- Harry S Truman ', 'r'</lang>
{{out}}
<pre>Original length: 0 <<<>>>
Squeezable on "SPACE": False
Squeezed length: 0 <<<>>>
 
Original length: 72 <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
Squeezable on "HYPHEN-MINUS": True
Squeezed length: 70 <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>
 
Original length: 72 <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
Squeezable on "DIGIT SEVEN": True
Squeezed length: 69 <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>
 
Original length: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
Squeezable on "FULL STOP": False
Squeezed length: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
 
Original length: 72 <<< --- Harry S Truman >>>
Squeezable on "SPACE": True
Squeezed length: 20 <<< --- Harry S Truman >>>
 
Original length: 72 <<< --- Harry S Truman >>>
Squeezable on "HYPHEN-MINUS": True
Squeezed length: 70 <<< - Harry S Truman >>>
 
Original length: 72 <<< --- Harry S Truman >>>
Squeezable on "LATIN SMALL LETTER R": True
Squeezed length: 71 <<< --- Hary S Truman >>>
</pre>
 
=={{header|Phix}}==
Line 1,034 ⟶ 987:
S = "..1111111111111111111111111111111111111111111111111111111111111117888"
</pre>
 
 
 
=={{header|Python}}==
Line 1,095 ⟶ 1,046:
Original Size: 8 «««😍😀🙌💃😍😍😍🙌»»»
Squeezer '😍' Size: 6 «««😍😀🙌💃😍🙌»»»</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2019.07.1}}
 
<lang perl6>map {
my $squeeze = $^phrase;
sink $^reg;
$squeeze ~~ s:g/($reg)$0+/$0/;
printf "\nOriginal length: %d <<<%s>>>\nSqueezable on \"%s\": %s\nSqueezed length: %d <<<%s>>>\n",
$phrase.chars, $phrase, $reg.uniname, $phrase ne $squeeze, $squeeze.chars, $squeeze
},
'', ' ',
'"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ', '-',
'..1111111111111111111111111111111111111111111111111111111111111117777888', '7',
"I never give 'em hell, I just tell the truth, and they think it's hell. ", '.',
' --- Harry S Truman ', ' ',
' --- Harry S Truman ', '-',
' --- Harry S Truman ', 'r'</lang>
{{out}}
<pre>Original length: 0 <<<>>>
Squeezable on "SPACE": False
Squeezed length: 0 <<<>>>
 
Original length: 72 <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
Squeezable on "HYPHEN-MINUS": True
Squeezed length: 70 <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>
 
Original length: 72 <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
Squeezable on "DIGIT SEVEN": True
Squeezed length: 69 <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>
 
Original length: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
Squeezable on "FULL STOP": False
Squeezed length: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
 
Original length: 72 <<< --- Harry S Truman >>>
Squeezable on "SPACE": True
Squeezed length: 20 <<< --- Harry S Truman >>>
 
Original length: 72 <<< --- Harry S Truman >>>
Squeezable on "HYPHEN-MINUS": True
Squeezed length: 70 <<< - Harry S Truman >>>
 
Original length: 72 <<< --- Harry S Truman >>>
Squeezable on "LATIN SMALL LETTER R": True
Squeezed length: 71 <<< --- Hary S Truman >>>
</pre>
 
=={{header|REXX}}==
Line 1,159 ⟶ 1,158:
═════════════════════════════════════════════════════════════════════════════════════════════════════════
</pre>
 
=={{header|Ruby}}==
<lang ruby>strings = ["",