Determine if a string is collapsible: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 146:
new: 14 <<<headmistreship>>>
</pre>
 
=={{header|C}}==
Identical implementation as in [[Determine_if_a_string_is_squeezable]], 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 371 ⟶ 372:
Final ««« - Hary S Truman»»»
Length : 16
</pre>
 
=={{header|C sharp}}==
<lang csharp>using System;
using static System.Linq.Enumerable;
 
public class Program
{
static void Main()
{
string[] input = {
"",
"The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
"headmistressship",
"\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ",
"..1111111111111111111111111111111111111111111111111111111111111117777888",
"I never give 'em hell, I just tell the truth, and they think it's hell. ",
" --- Harry S Truman "
};
foreach (string s in input) {
Console.WriteLine($"old: {s.Length} «««{s}»»»");
string c = Collapse(s);
Console.WriteLine($"new: {c.Length} «««{c}»»»");
}
}
 
static string Collapse(string s) => string.IsNullOrEmpty(s) ? "" :
s[0] + new string(Range(1, s.Length - 1).Where(i => s[i] != s[i - 1]).Select(i => s[i]).ToArray());
}</lang>
{{out}}
<pre>
old: 0 «««»»»
new: 0 «««»»»
old: 80 «««The better the 4-wheel drive, the further you'll be from help when ya get stuck!»»»
new: 77 «««The beter the 4-whel drive, the further you'l be from help when ya get stuck!»»»
old: 16 «««headmistressship»»»
new: 14 «««headmistreship»»»
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 »»»
old: 72 «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
new: 4 «««.178»»»
old: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
new: 69 «««I never give 'em hel, I just tel the truth, and they think it's hel. »»»
old: 72 ««« --- Harry S Truman »»»
new: 17 ««« - Hary S Truman »»»
</pre>
 
Line 421 ⟶ 467:
original string: <<< --- Harry S Truman >>>, length = 72
result string: <<< - Hary S Truman >>>, length = 17
</pre>
 
=={{header|C sharp}}==
<lang csharp>using System;
using static System.Linq.Enumerable;
 
public class Program
{
static void Main()
{
string[] input = {
"",
"The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
"headmistressship",
"\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ",
"..1111111111111111111111111111111111111111111111111111111111111117777888",
"I never give 'em hell, I just tell the truth, and they think it's hell. ",
" --- Harry S Truman "
};
foreach (string s in input) {
Console.WriteLine($"old: {s.Length} «««{s}»»»");
string c = Collapse(s);
Console.WriteLine($"new: {c.Length} «««{c}»»»");
}
}
 
static string Collapse(string s) => string.IsNullOrEmpty(s) ? "" :
s[0] + new string(Range(1, s.Length - 1).Where(i => s[i] != s[i - 1]).Select(i => s[i]).ToArray());
}</lang>
{{out}}
<pre>
old: 0 «««»»»
new: 0 «««»»»
old: 80 «««The better the 4-wheel drive, the further you'll be from help when ya get stuck!»»»
new: 77 «««The beter the 4-whel drive, the further you'l be from help when ya get stuck!»»»
old: 16 «««headmistressship»»»
new: 14 «««headmistreship»»»
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 »»»
old: 72 «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
new: 4 «««.178»»»
old: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
new: 69 «««I never give 'em hel, I just tel the truth, and they think it's hel. »»»
old: 72 ««« --- Harry S Truman »»»
new: 17 ««« - Hary S Truman »»»
</pre>
 
Line 826 ⟶ 827:
Collapsible: True
Length: 1 <<<A>>></pre>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2019.07.1}}
Technically, the task is asking for a boolean. "Determine if a string is collapsible" is answerable with True/False, so return a boolean as well.
 
<lang perl6>map {
my $squish = .comb.squish.join;
printf "\nLength: %2d <<<%s>>>\nCollapsible: %s\nLength: %2d <<<%s>>>\n",
.chars, $_, .chars != $squish.chars, $squish.chars, $squish
}, lines
q:to/STRINGS/;
"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln
..1111111111111111111111111111111111111111111111111111111111111117777888
I never give 'em hell, I just tell the truth, and they think it's hell.
--- Harry S Truman
The American people have a right to know if their president is a crook.
--- Richard Nixon
AАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑ
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
STRINGS
</lang>
{{out}}
<pre>Length: 0 <<<>>>
Collapsible: False
Length: 0 <<<>>>
 
Length: 72 <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
Collapsible: True
Length: 70 <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>
 
Length: 72 <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
Collapsible: True
Length: 4 <<<.178>>>
 
Length: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
Collapsible: True
Length: 69 <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>>
 
Length: 72 <<< --- Harry S Truman >>>
Collapsible: True
Length: 17 <<< - Hary S Truman >>>
 
Length: 72 <<<The American people have a right to know if their president is a crook. >>>
Collapsible: True
Length: 71 <<<The American people have a right to know if their president is a crok. >>>
 
Length: 72 <<< --- Richard Nixon >>>
Collapsible: True
Length: 17 <<< - Richard Nixon >>>
 
Length: 72 <<<AАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑ>>>
Collapsible: False
Length: 72 <<<AАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑ>>>
 
Length: 72 <<<AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA>>>
Collapsible: True
Length: 1 <<<A>>>
</pre>
 
=={{header|Phix}}==
Line 998 ⟶ 939:
Original : <<<😍😀🙌💃😍😍😍🙌>>> (len=8)
Collapse : <<<😍😀🙌💃😍🙌>>> (len=6)</pre>
 
 
=={{header|Prolog}}==
Line 1,098 ⟶ 1,038:
Original Size: 8 «««😍😀🙌💃😍😍😍🙌»»»
Collapsed Size: 6 «««😍😀🙌💃😍🙌»»»</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2019.07.1}}
Technically, the task is asking for a boolean. "Determine if a string is collapsible" is answerable with True/False, so return a boolean as well.
 
<lang perl6>map {
my $squish = .comb.squish.join;
printf "\nLength: %2d <<<%s>>>\nCollapsible: %s\nLength: %2d <<<%s>>>\n",
.chars, $_, .chars != $squish.chars, $squish.chars, $squish
}, lines
q:to/STRINGS/;
"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln
..1111111111111111111111111111111111111111111111111111111111111117777888
I never give 'em hell, I just tell the truth, and they think it's hell.
--- Harry S Truman
The American people have a right to know if their president is a crook.
--- Richard Nixon
AАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑ
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
STRINGS
</lang>
{{out}}
<pre>Length: 0 <<<>>>
Collapsible: False
Length: 0 <<<>>>
 
Length: 72 <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
Collapsible: True
Length: 70 <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>
 
Length: 72 <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
Collapsible: True
Length: 4 <<<.178>>>
 
Length: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
Collapsible: True
Length: 69 <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>>
 
Length: 72 <<< --- Harry S Truman >>>
Collapsible: True
Length: 17 <<< - Hary S Truman >>>
 
Length: 72 <<<The American people have a right to know if their president is a crook. >>>
Collapsible: True
Length: 71 <<<The American people have a right to know if their president is a crok. >>>
 
Length: 72 <<< --- Richard Nixon >>>
Collapsible: True
Length: 17 <<< - Richard Nixon >>>
 
Length: 72 <<<AАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑ>>>
Collapsible: False
Length: 72 <<<AАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑAАΑ>>>
 
Length: 72 <<<AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA>>>
Collapsible: True
Length: 1 <<<A>>>
</pre>
 
=={{header|REXX}}==
10,339

edits