title: How change the cycle for getting correct answers?
body: <p>I have a task to create a function that reverse any string character inside the regular bracket sequence, starting from the innermost pair. The string sequence can have, spaces, punctuation marks, letters and brakets. So the result should be sting.</p>
  
  <p>Example</p>
  
  <p>For string </p>
  
  <pre><code>s = "a(bc)de"
  </code></pre>
  
  <p>the output should be</p>
  
  <pre><code>reverseParentheses(s) = "acbde".
  </code></pre>
  
  <p>I have wrote the following code to solve this problem:</p>
  
  <pre><code>s_i = s   
  for i in range(s.count('(')):
      # reverse letters inside parenthesis
      s_i = s_i.replace(s_i[s_i.rindex('(')+1:s_i.index(')')], s_i[s_i.rindex('(')+1:s_i.index(')')][::-1])
      # delete outward parenthesis
      s_i =s_i[:s_i.rindex('(')] + s_i[s_i.rindex('(')+1:]
      # delete inward parenthesis
      s_i =s_i[:s_i.index(')')] + s_i[s_i.index(')')+1:]
      i += 1
  print(s_i)
  </code></pre>
  
  <p>However, I become false results for following strings:</p>
  
  <pre><code>s = "abc(cba)ab(bac)c"
  </code></pre>
  
  <p>It should be  </p>
  
  <pre><code>abcabcabcabc
  </code></pre>
  
  <p>I get</p>
  
  <pre><code>abccabbaabcc
  </code></pre>
  
  <p>And </p>
  
  <pre><code>s = "The ((quick (brown) (fox) jumps over the lazy) dog)"
  </code></pre>
  
  <p>It should be like this:</p>
  
  <pre><code>The god quick nworb xof jumps over the lazy
  </code></pre>
  
  <p>But I get only:</p>
  
  <pre><code>The god quick xof nworb jumps over the lazy
  </code></pre>
  
  <p>How should I correct or adjust my code for becoming right results for last two examples?</p>
  
  <p><strong>Code Adjustment</strong></p>
  
  <p>I have tried to take into consideration answers and hints but I could not use recursion. I adressed the problem with the parantacies when there are just two of them located as: <code>"..(...) (...).., .."</code></p>
  
  <p>So I made the following code:</p>
  
  <pre><code>def reverse(s):
  #ensure parens are in pairs
  if '(' not in s and ')' not in s:
      while '(' in s:
              s = s.replace(s[s.rindex('(')+1:s.index(')')], s[s.rindex('(')+1:s.index(')')][::-1])
              s = s[:s.rindex('(')] + s[s.rindex('(')+1:]
              s = s[:s.index(')')] + s[s.index(')')+1:]
      return s
  else:
      if (s[s.index(')'):s.rindex('(')+1] == ''):
          while '(' in s:
                  s = s.replace(s[s.rindex('(')+1:s.index(')')], s[s.rindex('(')+1:s.index(')')][::-1])
                  s = s[:s.rindex('(')] + s[s.rindex('(')+1:]
                  s = s[:s.index(')')] + s[s.index(')')+1:]
          return s
      elif (s[s.index(')'):s.rindex('(')+1] != ''):
          betw = s[s.index(')')+1:s.rindex('(')]
          part1 = s[:s.index(')')+1]
          part2 = s[s.rindex('('):]
          part1 = part1.replace(part1[part1.rindex('(')+1:part1.index(')')], part1[part1.rindex('(')+1:part1.index(')')][::-1])
          part1 = part1[:part1.rindex(')')]
          part2 = part2.replace(part2[part2.rindex('(')+1:part2.index(')')], part2[part2.rindex('(')+1:part2.index(')')][::-1])
          part2 = part2[part2.rindex('(')+1:]
          s = part1+betw+part2
          s = s[:s.rindex('(')] + s[s.rindex('(')+1:]
          s = s[:s.index(')')] + s[s.index(')')+1:]
          while '(' in s:
              s = s.replace(s[s.rindex('(')+1:s.index(')')], s[s.rindex('(')+1:s.index(')')][::-1])
              s = s[:s.rindex('(')] + s[s.rindex('(')+1:]
              s = s[:s.index(')')] + s[s.index(')')+1:]
          return s
      else:
          while '(' in s:
              s = s.replace(s[s.rindex('(')+1:s.index(')')], s[s.rindex('(')+1:s.index(')')][::-1])
              s = s[:s.rindex('(')] + s[s.rindex('(')+1:]
              s = s[:s.index(')')] + s[s.index(')')+1:]
          return s
  </code></pre>
  
  <p>However, I think that it can not perform well for the following example:</p>
  
  <pre><code>s = "abc(147)ab(123)c(12)asd"
  </code></pre>
  
  <p>The answer should be: <code>"abc741ab321c21asd"</code> but I get <code>"abc12c321ba147asd"</code></p>
  
  <p>What should be changed in order to get the correct answer?</p>