For any ragged list its dimensions will be a list of non-negative integers defined as follows:
- Elements that are not a list will have dimensions \$\textbf{[}\,\,\textbf{]}\$.
- An empty list has dimensions \$[0]\$.
- An \$n\$ dimensional list \$L\$ of length \$x\$ has dimensions \$x : k\$ where \$k\$ is the element wise maximum of the dimensions of the elements of \$L\$ (treating missing elements as zero).
(Here, :
is "cons", the list constructor, e.g. in Lisp or Haskell)
The intuitive idea is that this is the dimensions of the smallest box which can fit the list. But this is still a little bit daunting, so let's work through some examples.
To start with, for one dimensional lists, its dimensions are always just its length.
[ 1
, 2
, 3
]
There are 3 elements here so its dimensions are \$3:k\$. To determine \$k\$ we look at the dimensions of each element. They are all integers so they have dimensions \$\textbf{[}\,\,\textbf{]}\$, and the pairwise maximum is also \$\textbf{[}\,\,\textbf{]}\$. So the dimensions are \$3:\textbf{[}\,\,\textbf{]}\$ or just \$[3]\$.
Let's do an example that is actually ragged:
[ [ 1
, 2
, 9
, 9
]
, 4
]
There are two elements, the first is one dimensional so it has dimensions \$[4]\$, the second is an integer so it has dimensions \$\textbf{[}\,\,\textbf{]}\$. Now we take the pairwise maximum. Since \$[4]\$ has more elements we treat the missing elements as zeros. The maximum is just \$[4]\$ then. The total list has length \$2\$ so the answer is \$2:[4]\$ or \$[2,4]\$.
Let's do another example:
[ [ 1
, 2
, 9
, 9
]
, [ []
, 2
]
]
The first element is the same, but the second one is different, so let's calculate its dimensions. It has two elements with dimensions \$[0]\$ and \$\textbf{[}\,\,\textbf{]}\$. The pairwise maximum is \$[0]\$ so this element has dimensions \$[2,0]\$. With this in mind now we take the pairwise maximum of \$[4]\$ and \$[2,0]\$ which gives \$[4,0]\$. Finally we add one the \$2\$ for the length of the top level and get the dimensions of \$[2,4,0]\$
Task
Given an arbitrary ragged list of positive integer give a list of non-negative integers representing its dimensions. You may assume that the input is a list.
This is code-golf so the goal is to minimize the size of your source code as measured in bytes.
Test cases
[]
=> [0]
[ 1, 2, 3]
=> [3]
[[1, 2, 9, 9], 4]
=> [2,4]
[[1, 2, 9, 9], [[], 2]]
=> [2,4,0]
[[[]],[]]
=> [2,1,0]
[[1,2,3],[[1,2,3],[[1,2,3],[[1,2,3],[1,2,3]]]]]
=> [2,3,3,3,3]
[[1, 2, 9, 9], [[], 2]]
; you'd need 2×4×1 to fit that last 2. \$\endgroup\$[[1, 2, 9, 9], [[], 2]]
into a 2×4×0 non-ragged array? \$\endgroup\$