Determine if a string is collapsible: Difference between revisions

Tcl
m (promoted draft task to a (full) task.)
(Tcl)
Line 925:
COLLAPSED : length = 7, string = «««ardvark»»»
This string IS collapsible !
</pre>
 
=={{header|Tcl}}==
Please note the ;# ' comments, there appears to be a syntax hightlighting bug with RC
<lang tcl>set test {
{}
{"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 }
{The better the 4-wheel drive, the further you'll be from help when ya get stuck!} ;# '
{headmistressship}
}
 
foreach {str} $test {
# Uses regexp lookbehind to detect repeated characters
set sub [regsub -all {(.)(\1+)} $str {\1}]
 
# Output
puts [format "Original (length %3d): %s" [string length $str] $str]
puts [format "Subbed (length %3d): %s" [string length $sub] $sub]
puts ----------------------
}
</lang>
{{out}}
<pre>
Original (length 0):
Subbed (length 0):
----------------------
Original (length 72): "If I were two-faced, would I be wearing this one?" --- Abraham Lincoln
Subbed (length 70): "If I were two-faced, would I be wearing this one?" - Abraham Lincoln
----------------------
Original (length 72): ..1111111111111111111111111111111111111111111111111111111111111117777888
Subbed (length 4): .178
----------------------
Original (length 72): I never give 'em hell, I just tell the truth, and they think it's hell.
Subbed (length 69): I never give 'em hel, I just tel the truth, and they think it's hel.
----------------------
Original (length 72): --- Harry S Truman
Subbed (length 17): - Hary S Truman
----------------------
Original (length 80): The better the 4-wheel drive, the further you'll be from help when ya get stuck!
Subbed (length 77): The beter the 4-whel drive, the further you'l be from help when ya get stuck!
----------------------
Original (length 16): headmistressship
Subbed (length 14): headmistreship
----------------------
</pre>
 
Anonymous user