RosettaCodeData/Task/Determine-if-a-string-is-squeezable/Pluto/determine-if-a-string-is-squeezable.pluto
2026-04-30 12:34:36 -04:00

38 lines
1.3 KiB
Text

local fmt = require "fmt"
require "uchar"
-- Returns squeezed string, original and new lengths in
-- unicode code points (not normalized).
local function squeeze(s, ch)
local c = uchar.of(s)
local c2 = uchar.of(ch)
local le = c:len()
if le < 2 then return s, le, le end
for i = le - 1, 1, -1 do
if c:get(i) == c2:get(1) and c:get(i) == c:get(i + 1) then c:remove(i) end
end
return c:tostring(), le, c:len()
end
local strings = {
"",
"\"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",
"aardvark",
"😍😀🙌💃😍😍😍🙌"
}
local chars = { {" "}, {"-"}, {"7"}, {"."}, {" ", "-", "r"}, {"e"}, {"s"}, {"a"}, {"😍"} }
for i, s in strings do
for chars[i] as ch do
local cc, le, lcc = squeeze(s, ch)
print($"Specified character = '{ch}'")
fmt.print("original : length = %2d, string = «««%s»»»", le, s)
fmt.print("squeezed : length = %2d, string = «««%s»»»\n", lcc, cc)
end
end