0

Explain plz

This code checks whether a series of parentheses are in correct order or in pairs. But i didnt understand the code. Can you explain the loop part in the function def is_balanced(input_str): s = list() for ch in input_str: if ch == '(': s.append(ch) if ch == ')': if not s: return False s.pop() return not s if __name__=="__main__": input_str = input() if is_balanced(input_str): print(input_str, "is balanced") else: print(input_str, "is not balanced")

27th Mar 2021, 12:56 PM
Swapnil
Swapnil - avatar
1 ответ
+ 2
Ex: 1 ()(()) Appended ( pop ( Append ( Append ( pop ( pop ( list it empty so returns not false i.e true Ex: 2 ())( Append ( pop ( now its ) so if not s: since now list is empty and condition true that means ) encountered before ( so wrong ordered should return false so it return false now.. hope this examples clears it....
27th Mar 2021, 1:02 PM
Jayakrishna 🇮🇳