Note that there are some explanatory texts on larger screens.

plurals
  1. POpop() empty deque() in logic
    text
    copied!<p>I want to be able to validate parenthesis so they enclose and ignore any type of characters. As long as there is the valid use of enclosure of strings with parenthesis then <code>True</code> else `False.</p> <p>I am still new to python so I'm not sure how to properly create an if statement for this certain condition. I am trying to create an fi statement such that when I <code>.pop()</code> an empty <code>deque()</code> I will be able to <code>return False</code> instead of receiving the error message:</p> <pre><code>Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; IndexError: pop from an empty deque </code></pre> <p>Perhaps there is another better method around solving this problem. If so I would be glad to see how someone else would solve it</p> <p>For example:</p> <pre><code>a = 'sdf(sadf(sdf)sdf)sdfsd0sdf)sdf(sdf0)' # false b = 'dsf))))(((((dsfsdf' # false c = '()()()()' # true d = '((((asd(asd)asd)()()asd))' # true </code></pre> <p>my code:</p> <pre><code># any letter is ignored # jsut make sure that the parenthesis are equal from collections import * def str_valid(stringy): param_stack = deque() for n in stringy: if n ==')': param_stack.pop() if n == '(': param_stack.append('(') if param_stack == []: return True else: return False a = 'sdf(sadf(sdf)sdf)sdfsd0sdf)sdf(sdf0)' # false b = 'dsf))))(((((dsfsdf' # false c = '()()()()' # true d = '((((asd(asd)asd)()()asd))' # true print str_valid(a) print str_valid(b) print str_valid(c) print str_valid(d) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload