Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit de45f5b

Browse files
Implemented balanced brackets in Python. Closes issue #30
1 parent fd56c15 commit de45f5b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Use a dictionary to map sets of brackets to their opposites
2+
brackets = {
3+
'(': ')',
4+
'{': '}',
5+
'[': ']'
6+
}
7+
8+
# On each input string, process it using the balance checker
9+
def balancedBrackets(string):
10+
stack = []
11+
# Process every character on input
12+
for char in string:
13+
# Assign an initial value in case the stack is empty
14+
last = 0
15+
# Assign the value of the last element if stack is not empty
16+
if stack:
17+
last = stack[len(stack) - 1]
18+
if stack and last in brackets and brackets[last] == char:
19+
stack.pop()
20+
else:
21+
stack.append(char)
22+
23+
return not stack

0 commit comments

Comments
 (0)