Determine if a string is collapsible: Difference between revisions

Added C#
(Added Sidef)
(Added C#)
Line 421:
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>
 
196

edits