-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Created Dijkstra's Two Stack Algorithm #2321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
testing purposes.
testing purposes.
""" Stack Data Structure Used for the Algorithm""" | ||
def __init__(self): | ||
self.__array = [] | ||
self.__len = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let’s do as __len__()
self.__len = 0 | ||
self.underflow_error = False | ||
|
||
def is_empty(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don’t need this method. if stack:
is sufficient.
|
||
def push(self, x): | ||
self.__array.append(x) | ||
self.__len += 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don’t to need to do this busywork if __len__()
returns len(self.array)
.
self.__len += 1 | ||
|
||
def pop(self): | ||
if not self.is_empty(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if not self.is_empty(): | |
if not self.array: | |
raise IndexError("pop from empty list") |
just like [].pop()
def peek(self): | ||
return self.__array[-1] if not self.is_empty() else None | ||
|
||
def length(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not in Python!
return self.__len | ||
|
||
def print_stack(self): | ||
print(self.__array) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define as __str__()
pass | ||
|
||
# RULE 5 | ||
result = operand_stack.peek() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result = operand_stack.peek() | |
return operand_stack.peek() |
def main(): | ||
equation = "(5 + ((4 * 2) * (2 + 3)))" | ||
# answer = 45 | ||
answer = dijkstras_two_stack_algorithm(equation) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
answer = dijkstras_two_stack_algorithm(equation) | |
print(f"{dijkstras_two_stack_algorithm(equation) = }") |
print(self.__array) | ||
|
||
|
||
def dijkstras_two_stack_algorithm(equation): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type hints?
Changed a few minor things.
Changed a few minor things.
Co-authored-by: Christian Clauss <cclauss@me.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
* created dijkstra's two stack algorithm * Made changes to dijkstras two stack algorithm for documentation and testing purposes. * Made changes to dijkstras two stack algorithm for documentation and testing purposes. * Fixed Grammar Mistake * Added Explanation Reference * Imported stack instead of using my own Changed a few minor things. * Imported stack instead of using my own Changed a few minor things. * Update data_structures/stacks/dijkstras_two_stack_algorithm.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update dijkstras_two_stack_algorithm.py Co-authored-by: Christian Clauss <cclauss@me.com>
Describe your change:
Checklist:
Fixes: #{$ISSUE_NO}
.