Determine if a string is collapsible: Difference between revisions
Content added Content deleted
Line 1,121: | Line 1,121: | ||
original : length = 8, string = «««aardvark»»» |
original : length = 8, string = «««aardvark»»» |
||
collapsed : length = 7, string = «««ardvark»»»</pre> |
collapsed : length = 7, string = «««ardvark»»»</pre> |
||
=={{header|Lua}}== |
|||
{{trans|C#}} |
|||
<lang lua>function collapse(s) |
|||
local ns = "" |
|||
local last = nil |
|||
for c in s:gmatch"." do |
|||
if last then |
|||
if last ~= c then |
|||
ns = ns .. c |
|||
end |
|||
last = c |
|||
else |
|||
ns = ns .. c |
|||
last = c |
|||
end |
|||
end |
|||
return ns |
|||
end |
|||
function test(s) |
|||
print("old: " .. s:len() .. " <<<" .. s .. ">>>") |
|||
local a = collapse(s) |
|||
print("new: " .. a:len() .. " <<<" .. a .. ">>>") |
|||
end |
|||
function main() |
|||
test("") |
|||
test("The better the 4-wheel drive, the further you'll be from help when ya get stuck!") |
|||
test("headmistressship") |
|||
test("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ") |
|||
test("..1111111111111111111111111111111111111111111111111111111111111117777888") |
|||
test("I never give 'em hell, I just tell the truth, and they think it's hell. ") |
|||
test(" --- Harry S Truman ") |
|||
end |
|||
main()</lang> |
|||
{{out}} |
|||
<pre>old: 0 <<<>>> |
|||
new: 0 <<<>>> |
|||
old: 80 <<<The better the 4-wheel drive, the further you'll be from help when ya get stuck!>>> |
|||
new: 77 <<<The beter the 4-whel drive, the further you'l be from help when ya get stuck!>>> |
|||
old: 16 <<<headmistressship>>> |
|||
new: 14 <<<headmistreship>>> |
|||
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 >>> |
|||
old: 72 <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> |
|||
new: 4 <<<.178>>> |
|||
old: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> |
|||
new: 69 <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>> |
|||
old: 72 <<< --- Harry S Truman >>> |
|||
new: 17 <<< - Hary S Truman >>></pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |