For example: if I have a 2d array
arr = [[1,2], [4,5], [7,5], 8]
the output should be 1, since it can be flatten once. If I have a 1d array
arr = [1,3,4]
the output should be 0 since it can't be flatten.
What function should I use?
For example: if I have a 2d array
arr = [[1,2], [4,5], [7,5], 8]
the output should be 1, since it can be flatten once. If I have a 1d array
arr = [1,3,4]
the output should be 0 since it can't be flatten.
What function should I use?
Using recursion:
def depth(arr):
if isinstance(arr, list):
return 1 + max(depth(elem) for elem in arr)
else:
return 0