This is not a duplicate of this challenge.
Here by an array, I mean a nested list that is "not ragged", i.e., it is either a list of elements, or a list of arrays of the same shape. For example, this is an array of shape (2,3)
:
[[1, 2, 3], [4, 5, 6]]
The depth (or rank) of an array is the length of its shape. The depth of the array above is 2
.
But we can see every ragged-list as an array, where every element of the array is a either an atom (e.g., an integer) or a ragged-list. For example, the following ragged-list can be seen as an array of shape (2,3)
:
[[1, 2, [3]], [4, [5], [6, [7]]]]
The six elements of this array are 1
, 2
, [3]
, 4
, [5]
, [6, [7]]
. As an array, its depth is 2
.
So we can define the array depth of a ragged list to be its maximum depth as an array.
Or more formally:
- If a ragged list has length \$a\$, we say it is an array of shape \$(a)\$.
- If every element of a ragged list is an array of shape \$(a_1,\dots,a_n)\$, and the length of the ragged list is \$a_0\$, then we say it is an array of shape \$(a_0,a_1,\dots,a_n)\$. (So it is also an array of shape \$(a_0,a_1,\dots,a_{n-1})\$.)
- The array depth of a ragged list is the maximum length of its shape when we see it as an array.
Task
Given a non-empty ragged list of positive integers, output its array depth. You may assume that the input does not contain any empty list.
This is code-golf, so the shortest code in bytes wins.
Testcases
[1] -> 1
[1, 2, 3] -> 1
[[1, 2, 3]] -> 2
[3, [3, [3], 3], 3] -> 1
[[[[1], 2], [3, [4]]]] -> 3
[[1, 2, 3], [4, 5, 6]] -> 2
[[1, 2, [3]], [4, [5], [6, [7]]]] -> 2
[[1, 2], [3, 4, 5], [6, 7, 8, 9]] -> 1
[[[1, 2]], [[3, 4], [5, 6]]] -> 1
[[1, [2]], [[[3]], [[[4]]]]] -> 2
[[[1], [2]], [[3, 4], [5, 6]]] -> 2
[[[[[[[3]]]]]]] -> 7
[3, [3, [3], 3], 3]
is 1, rather than 2 or 3, might also be good. \$\endgroup\$[[[1], [2]], [[3, 4], [5, 6]]] -> 2
. \$\endgroup\$[[], [], []]
. If it can, what is expected output for this testcase? \$\endgroup\$