Determine if a string is squeezable: Difference between revisions
Content added Content deleted
(Added C#) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 102: | Line 102: | ||
:* [https://rosettacode.org/wiki/Determine_if_a_string_is_collapsible determine if a string is collapsible]. |
:* [https://rosettacode.org/wiki/Determine_if_a_string_is_collapsible determine if a string is collapsible]. |
||
<br><br> |
<br><br> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
Line 173: | Line 172: | ||
new: 14 <<<headmistreship>>> |
new: 14 <<<headmistreship>>> |
||
</pre> |
</pre> |
||
=={{header|C}}== |
=={{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. |
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: | Line 436: | ||
Final ««« --- Hary S Truman »»» |
Final ««« --- Hary S Truman »»» |
||
Length : 71 |
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> |
</pre> |
||
Line 504: | Line 557: | ||
original: <<< --- Harry S Truman >>>, length: 72 |
original: <<< --- Harry S Truman >>>, length: 72 |
||
result: <<< --- Hary S Truman >>>, length: 71 |
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> |
</pre> |
||
Line 823: | Line 823: | ||
Squeezable on "LATIN SMALL LETTER R": True |
Squeezable on "LATIN SMALL LETTER R": True |
||
Squeezed length: 71 <<< --- Hary S Truman >>></pre> |
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}}== |
=={{header|Phix}}== |
||
Line 1,034: | Line 987: | ||
S = "..1111111111111111111111111111111111111111111111111111111111111117888" |
S = "..1111111111111111111111111111111111111111111111111111111111111117888" |
||
</pre> |
</pre> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 1,095: | Line 1,046: | ||
Original Size: 8 «««😍😀🙌💃😍😍😍🙌»»» |
Original Size: 8 «««😍😀🙌💃😍😍😍🙌»»» |
||
Squeezer '😍' Size: 6 «««😍😀🙌💃😍🙌»»»</pre> |
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}}== |
=={{header|REXX}}== |
||
Line 1,159: | Line 1,158: | ||
═════════════════════════════════════════════════════════════════════════════════════════════════════════ |
═════════════════════════════════════════════════════════════════════════════════════════════════════════ |
||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<lang ruby>strings = ["", |
<lang ruby>strings = ["", |