Determine if a string is squeezable: Difference between revisions

Realize in F#
m (Reformatted to reduce line count)
(Realize in F#)
Line 717:
new: <<< --- Hary S Truman >>>, length = 71</pre>
 
=={{header|F_Sharp|F#}}==
<lang fsharp>
// Determine if a string is squeezable. Nigel Galloway: June 9th., 2020
let squeeze n i=if String.length n=0 then None else
let fN=let mutable g=n.[0] in (fun n->if n=i && n=g then false else g<-n; true)
let fG=n.[0..0]+System.String(n.[1..].ToCharArray()|>Array.filter fN)
if fG.Length=n.Length then None else Some fG
let isSqueezable n g=match squeeze n g with
Some i->printfn "%A squeezes <<<%s>>> (length %d) to <<<%s>>> (length %d)" g n n.Length i i.Length
|_->printfn "%A does not squeeze <<<%s>>> (length %d)" g n n.Length
 
isSqueezable "" ' '
isSqueezable "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln " '-'
isSqueezable "..1111111111111111111111111111111111111111111111111111111111111117777888" '7'
isSqueezable "I never give 'em hell, I just tell the truth, and they think it's hell. " '.'
let fN=isSqueezable " --- Harry S Truman " in fN ' '; fN '-'; fN 'r'
</lang>
{{out}}
<pre>
' ' does not squeeze <<<>>> (length 0)
'-' squeezes <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> (length 72) to <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> (length 70)
'7' squeezes <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> (length 72) to <<<..1111111111111111111111111111111111111111111111111111111111111117888>>> (length 69)
'.' does not squeeze <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> (length 72)
' ' squeezes <<< --- Harry S Truman >>> (length 72) to <<< --- Harry S Truman >>> (length 20)
'-' squeezes <<< --- Harry S Truman >>> (length 72) to <<< - Harry S Truman >>> (length 70)
'r' squeezes <<< --- Harry S Truman >>> (length 72) to <<< --- Hary S Truman >>> (length 71)
</pre>
=={{header|Factor}}==
<lang factor>USING: formatting fry io kernel math sbufs sequences strings ;
2,172

edits