Determine if a string is squeezable: Difference between revisions

Content added Content deleted
(Added Sidef)
(Added C#)
Line 504: Line 504:
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>