Talk:Tree from nesting levels: Difference between revisions

just an abstract idea
(just an abstract idea)
Line 26:
 
::It is going to have to change anyway, languages needs an apostrophe. The point is that all will be better understood when the operations for which this data structure are useful is explained.--[[User:Nigel Galloway|Nigel Galloway]] ([[User talk:Nigel Galloway|talk]]) 16:15, 2 February 2021 (UTC)
 
:::Probably a little too abstract, but I thought of flatten() getting the input back, and also whipped up a quick "collect item*level" traversal:
<pre style="font-size: 12px; float:left">
function square(sequence s, integer level=1)
integer res = 0
for i=1 to length(s) do
res += iff(atom(s[i])?s[i]*level:square(s[i],level+1))
end for
return res
end function
</pre>
<pre style="font-size: 12px; float:left">
?{ti,flatten(res,{}),square(res),sum(sq_mul(ti,ti))}
-- results:
{{},{},0,0}
{{1,2,4},{1,2,4},21,21}
{{3,1,3,1},{3,1,3,1},20,20}
{{1,2,3,1},{1,2,3,1},15,15}
{{3,2,1,3},{3,2,1,3},23,23}
{{3,3,3,1,1,3,3,3},{3,3,3,1,1,3,3,3},56,56}
</pre>
<div style="clear:both"></div>
:::(The tree traversal in no way has to be recursive, of course) --[[User:Petelomax|Pete Lomax]] ([[User talk:Petelomax|talk]]) 22:49, 2 February 2021 (UTC)
7,794

edits