Modul:Mergesort: Unterschied zwischen den Versionen
Aus KGS-Wiki
Sn (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „local p = {} function p.render() local a = math.random(99) local b = math.random(99) local c = math.random(99) local d = math.random(99) local e = math.random(99) local f = math.random(99) local g = math.random(99) local h = math.random(99) local content = 'graph TD\n' content = content .. 'subgraph divide\n' content = content .. 'a[' .. a .. ' ' .. b .. ' ' .. c .. ' ' .. d .. ' ' .. e .. ' ' .. f .. ' ' .. g .. ' ' .. h .. ']\n' content = c…“) |
Sn (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
||
(18 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
Zeile 1: | Zeile 1: | ||
local p = {} | local p = {} | ||
function | function createRandomList() | ||
local | math.randomseed( tonumber( mw.getContentLanguage():formatDate( "U" ) ) * 10000 + os.clock() * 10000 ) | ||
local | local res = {} | ||
local | for i = 1,8 do | ||
local | res[i] = math.random(99) | ||
local | end | ||
local | return res | ||
local | end | ||
local | |||
function sublist(l,from,to) | |||
local res = {} | |||
local index = 1 | |||
for i = from,to do | |||
res[index] = l[i] | |||
index = index + 1 | |||
end | |||
return res | |||
end | |||
function p.example() | |||
local l = createRandomList() | |||
local l14 = sublist(l,1,4) | |||
local l12 = sublist(l,1,2) | |||
local l34 = sublist(l,3,4) | |||
local l58 = sublist(l,5,8) | |||
local l56 = sublist(l,5,6) | |||
local l78 = sublist(l,7,8) | |||
frame = mw.getCurrentFrame() | |||
local content = 'graph TD\n' | local content = 'graph TD\n' | ||
content = content .. 'subgraph divide\n' | content = content .. 'subgraph divide\n' | ||
content = content .. 'a[' .. | content = content .. 'a[' .. table.concat(l,' ') .. ']\n' | ||
content = content .. 'a --> b[' .. | content = content .. 'a --> b[' .. table.concat(l14,' ') .. '] & c[' .. table.concat(l58,' ') .. ']\n' | ||
content = content .. 'b --> d[' .. | content = content .. 'b --> d[' .. table.concat(l12,' ') .. '] & e[' .. table.concat(l34,' ') .. ']\n' | ||
content = content .. 'c --> f[' .. | content = content .. 'c --> f[' .. table.concat(l56,' ') .. '] & g[' .. table.concat(l78,' ') .. ']\n' | ||
content = content .. 'd --> h[' .. | content = content .. 'd --> h[' .. l[1] .. '] & i[' .. l[2] .. ']\n' | ||
content = content .. 'e --> j[' .. | content = content .. 'e --> j[' .. l[3] .. '] & k[' .. l[4] .. ']\n' | ||
content = content .. 'f --> l[' .. | content = content .. 'f --> l[' .. l[5] .. '] & m[' .. l[6] .. ']\n' | ||
content = content .. 'g --> n[' .. | content = content .. 'g --> n[' .. l[7] .. '] & o[' .. l[8] .. ']\n' | ||
content = content .. 'end\n' | content = content .. 'end\n' | ||
table.sort(l) | |||
table.sort(l12) | |||
table.sort(l34) | |||
table.sort(l56) | |||
table.sort(l78) | |||
table.sort(l14) | |||
table.sort(l58) | |||
content = content .. 'subgraph conquer\n' | |||
content = content .. 'h --> p[' .. table.concat(l12,' ') .. ']\n' | |||
content = content .. 'i --> p\n' | |||
content = content .. 'j --> q[' .. table.concat(l34,' ') .. ']\n' | |||
content = content .. 'k --> q\n' | |||
content = content .. 'l --> r[' .. table.concat(l56,' ') .. ']\n' | |||
content = content .. 'm --> r\n' | |||
content = content .. 'n --> s[' .. table.concat(l78,' ') .. ']\n' | |||
content = content .. 'o --> s\n' | |||
content = content .. 'p --> t[' .. table.concat(l14,' ') .. ']\n' | |||
content = content .. 'q --> t\n' | |||
content = content .. 'r --> u[' .. table.concat(l58,' ') .. ']\n' | |||
content = content .. 's --> u\n' | |||
content = content .. 't --> v[' .. table.concat(l,' ') .. ']\n' | |||
content = content .. 'u --> v\n' | |||
content = content .. 'end' | |||
local graph = frame:extensionTag('mermaid', content, {}) | local graph = frame:extensionTag('mermaid', content, {}) | ||
return graph | return graph |
Aktuelle Version vom 3. Juni 2024, 14:17 Uhr
Die Dokumentation für dieses Modul kann unter Modul:Mergesort/Doku erstellt werden
local p = {}
function createRandomList()
math.randomseed( tonumber( mw.getContentLanguage():formatDate( "U" ) ) * 10000 + os.clock() * 10000 )
local res = {}
for i = 1,8 do
res[i] = math.random(99)
end
return res
end
function sublist(l,from,to)
local res = {}
local index = 1
for i = from,to do
res[index] = l[i]
index = index + 1
end
return res
end
function p.example()
local l = createRandomList()
local l14 = sublist(l,1,4)
local l12 = sublist(l,1,2)
local l34 = sublist(l,3,4)
local l58 = sublist(l,5,8)
local l56 = sublist(l,5,6)
local l78 = sublist(l,7,8)
frame = mw.getCurrentFrame()
local content = 'graph TD\n'
content = content .. 'subgraph divide\n'
content = content .. 'a[' .. table.concat(l,' ') .. ']\n'
content = content .. 'a --> b[' .. table.concat(l14,' ') .. '] & c[' .. table.concat(l58,' ') .. ']\n'
content = content .. 'b --> d[' .. table.concat(l12,' ') .. '] & e[' .. table.concat(l34,' ') .. ']\n'
content = content .. 'c --> f[' .. table.concat(l56,' ') .. '] & g[' .. table.concat(l78,' ') .. ']\n'
content = content .. 'd --> h[' .. l[1] .. '] & i[' .. l[2] .. ']\n'
content = content .. 'e --> j[' .. l[3] .. '] & k[' .. l[4] .. ']\n'
content = content .. 'f --> l[' .. l[5] .. '] & m[' .. l[6] .. ']\n'
content = content .. 'g --> n[' .. l[7] .. '] & o[' .. l[8] .. ']\n'
content = content .. 'end\n'
table.sort(l)
table.sort(l12)
table.sort(l34)
table.sort(l56)
table.sort(l78)
table.sort(l14)
table.sort(l58)
content = content .. 'subgraph conquer\n'
content = content .. 'h --> p[' .. table.concat(l12,' ') .. ']\n'
content = content .. 'i --> p\n'
content = content .. 'j --> q[' .. table.concat(l34,' ') .. ']\n'
content = content .. 'k --> q\n'
content = content .. 'l --> r[' .. table.concat(l56,' ') .. ']\n'
content = content .. 'm --> r\n'
content = content .. 'n --> s[' .. table.concat(l78,' ') .. ']\n'
content = content .. 'o --> s\n'
content = content .. 'p --> t[' .. table.concat(l14,' ') .. ']\n'
content = content .. 'q --> t\n'
content = content .. 'r --> u[' .. table.concat(l58,' ') .. ']\n'
content = content .. 's --> u\n'
content = content .. 't --> v[' .. table.concat(l,' ') .. ']\n'
content = content .. 'u --> v\n'
content = content .. 'end'
local graph = frame:extensionTag('mermaid', content, {})
return graph
end
return p