Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class ValidatorClass:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need class keyword in a class name.

opening_parantheses_list=['(','{','[']
closing_parantheses_list=[')','}',']']

def __init__(self,expression):
self.expression=expression
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need expression at class level this way we can't easily use same instance of class for multiple validations.

self.parantheses_stack=[]

def check_balance(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think of a more descriptive name.

for char in self.expression:
if char in self.opening_parantheses_list:
self.parantheses_stack.append(char)
elif char in self.closing_parantheses_list:
index=self.closing_parantheses_list.index(char)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.parantheses_stack.pop() also returns the value, think a solution with this, it will simplify you code even more

if len(self.parantheses_stack)!=0:
if self.opening_parantheses_list[index]==self.parantheses_stack[len(self.parantheses_stack)-1]:
self.parantheses_stack.pop()
else:
return False
else:
return False
if len(self.parantheses_stack)==0:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len(self.parantheses_stack)==0 is a boolean expression we can simply return this and save the cost of this check.

return True

def main():
expression_one="(){}}"
expression_two="(){}"
validator_object_one=ValidatorClass(expression_one)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use single instance of a class for multiple validations. see comment above for reference.

validator_object_two=ValidatorClass(expression_two)
#flag=validator_object_one.check_balance()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to remove unused code before committing final changes

print("%s : %r" % (expression_one,validator_object_one.check_balance()))
print("%s : %r" % (expression_two,validator_object_two.check_balance()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an old way of formatting use format() or f strings for it


if __name__=='__main__':
main()
4 changes: 2 additions & 2 deletions paranthesis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
openingParanthesisList=['(','{','[']
closingParanthesisList=[')','}',']']
def checkBalance(expression):
openingParanthesisList=['(','{','[']
closingParanthesisList=[')','}',']']
stack=[]
for char in expression:
if char in openingParanthesisList:
Expand Down