-
-
Notifications
You must be signed in to change notification settings - Fork 359
Added a stack and queue implementation for python3 #1008
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
Added a stack and queue implementation for python3 #1008
Conversation
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.
Sorry about the delay, I was not in the mental space to review this PR before...
Thanks for your interest for the Arcane Algorithm Archive!
So, onto the review, I tend to make harsh code reviews, so sorry in advance if I'm too harsh on you.
It seems you have basically transpiled Java or TypeScript code into Python, so it's definitely not idiomatic, especially with the naming conventions.
I'm definitely not a fan of using abstract base classes when it's not necessary to do so, and you have not proven the need to use them, so that's a hard no for me.
If you had multiple implementations, then maybe I would judge an ABC more favourably, but it is not the case here, so please remove them both.
class IStack(Generic[T], Sized, abc.ABC): | ||
@abc.abstractmethod | ||
def pop(self) -> T: | ||
""" | ||
Removes the last element from the stack and returns it. | ||
""" | ||
... | ||
|
||
@abc.abstractmethod | ||
def push(self, element: T) -> int: | ||
""" | ||
Adds an element to the end of the stack and returns the | ||
new length of the stack. | ||
""" | ||
... | ||
|
||
@abc.abstractmethod | ||
def top(self) -> T: | ||
""" | ||
Returns the first element of the stack. | ||
""" | ||
... |
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.
I feel that this is not necessary for understanding, and may actively harm understanding, as interfaces are not really that common in Python code, contrary to (say) Java code.
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.
Yes I agree with this, like I mentioned below this is my first time contributing and I just didn't know what was expected. I've removed both the abcs in the new revision.
class IQueue(Generic[T], Sized, abc.ABC): | ||
@abc.abstractmethod | ||
def dequeue(self) -> T: | ||
""" | ||
Removes the first element from the queue and returns it. | ||
""" | ||
... | ||
|
||
@abc.abstractmethod | ||
def enqueue(self, element: T) -> int: | ||
""" | ||
Add an element at the end of the queue and returns the | ||
new size of the queue. | ||
""" | ||
... | ||
|
||
@abc.abstractmethod | ||
def front(self) -> T: | ||
""" | ||
Returns the first element in the queue without removing it. | ||
""" | ||
... |
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.
Same thing as the stack one: not sure whether this interface is necessary.
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.
As above
In _stacks_, data follows _Last In, First Out_ (LIFO), which basically means that whichever element you put in last will be the first element you take out. It acts exactly like a stack in real life. If you put a book on a stack of other books, the first book you will look at when sifting through the stack will be the book you just put on the stack. | ||
|
||
In *Queues*, data follows *First In, First Out* (FIFO), which means that whichever element you put in first will be the first element you take out. Imagine a queue of people. It would be unfair if the first person in line for groceries were not the first person to receive attention once the attendant finally shows up. | ||
In _Queues_, data follows _First In, First Out_ (FIFO), which means that whichever element you put in first will be the first element you take out. Imagine a queue of people. It would be unfair if the first person in line for groceries were not the first person to receive attention once the attendant finally shows up. |
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.
Was changing the formatting necessary?
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.
I've reverted this in the new commit
ba84348
to
7eb2445
Compare
Hi @Amaras , thanks so much for taking a look at this PR! Apologies for the the late reply, I was out of town for a little while. This is my first time contributing to the Algorithm Archive so I just wasn't 100% what was expected. I've attempted addressed all of your previous comments in a new revision. Please let me know if there's anything else you would like me to change. |
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.
Don't worry about being late, I'm not that much on GitHub anyway.
I think this revision is much better than the previous one.
We just need to ensure we're on the same page concerning the use of the Sized
abstract base class, so feel free to remove it or argue for the inclusion, whichever you prefer.
We're definitely doing a last pass once this point is resolved :)
T = TypeVar("T") | ||
|
||
|
||
class Queue(Generic[T], Sized): |
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.
Since you define __len__
, I'm not sure if you need to use the Sized
abc.
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.
Good point. I think I originally added in the Sized
abc to save myself typing out __len__
in the 'interfaces' I had before. But since the interfaces are gone now, there's really no reason to keep them around and have been removed in the latest commit.
T = TypeVar("T") | ||
|
||
|
||
class Stack(Generic[T], Sized): |
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.
Same as with the Queue class, not sure if you need to use the Sized
abc.
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.
As above
Thanks for the quick response! I've attempted to address these new comments in the latest commit. Again, let me know if there is anything else you would like me to change. |
Looks good to me. |
f496493
to
ee24f01
Compare
Done! Rebased and added changes to my main branch and pushed them to my forked repo. |
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.
LGTM
Added Stack and Queue implementations for python3
Motivation
Testing
npm run serve
and manually confirmed changes.Notes