Determine if a string is collapsible: Difference between revisions
Content added Content deleted
m (.. wasn't too sure of the introduction date for the PL/I BIF.) |
(julia example) |
||
Line 87: | Line 87: | ||
__TOC__ |
__TOC__ |
||
=={{header|Julia}}== |
|||
<lang julia>const teststrings = [ |
|||
"", |
|||
""""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 "] |
|||
function collapse(s) |
|||
len = length(s) |
|||
if len < 2 |
|||
return s, len, s, len |
|||
end |
|||
result = last = s[1] |
|||
for c in s[2:end] |
|||
if c != last |
|||
last = c |
|||
result *= c |
|||
end |
|||
end |
|||
return s, len, result, length(result) |
|||
end |
|||
function testcollapse(arr) |
|||
for s in arr |
|||
(s1, len1, s2, len2) = collapse(s) |
|||
println("«««$s1»»» (length $len1)\n collapses to:\n«««$s2»»» (length $len2).\n") |
|||
end |
|||
end |
|||
testcollapse(teststrings) |
|||
</lang>{{out}} |
|||
<pre> |
|||
«««»»» (length 0) |
|||
collapses to: |
|||
«««»»» (length 0). |
|||
«««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»» (length 72) |
|||
collapses to: |
|||
«««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»» (length 70). |
|||
«««..1111111111111111111111111111111111111111111111111111111111111117777888»»» (length 72) |
|||
collapses to: |
|||
«««.178»»» (length 4). |
|||
«««I never give 'em hell, I just tell the truth, and they think it's hell. »»» (length 72) |
|||
collapses to: |
|||
«««I never give 'em hel, I just tel the truth, and they think it's hel. »»» (length 69). |
|||
««« --- Harry S Truman »»» (length 72) |
|||
collapses to: |
|||
««« - Hary S Truman »»» (length 17). |
|||
</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |