Your challenge is simple, calculate the depth of each matching brackets in the given input e.g. (()()(()))->[[1,1,[1]]], and output it in the form of a list/array/string representation


For every bracket in the input, you have to count how many nested brackets there are for that part of the input e.g. () has 1 depth

Although if find a bracket of depth 2 e.g. (()), a list [] is formed from the 1st bracket ( ) has to be added to the output with the 2nd bracket, () of depth 1 inside that list

Test cases:

() -> [1]
[[][]] -> [[1,1]]
{}{}{{}}{{{}}} -> [1,1,[1],[[1]]


You may assume that there are only lists and 1's will be in the required output

For the output, you are allowed to output the depth as a list/array, or as a string representation e.g. 1 1 [1] for ()()(())

You can output it in other ways, but the depth of the brackets must be distinguishable from each other.

Your answer should be able to handle one type of bracket (one of (), [], or {}), at your choice.

You may assume that input is balanced and only uses the type of bracket you chose.

  • 7
    \$\begingroup\$ "You may assume only parentheses will appear in the input"... "you may assume that other types of brackets are allowed". Does this mean that any solution only needs to accept one variety of bracket, but is free to choose which? Also, how does indexing figure into this? The challenge appears to just be replace immediate pairs of brackets with 1, then add some (unspecified?) separators. \$\endgroup\$ Feb 5, 2022 at 5:07
  • \$\begingroup\$ added the explanation and changed accordingly \$\endgroup\$
    – DialFrost
    Feb 5, 2022 at 5:30
  • \$\begingroup\$ If I output as a string, must I have a separator between elements (i.e. can I output "11[1]" instead of "1 1 [1]"?) \$\endgroup\$
    – hakr14
    Feb 5, 2022 at 8:49
  • \$\begingroup\$ u must have a separator between elements its supposed to be distuingisible. list is best but if u have to use spaces \$\endgroup\$
    – DialFrost
    Feb 5, 2022 at 8:50
  • \$\begingroup\$ Are [] () {} all the valid brackets, or might there be others (i.e. <>)? Can we assume the input will be balanced? \$\endgroup\$
    – hakr14
    Feb 5, 2022 at 8:51

1 Answer 1


Python, 236 155 bytes

def r(o):
 for c in o:
  if c=="(":
   if s<1:a=n+1
   if a==n:v+="1,"
   elif s<1:v+=r(o[a:n])
 return v+"]"

Is this a good solution? No. But I don't think there'll be another answer here soon, and I'm very new to codegolf so I thought I might try my luck here :p

Attmpt this online!

  • 2
    \$\begingroup\$ Welcome to Code Golf, and nice first answer! Be sure to check out our Tips for Golfing in Python page for ways you may be able to golf your code! For example, you can remove many of the spaces: ato.pxeger.com/… \$\endgroup\$
    – pxeger
    Jun 4, 2022 at 8:05
  • \$\begingroup\$ 138 \$\endgroup\$
    – naffetS
    Jun 4, 2022 at 19:55
  • \$\begingroup\$ if it's allowed to use generators of generators, 131 \$\endgroup\$
    – naffetS
    Jun 4, 2022 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.