RosettaCodeData/Task/Range-expansion/Pluto/range-expansion.pluto
2026-04-30 12:34:36 -04:00

30 lines
924 B
Text

require "table2"
local function expand_range(s)
local list = {}
local items = s:split(",")
for items as item do
local count = item:split(""):count(|c| -> c == "-")
if count == 0 or (count == 1 and item[1] == "-") then
list:insert(tonumber(item))
else
local items2 = item:split("-")
local first, last
if count == 1 then
first = tonumber(items2[1])
last = tonumber(items2[2])
elseif count == 2 then
first = tonumber(items2[2]) * -1
last = tonumber(items2[3])
else
first = tonumber(items2[2]) * -1
last = tonumber(items2[4]) * -1
end
for i = first, last do list:insert(i) end
end
end
return list
end
local s = "-6,-3--1,3-5,7-11,14,15,17-20"
print(expand_range(s):concat(", "))