Is there an easy way to determine the nesting level (without dependence on Python's recursion limit) of t
(representing recombining binomial tree)?
t = (4, (3, 5, (2, 4, 6, (1, 3, 5, 7))))
Note that without a priori knowledge of depth of t
, a routine may face recursion limit, which is set by sys.setrecursionlimit(n)
and viewed by sys.getrecursionlimit()
. Still, setting recursion limit very high before hand, may not be sufficient, thereby generating an error
`RecursionError: maximum recursion depth exceeded while calling a Python object`.
The following generates a larger (deeper) t
:
t = tuple(tuple(range(k)) for k in range(1,200))`
I guess these may work (have not worked out the details):
- one can convert
t
to string and count number of opening brackets - If flattened tuple has size $N$, then the depth has size of the positive quadratic root of $n(n+1)/2=N$, that is $n=(-1+\sqrt(1+8N))/2$
- iteratively peel (and count) the outer container until deepest nesting
- any others?
P.S. Any ideas why in-line TeX is not rendering in my question? Testing: $N$