RosettaCodeData/Task/Tree-from-nesting-levels/J/tree-from-nesting-levels-2.j
2023-07-01 13:44:08 -04:00

13 lines
542 B
Text

NB. first we nest each integer to the required depth, independently
NB. then we recursively merge deep boxes
NB. for consistency, if there are no integers, we box that empty list
dtree=: {{
<^:(0=L.) merge <^:]each y
}}
merge=: {{
if.(0=#$y)+.2>L.y do.y return.end. NB. done if no deep boxes left
shallow=. 2 > L."0 y NB. locate shallow boxes
group=. shallow} (+/\ shallow),:-#\y NB. find groups of adjacent deep boxes
merge each group ,each//. y NB. combine them and recursively merge their contents
}}