Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
23
Task/Matrix-transposition/Lua/matrix-transposition-1.lua
Normal file
23
Task/Matrix-transposition/Lua/matrix-transposition-1.lua
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
function Transpose( m )
|
||||
local res = {}
|
||||
|
||||
for i = 1, #m[1] do
|
||||
res[i] = {}
|
||||
for j = 1, #m do
|
||||
res[i][j] = m[j][i]
|
||||
end
|
||||
end
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
-- a test for Transpose(m)
|
||||
mat = { { 1, 2, 3 }, { 4, 5, 6 } }
|
||||
erg = Transpose( mat )
|
||||
for i = 1, #erg do
|
||||
for j = 1, #erg[1] do
|
||||
io.write( erg[i][j] )
|
||||
io.write( " " )
|
||||
end
|
||||
io.write( "\n" )
|
||||
end
|
||||
27
Task/Matrix-transposition/Lua/matrix-transposition-2.lua
Normal file
27
Task/Matrix-transposition/Lua/matrix-transposition-2.lua
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
function map(f, a)
|
||||
local b = {}
|
||||
for k,v in ipairs(a) do b[k] = f(v) end
|
||||
return b
|
||||
end
|
||||
|
||||
function mapn(f, ...)
|
||||
local c = {}
|
||||
local k = 1
|
||||
local aarg = {...}
|
||||
local n = #aarg
|
||||
while true do
|
||||
local a = map(function(b) return b[k] end, aarg)
|
||||
if #a < n then return c end
|
||||
c[k] = f(unpack(a))
|
||||
k = k + 1
|
||||
end
|
||||
end
|
||||
|
||||
function apply(f1, f2, a)
|
||||
return f1(f2, unpack(a))
|
||||
end
|
||||
|
||||
xy = {{1,2,3,4},{1,2,3,4},{1,2,3,4}}
|
||||
yx = apply(mapn, function(...) return {...} end, xy)
|
||||
print(table.concat(map(function(a) return table.concat(a,",") end, xy), "\n"),"\n")
|
||||
print(table.concat(map(function(a) return table.concat(a,",") end, yx), "\n"))
|
||||
Loading…
Add table
Add a link
Reference in a new issue