-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstacks.py
More file actions
38 lines (35 loc) · 1.17 KB
/
stacks.py
File metadata and controls
38 lines (35 loc) · 1.17 KB
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
## Stack: LIFO (last-in-first-out)
## Python List (with append and pop) is a natrual implementation of this
## In essence, stack specifies MATCHING INFORMATION in the sequence
## Language Recognition Problem
## the language model accepts the following sequneces:
## S -> empty
## S -> S S
## S -> [ S ]
## S -> ( S )
## It is context-free language but not a regular expression
## e.g., accepted sequences "()", "[[]]", "[()[]()] []"
## unaccepted sequences "]", ")(", "(]", "([)]"
## Inspiration: Think about the onion layer model as discussed
## in ../sorting/sort_application.py
def language_accept(text):
s = []
text = text.replace(' ','')
for c in text:
if c in ('(', '['):
s.append(c)
elif c in (')', ']'):
cc = s.pop() if s else ''
if cc+c not in ('()', '[]'):
return False
else:
return False
return False if len(s) > 0 else True
## tests
if __name__ == '__main__':
## test language_accept
for t in ["()", "[[]]", "[()[]()] []"]:
assert language_accept(t)
for t in ["]", ")(", "(]", "([)]"]:
assert not language_accept(t)
print 'all test pass'