check balanced parenthesis in python
up vote
0
down vote
favorite
I want to make a code that can match parenthesis.
Assumptions are that parenthesis is given by string, and the input is given by list.
So below is my code:
I got the error that:
'AttributeError: 'list' object has no attribute 'parenthesis''
Please let me know what's wrong with my code.
def push(item):
stack.append(item)
def peek():
if len(stack) != 0:
return stack[-1]
def pop(): #delete step
if len(stack) != 0:
item = stack.pop(-1)
return item
def check(item):
global stack1
stack1 =
global stack2
stack2 =
if item == '{':
stack1.push('1')
elif item == '}':
stack1.pop()
elif item == '(':
stack2.push('2')
elif item == ')':
stack2.pop()
def parenthesis(self):
for i in len(self):
tail = self.peek
tail.check
self.pop()
if len(stack1) == 0 & len(stack2) ==0 :
print('pass')
else:
print('fail')
stack =
push('{')
push('}')
stack.parenthesis()
python xcode list stack parentheses
New contributor
|
show 1 more comment
up vote
0
down vote
favorite
I want to make a code that can match parenthesis.
Assumptions are that parenthesis is given by string, and the input is given by list.
So below is my code:
I got the error that:
'AttributeError: 'list' object has no attribute 'parenthesis''
Please let me know what's wrong with my code.
def push(item):
stack.append(item)
def peek():
if len(stack) != 0:
return stack[-1]
def pop(): #delete step
if len(stack) != 0:
item = stack.pop(-1)
return item
def check(item):
global stack1
stack1 =
global stack2
stack2 =
if item == '{':
stack1.push('1')
elif item == '}':
stack1.pop()
elif item == '(':
stack2.push('2')
elif item == ')':
stack2.pop()
def parenthesis(self):
for i in len(self):
tail = self.peek
tail.check
self.pop()
if len(stack1) == 0 & len(stack2) ==0 :
print('pass')
else:
print('fail')
stack =
push('{')
push('}')
stack.parenthesis()
python xcode list stack parentheses
New contributor
Try to implement stack in line with OOP paradigm and avoid using global variables. For example you may create a Stack class
– artona
Nov 19 at 11:47
thank you artona!! and i'll try Stack class. Can i ask one question that why do you recommend to avoid using global variables? they have some disadvantages?
– Heewon
Nov 19 at 11:50
Making long story short: they are dangerous because you can forget about them when developing your program. Here is the better thread on this: stackoverflow.com/questions/423379/…
– artona
Nov 19 at 11:53
artona thank you . have a nice day :)
– Heewon
Nov 19 at 11:55
it looks like your code was copied from a source that originally had a stack class, i suggest you look into the original code
– AntiMatterDynamite
Nov 19 at 11:57
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to make a code that can match parenthesis.
Assumptions are that parenthesis is given by string, and the input is given by list.
So below is my code:
I got the error that:
'AttributeError: 'list' object has no attribute 'parenthesis''
Please let me know what's wrong with my code.
def push(item):
stack.append(item)
def peek():
if len(stack) != 0:
return stack[-1]
def pop(): #delete step
if len(stack) != 0:
item = stack.pop(-1)
return item
def check(item):
global stack1
stack1 =
global stack2
stack2 =
if item == '{':
stack1.push('1')
elif item == '}':
stack1.pop()
elif item == '(':
stack2.push('2')
elif item == ')':
stack2.pop()
def parenthesis(self):
for i in len(self):
tail = self.peek
tail.check
self.pop()
if len(stack1) == 0 & len(stack2) ==0 :
print('pass')
else:
print('fail')
stack =
push('{')
push('}')
stack.parenthesis()
python xcode list stack parentheses
New contributor
I want to make a code that can match parenthesis.
Assumptions are that parenthesis is given by string, and the input is given by list.
So below is my code:
I got the error that:
'AttributeError: 'list' object has no attribute 'parenthesis''
Please let me know what's wrong with my code.
def push(item):
stack.append(item)
def peek():
if len(stack) != 0:
return stack[-1]
def pop(): #delete step
if len(stack) != 0:
item = stack.pop(-1)
return item
def check(item):
global stack1
stack1 =
global stack2
stack2 =
if item == '{':
stack1.push('1')
elif item == '}':
stack1.pop()
elif item == '(':
stack2.push('2')
elif item == ')':
stack2.pop()
def parenthesis(self):
for i in len(self):
tail = self.peek
tail.check
self.pop()
if len(stack1) == 0 & len(stack2) ==0 :
print('pass')
else:
print('fail')
stack =
push('{')
push('}')
stack.parenthesis()
python xcode list stack parentheses
python xcode list stack parentheses
New contributor
New contributor
edited Nov 19 at 13:23
user10638812
New contributor
asked Nov 19 at 11:43
Heewon
1
1
New contributor
New contributor
Try to implement stack in line with OOP paradigm and avoid using global variables. For example you may create a Stack class
– artona
Nov 19 at 11:47
thank you artona!! and i'll try Stack class. Can i ask one question that why do you recommend to avoid using global variables? they have some disadvantages?
– Heewon
Nov 19 at 11:50
Making long story short: they are dangerous because you can forget about them when developing your program. Here is the better thread on this: stackoverflow.com/questions/423379/…
– artona
Nov 19 at 11:53
artona thank you . have a nice day :)
– Heewon
Nov 19 at 11:55
it looks like your code was copied from a source that originally had a stack class, i suggest you look into the original code
– AntiMatterDynamite
Nov 19 at 11:57
|
show 1 more comment
Try to implement stack in line with OOP paradigm and avoid using global variables. For example you may create a Stack class
– artona
Nov 19 at 11:47
thank you artona!! and i'll try Stack class. Can i ask one question that why do you recommend to avoid using global variables? they have some disadvantages?
– Heewon
Nov 19 at 11:50
Making long story short: they are dangerous because you can forget about them when developing your program. Here is the better thread on this: stackoverflow.com/questions/423379/…
– artona
Nov 19 at 11:53
artona thank you . have a nice day :)
– Heewon
Nov 19 at 11:55
it looks like your code was copied from a source that originally had a stack class, i suggest you look into the original code
– AntiMatterDynamite
Nov 19 at 11:57
Try to implement stack in line with OOP paradigm and avoid using global variables. For example you may create a Stack class
– artona
Nov 19 at 11:47
Try to implement stack in line with OOP paradigm and avoid using global variables. For example you may create a Stack class
– artona
Nov 19 at 11:47
thank you artona!! and i'll try Stack class. Can i ask one question that why do you recommend to avoid using global variables? they have some disadvantages?
– Heewon
Nov 19 at 11:50
thank you artona!! and i'll try Stack class. Can i ask one question that why do you recommend to avoid using global variables? they have some disadvantages?
– Heewon
Nov 19 at 11:50
Making long story short: they are dangerous because you can forget about them when developing your program. Here is the better thread on this: stackoverflow.com/questions/423379/…
– artona
Nov 19 at 11:53
Making long story short: they are dangerous because you can forget about them when developing your program. Here is the better thread on this: stackoverflow.com/questions/423379/…
– artona
Nov 19 at 11:53
artona thank you . have a nice day :)
– Heewon
Nov 19 at 11:55
artona thank you . have a nice day :)
– Heewon
Nov 19 at 11:55
it looks like your code was copied from a source that originally had a stack class, i suggest you look into the original code
– AntiMatterDynamite
Nov 19 at 11:57
it looks like your code was copied from a source that originally had a stack class, i suggest you look into the original code
– AntiMatterDynamite
Nov 19 at 11:57
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Heewon is a new contributor. Be nice, and check out our Code of Conduct.
Heewon is a new contributor. Be nice, and check out our Code of Conduct.
Heewon is a new contributor. Be nice, and check out our Code of Conduct.
Heewon is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373921%2fcheck-balanced-parenthesis-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try to implement stack in line with OOP paradigm and avoid using global variables. For example you may create a Stack class
– artona
Nov 19 at 11:47
thank you artona!! and i'll try Stack class. Can i ask one question that why do you recommend to avoid using global variables? they have some disadvantages?
– Heewon
Nov 19 at 11:50
Making long story short: they are dangerous because you can forget about them when developing your program. Here is the better thread on this: stackoverflow.com/questions/423379/…
– artona
Nov 19 at 11:53
artona thank you . have a nice day :)
– Heewon
Nov 19 at 11:55
it looks like your code was copied from a source that originally had a stack class, i suggest you look into the original code
– AntiMatterDynamite
Nov 19 at 11:57