-
Notifications
You must be signed in to change notification settings - Fork 0
/
parentheses_checker1.py
61 lines (53 loc) · 2.47 KB
/
parentheses_checker1.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
59
60
61
import stack
#########################################################
# #
# Program to check for balanced parentheses in file #
# passed as parameter. #
# #
# __Time complexity: O(n) #
# __Space complexity: O(n/2) #
# __*where n is the length of file(str) #
# #
# Time of a python program execution for files used #
# as example: #
# #
# stack.py --- 0.002000093460083008 seconds ---#
# bad_stack1.py --- 0.0009999275207519531 seconds --#
# bad_stack2.py --- 0.021001338958740234 seconds ---#
# bad_stack3.py --- 0.021001338958740234 seconds ---#
# bad_stack4.py --- 0.0030002593994140625 seconds --#
# #
#########################################################
def parentheses_checker1(file):
"""
Returns the phrase 'Well parenthesed' if each open parenthesis has
the matching close one.
In the opposite case, returns the phrase 'Bad parenthesed'.
:param file: file ( .txt, .py, etc.)
:return: 'Well parenthesed'/'Bad parenthesed'
:rtype: str
:UC: file is an external file
:example:
>>> parentheses_checker1('stack.py')
'Well parenthesed'
>>> parentheses_checker1('bad_stack1.py')
'Bad parenthesed'
>>> parentheses_checker1('bad_stack3.py')
'Bad parenthesed'
"""
with open(file) as f:
strInput = f.read()
if strInput:
brackets = [ ('(',')'), ('[',']'), ('{','}')]
kStart = 0
kEnd = 1
stck=stack.create()
for char in strInput:
for bracketPair in brackets:
if char == bracketPair[kStart]:
stck.append(char)
elif char == bracketPair[kEnd] and len(stck) > 0 and stck.pop() != bracketPair[kStart]:
return 'Bad parenthesed'
if len(stck) == 0:
return 'Well parenthesed'
return 'Bad parenthesed'