-
-
Notifications
You must be signed in to change notification settings - Fork 360
adding some code for stack and queue in python #771
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
@Amaras please review my code and can it be used? |
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.
So, I have only done a code review. I don't even discuss the opportunity of adding code example on this chapter...
Don't worry if I'm a bit too harsh, I am usually on code, be it from myself or others.
Also, I don't have write access to the repository, so I couldn't officially approve your code, even if I wanted to
class Node: | ||
def __init__(self, val): | ||
self.value = val | ||
self.next = None |
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.
Why do you want to recreate a linked list structure inside Python?
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.
ahh, I thought this folder for basic implementation in python and we can contribute in any language, so that's why i tried to make one.
well in python itself already has list() which we can used simply, so yeah back to my first reason..
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'm still not sure this a good idea, but I'll let you use it
else : | ||
temp = Node(val) | ||
temp.next = self.stack_list | ||
self.stack_list = temp |
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.
The way you push to the stack seems weird... You push to the beginning, which is not usual. Why don't you simply use a list
object instead of your linked list?
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.
ahh this is back to the first reason.. i'm just implementing the basic way with python, and yeah I thought u're one of the reviewer, bcs I saw your name merged it, sorry abt that
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 now have write access to the repo, so I can now merge if I want. Things change a lot in a year (sorry about the delay) 😄
def pop(self): | ||
if self.stack_list == None: | ||
print ("Stack is Empty") | ||
return None |
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.
You should probably just raise a ValueError
exception instead of returning None
, as the Node
value can actually be None
, and it doesn't help you programmatically differentiate between a None
value, and an empty 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.
done
[lang: python] |
What is the state of this PR? I think there are a few lingering changes to be made, but after that, we now have code for stacks and queues, so this can probably be merged in |
@@ -15,6 +15,12 @@ The notation for this depends on the language you are using. Queues, for example | |||
|
|||
## License | |||
|
|||
## Example Code | |||
{% method %} |
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 could be wrong but are stack and queue not split into two separate examples?
{% method %}
# add stack example
{% endmethod %}
{% method %}
# add queue example
{% endblock %}
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.
Yeah, we'll probably have to change that to fit the current published format indeed.
I need to re-review this one for sure (wow, it's been a while). Doing that tomorrow or this weekend! |
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.
OK, so I still have some problems with your code, but overall it's more an inconsistency.
I'd prefer the code to be more pythonic (mainly that would mean using the __str__
method instead of the print
method, and removing the abbreviations) before merging, though, if you're still into that.
else : | ||
temp = Node(val) | ||
temp.next = self.stack_list | ||
self.stack_list = temp |
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 now have write access to the repo, so I can now merge if I want. Things change a lot in a year (sorry about the delay) 😄
example_stack.push(2) | ||
example_stack.push(3) | ||
example_stack.print() | ||
print("you just pop ", example_stack.pop()) |
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.
print("you just pop ", example_stack.pop()) | |
print("you just popped", example_stack.pop()) |
def print(self): | ||
temp = self.queue_list | ||
while temp.next: | ||
print(temp.value) | ||
temp = temp.next | ||
print(temp.value) |
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.
You might want to do the same as with the Stack.print
method, and say what you are printing.
A better (I think) way to implement the printing would be to provide a __str__
method, and let Python handle the printing when doing print(example_stack)
and print(example_queue)
@@ -15,6 +15,12 @@ The notation for this depends on the language you are using. Queues, for example | |||
|
|||
## License | |||
|
|||
## Example Code | |||
{% method %} |
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.
Yeah, we'll probably have to change that to fit the current published format indeed.
class Node: | ||
def __init__(self, val): | ||
self.value = val | ||
self.next = None |
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'm still not sure this a good idea, but I'll let you use 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.
It looks like you have switched stack and queue around. Your stack adds and removes items to/from the beginning of the stack, and your queue to the end. This is, to my understanding, usually the other way around and the other language implementations also does it this way.
self.stack_list = self.stack_list.next | ||
return pop_val | ||
|
||
def get_top(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.
Minor thing: maybe drop get_
and just call it top
- same with get_front
-> front
?
code example for queue and stack implementation in python
[x] following rule
[x] Only implement algorithms that are already in the AAA
[x] only one language and one algorithm
[x] already test my code