Modul:Mergesort: Unterschied zwischen den Versionen

Aus KGS-Wiki
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 22: Zeile 22:
function p.example()
function p.example()
local l = createRandomList()
local l = createRandomList()
local l14 = sublist(l,1,4)
local l12 = sublist(l,1,2)
local l58 = sublist(l,5,8)
frame = mw.getCurrentFrame()
frame = mw.getCurrentFrame()
local content = 'graph TD\n'
local content = 'graph TD\n'
Zeile 34: Zeile 37:
content = content .. 'g --> n[' .. l[7] .. '] & o[' .. l[8] .. ']\n'
content = content .. 'g --> n[' .. l[7] .. '] & o[' .. l[8] .. ']\n'
content = content .. 'end\n'
content = content .. 'end\n'
table.sort(l12)
content = content .. 'subgraph conquer\n'
content = content .. 'subgraph conquer\n'
content = content .. 'h --> p[' .. table.concat(table.sort(sublist(l,1,2)),' ') .. ']\n'
content = content .. 'h --> p[' .. table.concat(l12,' ') .. ']\n'
content = content .. 'i --> p\n'
content = content .. 'i --> p\n'
--j --> q["{{#af_join:{{#af_sort:{{#af_list:{{{3|58}}} | {{{4|61}}}}}}}| }}"]
--j --> q["{{#af_join:{{#af_sort:{{#af_list:{{{3|58}}} | {{{4|61}}}}}}}| }}"]

Version vom 3. Juni 2024, 13:00 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 l58 = sublist(l,5,8)
	frame = mw.getCurrentFrame()
	local content = 'graph TD\n'
	content = content .. 'subgraph divide\n'
	content = content .. 'a[' .. table.concat(l,' ',1,8) .. ']\n'
	content = content .. 'a --> b[' .. table.concat(l,' ',1,4) .. '] & c[' .. table.concat(l,' ',5,8) .. ']\n'
	content = content .. 'b --> d[' .. table.concat(l,' ',1,2) .. '] & e[' .. table.concat(l,' ',3,4) .. ']\n'
	content = content .. 'c --> f[' .. table.concat(l,' ',5,6) .. '] & g[' .. table.concat(l,' ',7,8) .. ']\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(l12)
	content = content .. 'subgraph conquer\n'
	content = content .. 'h --> p[' .. table.concat(l12,' ') .. ']\n'
	content = content .. 'i --> p\n'
	--j --> q["{{#af_join:{{#af_sort:{{#af_list:{{{3|58}}} | {{{4|61}}}}}}}| }}"]
	--k --> q
	--l --> r["{{#af_join:{{#af_sort:{{#af_list:{{{5|22}}} | {{{6|20}}}}}}}| }}"]
	--m --> r
	--n --> s["{{#af_join:{{#af_sort:{{#af_list:{{{7|46}}} | {{{8|75}}}}}}}| }}"]
	--o --> s
	--p --> t["{{#af_join:{{#af_sort:{{#af_list:{{{3|58}}} | {{{4|61}}} | {{{2|73}}} | {{{1|94}}}}}}}| }}"]
	--q --> t
	--r --> u["{{#af_join:{{#af_sort:{{#af_list:{{{6|20}}} | {{{5|22}}} | {{{7|46}}} | {{{8|75}}}}}}}| }}"]
	--s --> u
	--t --> v["{{#af_join:{{#af_sort:{{#af_list:{{{6|20}}} | {{{5|22}}} | {{{7|46}}} | {{{3|58}}} | {{{4|61}}} | {{{2|73}}} | {{{8|75}}} | {{{1|94}}}}}}}| }}"]
	--u --> v
	content = content .. 'end'
local graph = frame:extensionTag('mermaid', content, {})
return graph
end

return p