title: Finding A Matching Bracket, Python
body: <h3>The Problem:</h3>
  
  <p>I am attempting to write some python code that searches through a list and returns the index of a matching bracket. For example:</p>
  
  <pre><code>array = ["(","foo",")","(","bar","(",")",")"]
  f(0) =&gt; 2
  f(1) =&gt; ERROR: Not a bracket.
  f(2) =&gt; 0
  f(3) =&gt; 7
  </code></pre>
  
  <h3>My Feeble Attempts:</h3>
  
  <p>I tried looping through the list and finding the closest bracket, but then I realised it didn't work when you had loops inside loops (loopception). I have also tried adding a counter that adds one to the counter if it's a new bracket <code>(</code> and takes one if it's a close bracket <code>)</code>, then checks to see if it equals <code>-1</code>, but that doesn't work.</p>
  
  <p>Previous Code:</p>
  
  <pre><code>while True:
              if(count == -1):
                  iterator = j+1
                  break
              else:
                  j += 1
                  print j
                  if(commands[j] == "("):
                      count += 1
                  if(commands[j] == ")"):
                      count -= 1
  </code></pre>
  
  <p>Where iterator is the input and commands is the array</p>