Skip to content

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

Merged
merged 9 commits into from
Aug 20, 2020
Merged

Created Dijkstra's Two Stack Algorithm #2321

merged 9 commits into from
Aug 20, 2020

Conversation

echoaj
Copy link
Contributor

@echoaj echoaj commented Aug 17, 2020

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

""" Stack Data Structure Used for the Algorithm"""
def __init__(self):
self.__array = []
self.__len = 0
Copy link
Member

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):
Copy link
Member

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
Copy link
Member

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():
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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):
Copy link
Member

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)
Copy link
Member

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()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
result = operand_stack.peek()
return operand_stack.peek()

def main():
equation = "(5 + ((4 * 2) * (2 + 3)))"
# answer = 45
answer = dijkstras_two_stack_algorithm(equation)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
answer = dijkstras_two_stack_algorithm(equation)
print(f"{dijkstras_two_stack_algorithm(equation) = }")

print(self.__array)


def dijkstras_two_stack_algorithm(equation):
Copy link
Member

Choose a reason for hiding this comment

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

Type hints?

echoaj added 2 commits August 18, 2020 14:11
Changed a few minor things.
Changed a few minor things.
Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

Nice work!

@cclauss cclauss merged commit d3199da into TheAlgorithms:master Aug 20, 2020
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants