Determine if a string is squeezable: Difference between revisions

(→‎{{header|Perl 6}}: Add Python.)
Line 300:
Squeezable on "LATIN SMALL LETTER R": True
Squeezed length: 71 <<< --- Hary S Truman >>>
</pre>
 
=={{header|Phix}}==
<lang Phix>constant tests = {"", " ",
`"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln `,"-",
"..1111111111111111111111111111111111111111111111111111111111111117777888","7",
"I never give 'em hell, I just tell the truth, and they think it's hell. ",".",
" --- Harry S Truman "," -r"},
fmt = """
length %2d input: <<<%s>>>
length %2d squeeze(%c): <<<%s>>>
"""
function squeeze(sequence s, integer ch)
if length(s)>1 then
integer outdx = 1
object prev = s[1]
for i=2 to length(s) do
if s[i]!=prev or prev!=ch then
prev = s[i]
outdx += 1
s[outdx] = prev
end if
end for
s = s[1..outdx]
end if
return s
end function
 
for i=1 to length(tests) by 2 do
string ti = tests[i], chars = tests[i+1]
for j=1 to length(chars) do
string si = squeeze(ti,chars[j])
printf(1,fmt,{length(ti),ti,length(si),chars[j],si})
end for
end for</lang>
{{out}}
<pre>
length 0 input: <<<>>>
length 0 squeeze( ): <<<>>>
 
length 72 input: <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
length 70 squeeze(-): <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>
 
length 72 input: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
length 69 squeeze(7): <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>
 
length 72 input: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
length 72 squeeze(.): <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
 
length 72 input: <<< --- Harry S Truman >>>
length 20 squeeze( ): <<< --- Harry S Truman >>>
 
length 72 input: <<< --- Harry S Truman >>>
length 70 squeeze(-): <<< - Harry S Truman >>>
 
length 72 input: <<< --- Harry S Truman >>>
length 71 squeeze(r): <<< --- Hary S Truman >>>
</pre>
 
7,820

edits