-3

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?

Jimmy Liao
  • 71
  • 3
  • 9

1 Answers1

1

Using recursion:

def depth(arr):
    if isinstance(arr, list):
        return 1 + max(depth(elem) for elem in arr)
    else:
        return 0
Paco H.
  • 2,034
  • 7
  • 18