MCPcopy Index your code
hub / github.com/geekcomputers/Python / is_balanced

Function is_balanced

balance_parenthesis.py:32–52  ·  view source on GitHub ↗
(check_string)

Source from the content-addressed store, hash-verified

30
31
32def is_balanced(check_string):
33 s = Stack()
34 index = 0
35 is_bal = True
36 while index < len(check_string) and is_bal:
37 paren = check_string[index]
38 if paren in "{[(":
39 s.push(paren)
40 else:
41 if s.is_empty():
42 is_bal = False
43 else:
44 top = s.pop()
45 if not is_same(top, paren):
46 is_bal = False
47 index += 1
48
49 if s.is_empty() and is_bal:
50 return True
51 else:
52 return False
53
54
55print(is_balanced("[((())})]"))

Callers 1

Calls 5

pushMethod · 0.95
is_emptyMethod · 0.95
popMethod · 0.95
StackClass · 0.85
is_sameFunction · 0.85

Tested by

no test coverage detected