From ed1df8f539a7b7fec69c693d6e0053ac58d257e7 Mon Sep 17 00:00:00 2001 From: Indranjana Chatterjee Date: Sun, 8 Oct 2023 18:34:00 +0530 Subject: [PATCH 1/3] made_changes --- .vscode/settings.json | 3 +- data_structures/stacks/stack_by_two_queues.py | 78 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 data_structures/stacks/stack_by_two_queues.py diff --git a/.vscode/settings.json b/.vscode/settings.json index ef16fa1aa7ac..9800a08c8a6d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "githubPullRequests.ignoredPullRequestBranches": [ "master" - ] + ], + "GitHooks.hooksDirectory": "c:\\Users\\Indranjana\\OneDrive\\Desktop\\contains\\Python\\.git\\hooks" } diff --git a/data_structures/stacks/stack_by_two_queues.py b/data_structures/stacks/stack_by_two_queues.py new file mode 100644 index 000000000000..c076cbf9387c --- /dev/null +++ b/data_structures/stacks/stack_by_two_queues.py @@ -0,0 +1,78 @@ +from collections import deque +from typing import Generic, TypeVar + +T = TypeVar("T") +""" Implementing stack using two arrays""" + +class Stack(Generic[T]):#Stack class to implement stack operations + + + + + + def __init__(self) : + self.insert_queue=deque() # First Queue to be used for inserting + self.second_queue=deque() # Second Queue to be used + + + def push(self,item:int) : + self.insert_queue.append(item) + while(self.second_queue): + self.insert_queue.append(self.second_queue.popleft()) # Popping the elements + + self.insert_queue,self.second_queue=self.second_queue,self.insert_queue + + + def pop(self) ->int : + if not self.second_queue: # if the stack is empty + return None + return(self.second_queue.popleft()) # if not empty pop + + + def top(self) -> int: + if not self.second_queue: + return None + return(self.second_queue[0]) + + + def __str__(self) -> str: + return tuple(self.second_queue) + + def __len__(self) -> int: + return len(self.second_queue) + + + +def test_stack() -> None: + stack = Stack() # Creating a empty stack + choice=int(input("1 to push,2 to pop,3 to peek ,4 to print, 5 for size,6 to exit:")) + while (choice in (1, 2, 3, 4, 5, 6)): + match(choice): + case 1: + element=int(input("Enter the element to push:")) + stack.push(element) + case 2: + print(stack.pop()) + case 3: + print(stack.top()) + case 4: + print(stack.__str__()) + case 5: + leng=stack.__len__() + print(f"The size of the stack is {leng}") + case 6: + print("Exiting") + break + case _: + print("Enter properly") + + choice=int(input("1 to push,2 to pop,3 to peek ,4 to print, 5 for size,6 to exit:")) + + +if __name__=="__main__": + test_stack() # calling the test function + + + + + From 85ac78ed08913ae27bfd1566f9a8b6b77f0a77b4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Oct 2023 13:07:33 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/stacks/stack_by_two_queues.py | 114 ++++++++---------- 1 file changed, 53 insertions(+), 61 deletions(-) diff --git a/data_structures/stacks/stack_by_two_queues.py b/data_structures/stacks/stack_by_two_queues.py index c076cbf9387c..c1d28faa9d58 100644 --- a/data_structures/stacks/stack_by_two_queues.py +++ b/data_structures/stacks/stack_by_two_queues.py @@ -4,75 +4,67 @@ T = TypeVar("T") """ Implementing stack using two arrays""" -class Stack(Generic[T]):#Stack class to implement stack operations - - - - - - def __init__(self) : - self.insert_queue=deque() # First Queue to be used for inserting - self.second_queue=deque() # Second Queue to be used - - - def push(self,item:int) : + +class Stack(Generic[T]): # Stack class to implement stack operations + def __init__(self): + self.insert_queue = deque() # First Queue to be used for inserting + self.second_queue = deque() # Second Queue to be used + + def push(self, item: int): self.insert_queue.append(item) - while(self.second_queue): - self.insert_queue.append(self.second_queue.popleft()) # Popping the elements - - self.insert_queue,self.second_queue=self.second_queue,self.insert_queue - - - def pop(self) ->int : - if not self.second_queue: # if the stack is empty + while self.second_queue: + self.insert_queue.append( + self.second_queue.popleft() + ) # Popping the elements + + self.insert_queue, self.second_queue = self.second_queue, self.insert_queue + + def pop(self) -> int: + if not self.second_queue: # if the stack is empty return None - return(self.second_queue.popleft()) # if not empty pop - - + return self.second_queue.popleft() # if not empty pop + def top(self) -> int: if not self.second_queue: return None - return(self.second_queue[0]) - - + return self.second_queue[0] + def __str__(self) -> str: return tuple(self.second_queue) - + def __len__(self) -> int: return len(self.second_queue) - - + def test_stack() -> None: - stack = Stack() # Creating a empty stack - choice=int(input("1 to push,2 to pop,3 to peek ,4 to print, 5 for size,6 to exit:")) - while (choice in (1, 2, 3, 4, 5, 6)): - match(choice): - case 1: - element=int(input("Enter the element to push:")) - stack.push(element) - case 2: - print(stack.pop()) - case 3: - print(stack.top()) - case 4: - print(stack.__str__()) - case 5: - leng=stack.__len__() - print(f"The size of the stack is {leng}") - case 6: - print("Exiting") - break - case _: - print("Enter properly") - - choice=int(input("1 to push,2 to pop,3 to peek ,4 to print, 5 for size,6 to exit:")) - - -if __name__=="__main__": - test_stack() # calling the test function - - - - - + stack = Stack() # Creating a empty stack + choice = int( + input("1 to push,2 to pop,3 to peek ,4 to print, 5 for size,6 to exit:") + ) + while choice in (1, 2, 3, 4, 5, 6): + match (choice): + case 1: + element = int(input("Enter the element to push:")) + stack.push(element) + case 2: + print(stack.pop()) + case 3: + print(stack.top()) + case 4: + print(stack.__str__()) + case 5: + leng = stack.__len__() + print(f"The size of the stack is {leng}") + case 6: + print("Exiting") + break + case _: + print("Enter properly") + + choice = int( + input("1 to push,2 to pop,3 to peek ,4 to print, 5 for size,6 to exit:") + ) + + +if __name__ == "__main__": + test_stack() # calling the test function From 1399f93794051dbc6c74c77e38a42f017fb59ea4 Mon Sep 17 00:00:00 2001 From: Indranjana <112919974+IndranjanaChatterjee@users.noreply.github.com> Date: Sun, 8 Oct 2023 20:05:55 +0530 Subject: [PATCH 3/3] Delete .vscode/settings.json --- .vscode/settings.json | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 9800a08c8a6d..000000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "githubPullRequests.ignoredPullRequestBranches": [ - "master" - ], - "GitHooks.hooksDirectory": "c:\\Users\\Indranjana\\OneDrive\\Desktop\\contains\\Python\\.git\\hooks" -}