Conversation
marslan02
left a comment
There was a problem hiding this comment.
👍 for the use of stack logic.
Your logic is good I mostly added language related comments.
Also repos are public so its better to add all of this code in a proper repository instead of hello-world and file names are also important.
class.py
Outdated
| closing_parantheses_list=[')','}',']'] | ||
|
|
||
| def __init__(self,expression): | ||
| self.expression=expression |
There was a problem hiding this comment.
We don't need expression at class level this way we can't easily use same instance of class for multiple validations.
class.py
Outdated
| self.expression=expression | ||
| self.parantheses_stack=[] | ||
|
|
||
| def check_balance(self): |
There was a problem hiding this comment.
Think of a more descriptive name.
class.py
Outdated
| @@ -0,0 +1,35 @@ | |||
| class ValidatorClass: | |||
There was a problem hiding this comment.
we don't need class keyword in a class name.
class.py
Outdated
| 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) |
There was a problem hiding this comment.
self.parantheses_stack.pop() also returns the value, think a solution with this, it will simplify you code even more
class.py
Outdated
| return False | ||
| else: | ||
| return False | ||
| if len(self.parantheses_stack)==0: |
There was a problem hiding this comment.
len(self.parantheses_stack)==0 is a boolean expression we can simply return this and save the cost of this check.
class.py
Outdated
| expression_two="(){}" | ||
| validator_object_one=ValidatorClass(expression_one) | ||
| validator_object_two=ValidatorClass(expression_two) | ||
| #flag=validator_object_one.check_balance() |
There was a problem hiding this comment.
Try to remove unused code before committing final changes
class.py
Outdated
| def main(): | ||
| expression_one="(){}}" | ||
| expression_two="(){}" | ||
| validator_object_one=ValidatorClass(expression_one) |
There was a problem hiding this comment.
Use single instance of a class for multiple validations. see comment above for reference.
class.py
Outdated
| validator_object_two=ValidatorClass(expression_two) | ||
| #flag=validator_object_one.check_balance() | ||
| print("%s : %r" % (expression_one,validator_object_one.check_balance())) | ||
| print("%s : %r" % (expression_two,validator_object_two.check_balance())) |
There was a problem hiding this comment.
this is an old way of formatting use format() or f strings for it
No description provided.