En parlant de ruby sur un autre channel, j'en suis venu à montrer ce thread.
TsT a proposé son code en LUA, que je poste à son nom :
Code LUA :
local f = arg[1]
local fd = assert(io.open(f, 'r'))
local res = {}
local b = {}
while true do
local line = fd:read('*l')
if not line or line == "" then
if #b > 0 then
table.sort(b)
res[#res+1] = b
b = {}
end
if not line then break end
else
b[#b+1] = tonumber(line)
end
end
fd:close()
for i,v in ipairs(res) do
res[i] = table.concat(v, "\n")
end
print(table.concat(res, "\n\n"))
-- En minimisé oneliner
fd=io.open(arg[1],"r");res,b={},{};while true do local line=fd:read("*l");if not line or line=="" then if #b>0 then table.sort(b);res[#res+1]=b;b={} end if not line then break end else b[#b+1]=tonumber(line) end end;fd:close();for i,v in ipairs(res) do res[i]=table.concat(v,"\n") end;print(table.concat(res,"\n\n"))
-- Et avec un module
local strong = require("strong") -- https://github.com/BlackBulletIV/strong
local powertable = require("powertable") -- TsT small module
local function tprint(t) for aa,bb in ipairs(t) do print(" ", aa,bb) end end
local f = arg[1]
local fd = assert(io.open(f, "r"))
local res = {}
for elem in fd:read("*a"):lines("\n\n") do
res[#res+1] = powertable( elem:chomp()plit("\n", true) ):tsort( function(x,y) return tonumber(x) < tonumber(y) end ):concat("\n")
end
print(table.concat(res, "\n\n"))
-- Optimisé performance
local function process_data(fd, para_handler)
local buf = {} -- buffer for each lines of the current paragraph
while true do
local line = fd:read("*l") -- read one line
if not line then break end -- stop if the end of file is reached
if line == "" then -- a empty line = end of paragraph
para_handler(buf)
buf = {} -- reset buffer
else
table.insert(buf, tonumber(line))
end
end
para_handler(buf)
end
local function para_handler(buf, eof)
-- new paragraph ready
table.sort(buf)
print(table.concat(buf, "\n").."\n")
end
local f = arg[1]
local fd = assert(io.open(f, "r"))
process_data(fd, para_handler)
fd:close()
Voilà.
Temps et conso dernier code (d'après gruik):
Time : 8,80s
Conso : < 5k