-
Notifications
You must be signed in to change notification settings - Fork 0
/
7.1.Brackets.py
39 lines (31 loc) · 899 Bytes
/
7.1.Brackets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
7.1.Brackets
Determine whether a given string of parentheses is properly nested.
"""
import unittest
def solution(S):
# write your code in Python 2.7
stack = []
for s in S:
if s == '{' or s == '[' or s == '(':
stack.append(s)
else:
if len(stack) == 0:
return 0
l = stack.pop()
if s == '}' and l != '{':
return 0
elif s == ']' and l != '[':
return 0
elif s == ')' and l != '(':
return 0
if len(stack) == 0:
return 1
else:
return 0
class TestSolution(unittest.TestCase):
def test_solution(self):
self.assertEqual(solution("{[()()]}"), 1)
self.assertEqual(solution("([)()]"), 0)
if __name__ == '__main__':
unittest.main(exit=False)