# Determine the depth of an array

A simple challenge for your Monday evening (well, or Tuesday morning in the other half of the world...)

You're given as input a nested, potentially ragged array of positive integers:

[1, [[2, 3, [[4], 5], 6, [7, 8]], 9, [10, [[[11]]]], 12, 13], 14]


Your task is to determine its depth, which is the greatest nesting-depth of any integer in the list. In this case, the depth of 11 is 6, which is largest.

You may assume that none of the arrays will be empty.

You may write a program or function, taking input via STDIN (or closest alternative), command-line argument or function argument and outputting the result via STDOUT (or closest alternative), function return value or function (out) parameter.

Input may be taken in any convenient list or string format that supports non-rectangular arrays (with nested arrays of different depths), as long as the actual information isn't preprocessed.

You must not use any built-ins related to the shape of arrays (including built-ins that solve this challenge, that get you the dimensions of a nested array). The only exception to this is getting the length of an array.

Standard rules apply.

### Test Cases

[1]                                                               -> 1
[1, 2, 3]                                                         -> 1
[[1, 2, 3]]                                                       -> 2
[3, [3, [3], 3], 3]                                               -> 3
[[[[1], 2], [3, [4]]]]                                            -> 4
[1, [[3]], [5, 6], [[[[8]]]], 1]                                  -> 5
[1, [[2, 3, [[4], 5], 6, [7, 8]], 9, [10, [[[11]]]], 12, 13], 14] -> 6
[[[[[[[3]]]]]]]                                                   -> 7

• After discussion in chat I've decided to allow length built-ins, because some languages require them to iterate over an array cleanly. – Martin Ender Feb 8 '16 at 23:09
• Just for general education: ≡ is APL's built-in primitive for exactly this. – Adám Feb 9 '16 at 16:09
• @MartinBüttner I've run into a tiny problem. I started doing this in java, unfourtunetly when testing inputs the commas are causing it to split the inputs into multiple command line arguments rather then one. Can I use the escape character \  in the inputs? EDIT: nevermind just tried it like that. That doesn't even work either. Darn can I not use CMD args? – Ashwin Gupta Feb 10 '16 at 5:21
• @AshwinGupta can't you wrap the command line argument in quotes? You can also read input from STDIN or submit a function that takes an actual array object as a parameter. – Martin Ender Feb 10 '16 at 10:39
• @MartinBüttner oh I didn't know that quotes thing I'll try it out. Currently just using Scanner. (System.in). I believe that is a form of STDIN? – Ashwin Gupta Feb 10 '16 at 22:02

# Ruby, 42 characters

f=->a{a==a.flatten(1)?1:f[a.flatten(1)]+1}


Example:

irb(main):001:0> f.call([[[1,2],[2,3]],[[3,4],[5]]])
=> 3


• Welcome to the site! – DJMcMayhem Sep 7 '17 at 17:02

## Perl 6, 70 bytes

Whole program that reads the given examples terminated with newline or EOF from STDIN.

# perl6 % <<< '[1, [[2, 3, [[4], 5], 6, [7, 8]], 9, [10, [[[11]]]], 12, 13], 14]'

my $i;dd [max] lines.comb.map:{$i++when '[';$i--when ']'}  lines takes text line by line from STDIN and returns a list of Str. Calling .comb on a list will convert that into a Str and then split it into graphenes. Any block in Perl 6 is born with one positional argument that ends up in $_. map called with any callable expect it to have one positional argument. What is nice because when ']' is short for when \$_ ~~ ']'. The returned list is fed to [max] and reduced to it's biggest value.

## C#, 99 Bytes

int f(string s){int m=0,w=0;foreach(char c in s){if(c=='[')w++;if(c==']')w--;if(w>m)m=w;}return m;}

• Does int m=w=0 work in C# ? – Cyoce Oct 7 '16 at 0:40

# Axiom 106 bytes

RL(a:Union(List(Any),Any)):INT==(a case List(Any)=>(g:List(Any):=a;r:=1;for i in g repeat r:=r+RL(i);r);0)


ungolf

RL(a:Union(List(Any),Any)):INT==
a case List(Any)=>
g:List(Any):=a
r:=1
for i in g repeat r:=r+RL(i)
r
0


results

(3) -> for i in[[1],[1,2,3], [[1,2,3]], [3,[3,[3],3],3]]repeat output[i, RL(i)]
[[1],1]
[[1,2,3],1]
[[[1,2,3]],2]
[[3,[3,[3],3],3],3]


# C++14, 7270 67 bytes

-2 bytes for removing the curly brackets at for and -3 bytes for replacing m=l>m?l:m with m+=l>m.

As unnamed lambda returning via reference parameter:

[](auto&s,int&m){int l=m=0;for(auto c:s)l+=(c==91)-(c==93),m+=l>m;}


s can be std::string or char[]

Ungolfed and usage:

#include<string>
#include<iostream>

auto f=
[](auto&s,int&m){
int l=m=0;
for(auto c:s)
l+=(c==91)-(c==93),
m+=l>m;
}
;

int main(){
std::string S[] = {
"[1]",
"[1, 2, 3]",
"[[1, 2, 3]]",
"[3, [3, [3], 3], 3]",
"[[[[1], 2], [3, [4]]]]",
"[1, [[3]], [5, 6], [[[[8]]]], 1]",
"[1, [[2, 3, [[4], 5], 6, [7, 8]], 9, [10, [[[11]]]], 12, 13], 14]",
"[[[[[[[3]]]]]]]"
};

for (auto s:S){
int m;
g(s,m);
std::cout << m << std::endl;
}

}


Clojure, 57 bytes

(fn[s](apply max(reductions +(map #({$1$ -1}% 0)s))))


Also based on parsing a string:

(f "[3, [3, [3], 3], 3]")


R: 53 bytes.

d=function(L) ifelse(is.list(L),max(sapply(L,d))+1,0)


Recursive function, it adds one point everytime it finds an inner list. It chooses only the element with the maximum depth...