Determine if a string is squeezable: Difference between revisions

(Realize in F#)
Line 1,725:
 
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<lang vbnet>Imports System.Linq.Enumerable
 
Module Module1
 
Function Squeeze(s As String, c As Char) As String
If String.IsNullOrEmpty(s) Then
Return ""
End If
Return s(0) + New String(Range(1, s.Length - 1).Where(Function(i) s(i) <> c OrElse s(i) <> s(i - 1)).Select(Function(i) s(i)).ToArray())
End Function
 
Sub SqueezeAndPrint(s As String, c As Char)
Console.WriteLine("squeeze: '{0}'", c)
Console.WriteLine("old: {0} «««{1}»»»", s.Length, s)
s = Squeeze(s, c)
Console.WriteLine("new: {0} «««{1}»»»", s.Length, s)
End Sub
 
Sub Main()
Const QUOTE = """"
 
SqueezeAndPrint("", " ")
SqueezeAndPrint(QUOTE & "If I were two-faced, would I be wearing this one?" & QUOTE & " --- Abraham Lincoln ", "-")
SqueezeAndPrint("..1111111111111111111111111111111111111111111111111111111111111117777888", "7")
SqueezeAndPrint("I never give 'em hell, I just tell the truth, and they think it's hell. ", ".")
 
Dim s = " --- Harry S Truman "
SqueezeAndPrint(s, " ")
SqueezeAndPrint(s, "-")
SqueezeAndPrint(s, "r")
End Sub
 
End Module</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>
 
=={{header|Wren}}==
1,452

edits