Skip to content
This repository has been archived by the owner on Oct 23, 2018. It is now read-only.

Commit

Permalink
Merge pull request #1 from doms/balanced-brackets
Browse files Browse the repository at this point in the history
implement balanced brackets in python
  • Loading branch information
doms authored Oct 5, 2018
2 parents 9c024de + 6e408fa commit c83cbff
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Python/balanced-brackets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
PAIRINGS = {
'(': ')',
'{': '}',
'[': ']'
}


def is_balanced(symbols):
stack = []
for s in symbols:
if s in PAIRINGS.keys():
stack.append(s)
else:
try:
expected_opening_symbol = stack.pop()
except IndexError: # too many closing symbols
return False
if s != PAIRINGS[expected_opening_symbol]: # mismatch
return False
return len(stack) == 0 # false if too many opening symbols

0 comments on commit c83cbff

Please sign in to comment.