Skip to content

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

Closed
wants to merge 4 commits into from

Conversation

nandiya
Copy link

@nandiya nandiya commented Oct 16, 2020

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

@nandiya
Copy link
Author

nandiya commented Oct 16, 2020

@Amaras please review my code and can it be used?

Copy link
Member

@Amaras Amaras left a 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
Copy link
Member

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?

Copy link
Author

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..

Copy link
Member

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

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?

Copy link
Author

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

Copy link
Member

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

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

Copy link
Author

Choose a reason for hiding this comment

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

done

@berquist berquist added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Oct 16, 2020
@berquist berquist added hacktoberfest-accepted Hacktoberfest The label for all Hacktoberfest related things! labels Oct 27, 2020
@ntindle
Copy link
Member

ntindle commented Aug 28, 2021

[lang: python]

@github-actions github-actions bot added the lang: python Python programming language label Aug 28, 2021
@leios
Copy link
Member

leios commented Sep 15, 2021

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 %}
Copy link
Contributor

@henrikac henrikac Oct 19, 2021

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 %}

Copy link
Member

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.

@Amaras
Copy link
Member

Amaras commented Oct 21, 2021

I need to re-review this one for sure (wow, it's been a while). Doing that tomorrow or this weekend!

Copy link
Member

@Amaras Amaras left a 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
Copy link
Member

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())
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
print("you just pop ", example_stack.pop())
print("you just popped", example_stack.pop())

Comment on lines +68 to +73
def print(self):
temp = self.queue_list
while temp.next:
print(temp.value)
temp = temp.next
print(temp.value)
Copy link
Member

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

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

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

Copy link
Contributor

@henrikac henrikac left a 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):
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest The label for all Hacktoberfest related things! hacktoberfest-accepted Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) lang: python Python programming language
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants