-
-
Notifications
You must be signed in to change notification settings - Fork 46.5k
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
stack_using_two_queues #10002
stack_using_two_queues #10002
Conversation
for more information, see https://pre-commit.ci
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.
Looks cool...
""" Implementing stack using two arrays""" | ||
|
||
|
||
class Stack(Generic[T]): # Stack class to implement stack operations |
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.
""" Implementing stack using two arrays""" | |
class Stack(Generic[T]): # Stack class to implement stack operations | |
class Stack(Generic[T]): | |
""" Implement a stack using two queues""" | |
class Stack(Generic[T]): # Stack class to implement stack operations | ||
def __init__(self) -> None: | ||
self.insert_queue = deque() # First Queue to be used for inserting | ||
self.suffle_queue = deque() # Second Queue to be used for suffling |
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.
suffling
is not a word.
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.
OPTIONAL: Should Stack
be a dataclass?
self.suffle_queue = deque() # Second Queue to be used for suffling | ||
|
||
def push(self, item: int) -> None: | ||
self.insert_queue.append(item) # Add items into the Queue |
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 code already says what the comment repeats.
self.insert_queue.append(item) # Add items into the Queue | |
self.insert_queue.append(item) |
self.insert_queue.append( | ||
self.suffle_queue.popleft() | ||
) # Popping the elements |
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 not wrap lines just to make room for a comment.
self.insert_queue.append( | |
self.suffle_queue.popleft() | |
) # Popping the elements | |
# Popping the elements | |
self.insert_queue.append(self.suffle_queue.popleft()) |
self.insert_queue, self.suffle_queue = self.suffle_queue, self.insert_queue | ||
|
||
def pop(self) -> int: | ||
if not (self.suffle_queue): # if the stack 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.suffle_queue): # if the stack is empty | |
if not self.suffle_queue: # if the stack is empty |
def printing(self) -> None: | ||
print(self.suffle_queue) | ||
|
||
def size(self) -> int: | ||
return len(self.suffle_queue) |
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.
These magic methods
will allow us to just print(stack)
and len(stack)
.
def printing(self) -> None: | |
print(self.suffle_queue) | |
def size(self) -> int: | |
return len(self.suffle_queue) | |
def __str__(self) -> str: | |
return tuple(self.suffle_queue) | |
def __len__(self) -> int: | |
return len(self.suffle_queue) |
|
||
|
||
def test_stack() -> None: | ||
s = Stack() # Creating a stack in S |
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.
Single-letter variable names makes code look like it was written in the 1970's.
s = Stack() # Creating a stack in S | |
stack = Stack() # Create a empty stack |
Fixed in data_structures/stacks/stack_using_two_queues.py |
Describe your change:
Added the code for the implementation of stack using two queues
Fixes:#9987
Checklist: