-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20200706_balanced_brackets.py
58 lines (46 loc) · 1.47 KB
/
20200706_balanced_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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'''
2020-07-06
[from dailycodingproblem.com #27]
Given a string of round, curly, and square open and closing brackets, return
whether the brackets are balanced (well-formed).
For example, given the string "([])[]({})", you should return true.
Given the string "([)]" or "((()", you should return false.
'''
def balanced(string):
# start with placeholder to avoid index error / clumsy empty list checking
ledger = [' ']
for s in string:
last_open_bracket = ledger[-1]
if s not in '({[]})':
pass
if s in '({[':
if last_open_bracket not in ' ({[':
return False
ledger.append(s)
elif s in ')}]':
if (
(s == ')' and last_open_bracket == '(') or
(s == '}' and last_open_bracket == '{') or
(s == ']' and last_open_bracket == '[')
):
ledger.pop()
else:
return False
if ledger[-1] != ' ':
return False
return True
'''
# TESTS (must all return True)
# Base cases from question
balanced("([])[]({})") == True
balanced("([)]") == False
balanced("((()") == False
balanced("((((((((((asdf))))))))))") == True
balanced("((((((((((asdf)))))))))))))") == False
# No brackets should still return True
balanced("asdf") == True
# Starting with a closing bracket should return False
balanced("]asdf[") == False
balanced("))asdf((") == False
balanced("}}}asdf") == False
'''