diff options
Diffstat (limited to 'challenge-040/stuart-little/lua/ch-2.lua')
| -rwxr-xr-x | challenge-040/stuart-little/lua/ch-2.lua | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-040/stuart-little/lua/ch-2.lua b/challenge-040/stuart-little/lua/ch-2.lua new file mode 100755 index 0000000000..746989ea7e --- /dev/null +++ b/challenge-040/stuart-little/lua/ch-2.lua @@ -0,0 +1,30 @@ +#!/usr/bin/env lua + +-- run <script> <comma-separated numbers, space, comma-separated indices> + +function splt(s) + local t={} + for n in s:gmatch("(%d+)") do table.insert(t,tonumber(n)) end + return(t) +end + +function getSl(ar,ixs) + local slc={} + for k=#ixs,1,-1 do + table.insert(slc,table.remove(ar,ixs[k]+1)) + end + return slc +end + +function insMult(ar,ixs,slc) + for _,ix in ipairs(ixs) do + local val=table.remove(slc,1) + table.insert(ar,ix+1,val) + end +end + +local ar,ixs = splt(arg[1]),splt(arg[2]) +local slc=getSl(ar,ixs) +table.sort(slc) +insMult(ar,ixs,slc) +print(table.unpack(ar)) |
