From c36e0c4133b2960d6f4d6bb78e1150b62479550b Mon Sep 17 00:00:00 2001 From: Souhardyo Date: Sun, 8 Oct 2023 13:19:09 +0530 Subject: [PATCH 1/7] Added feature Stack Using Two Queues --- .vscode/settings.json | 3 +- data_structures/stacks/TwoQueuedStack.py | 65 ++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 data_structures/stacks/TwoQueuedStack.py diff --git a/.vscode/settings.json b/.vscode/settings.json index ef16fa1aa7ac..35eef8db5c7e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "githubPullRequests.ignoredPullRequestBranches": [ "master" - ] + ], + "python.analysis.typeCheckingMode": "basic" } diff --git a/data_structures/stacks/TwoQueuedStack.py b/data_structures/stacks/TwoQueuedStack.py new file mode 100644 index 000000000000..d2ac296db2ee --- /dev/null +++ b/data_structures/stacks/TwoQueuedStack.py @@ -0,0 +1,65 @@ +class StackUsingQueues: + def __init__(self): + """ + Initialising required queues using lists data structure + """ + self.queue1 = [] + self.queue2 = [] + + def push(self,item): + while len(self.queue1)!= 0: + """ + enqueuing queue 2 using the values from queue 1 bringing them out in First + In First Out (F.I.F.O) order. + """ + self.queue2.append(self.queue1[0]) + self.queue1 .pop(0) + """ + adding the new value to queue 1 + """ + self.queue1.append(item) + + """ + returning the values from queue 2 to queue 1 so as + to replicate the stack data structure + """ + while len(self.queue2)!= 0: + self.queue1.append(self.queue2[0]) + self.queue2.pop(0) + + + + + def pop(self): + if len(self.queue1) != 0: + """ + The first value of queue 1 is being popped here as it + has been implemented in a way that the + first value of the queue which gets popped is the last value of the stack. + And since stack follows Last In First Out (L.I.F.O) order the + following has been implemented. + """ + popped = self.queue1[0] + self.queue1.pop(0) + return popped + else: + return None + + + + + + + + def peek(self): + """ + Function to see the last value inserted in the stack + implemented using queue here, which has the last + value of the stack as its first. + """ + if len(self.queue1) != 0: + return self.queue1[0] + else: + return None + + From 860cc55fcbfbc07a0516af6b135e18c919347e94 Mon Sep 17 00:00:00 2001 From: Souhardyo Date: Sun, 8 Oct 2023 13:26:06 +0530 Subject: [PATCH 2/7] feature/Added-StackUsingTwoQueues --- data_structures/stacks/TwoQueuedStack.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/data_structures/stacks/TwoQueuedStack.py b/data_structures/stacks/TwoQueuedStack.py index d2ac296db2ee..4ff1d9109a22 100644 --- a/data_structures/stacks/TwoQueuedStack.py +++ b/data_structures/stacks/TwoQueuedStack.py @@ -39,8 +39,7 @@ def pop(self): And since stack follows Last In First Out (L.I.F.O) order the following has been implemented. """ - popped = self.queue1[0] - self.queue1.pop(0) + popped = self.queue1.pop(0) return popped else: return None From db8e457e0b9678c3199fd2b41d2c4c200aff0869 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 08:19:34 +0000 Subject: [PATCH 3/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/stacks/TwoQueuedStack.py | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/data_structures/stacks/TwoQueuedStack.py b/data_structures/stacks/TwoQueuedStack.py index 4ff1d9109a22..4c95556f3ce9 100644 --- a/data_structures/stacks/TwoQueuedStack.py +++ b/data_structures/stacks/TwoQueuedStack.py @@ -6,14 +6,14 @@ def __init__(self): self.queue1 = [] self.queue2 = [] - def push(self,item): - while len(self.queue1)!= 0: + def push(self, item): + while len(self.queue1) != 0: """ enqueuing queue 2 using the values from queue 1 bringing them out in First In First Out (F.I.F.O) order. """ self.queue2.append(self.queue1[0]) - self.queue1 .pop(0) + self.queue1.pop(0) """ adding the new value to queue 1 """ @@ -23,13 +23,10 @@ def push(self,item): returning the values from queue 2 to queue 1 so as to replicate the stack data structure """ - while len(self.queue2)!= 0: + while len(self.queue2) != 0: self.queue1.append(self.queue2[0]) self.queue2.pop(0) - - - def pop(self): if len(self.queue1) != 0: """ @@ -44,12 +41,6 @@ def pop(self): else: return None - - - - - - def peek(self): """ Function to see the last value inserted in the stack @@ -60,5 +51,3 @@ def peek(self): return self.queue1[0] else: return None - - From 847721fde60ca92362fe4d11aff936a8cb8ad354 Mon Sep 17 00:00:00 2001 From: Souhardyo Date: Sun, 8 Oct 2023 14:04:49 +0530 Subject: [PATCH 4/7] Added Doctest --- data_structures/stacks/TwoQueuedStack.py | 89 +++++++++++++----------- 1 file changed, 49 insertions(+), 40 deletions(-) diff --git a/data_structures/stacks/TwoQueuedStack.py b/data_structures/stacks/TwoQueuedStack.py index 4ff1d9109a22..f219e43b4b67 100644 --- a/data_structures/stacks/TwoQueuedStack.py +++ b/data_structures/stacks/TwoQueuedStack.py @@ -1,64 +1,73 @@ class StackUsingQueues: - def __init__(self): + def __init__(self) -> None: """ - Initialising required queues using lists data structure + Initialize the stack using two queues. + + >>> stack = StackUsingQueues() """ self.queue1 = [] self.queue2 = [] - def push(self,item): - while len(self.queue1)!= 0: - """ - enqueuing queue 2 using the values from queue 1 bringing them out in First - In First Out (F.I.F.O) order. - """ - self.queue2.append(self.queue1[0]) - self.queue1 .pop(0) - """ - adding the new value to queue 1 - """ - self.queue1.append(item) - + def push(self, item) -> None: """ - returning the values from queue 2 to queue 1 so as - to replicate the stack data structure + Push an item onto the stack. + + >>> stack = StackUsingQueues() + >>> stack.push(1) + >>> stack.push(2) + >>> stack.push(3) + >>> stack.peek() + 3 """ - while len(self.queue2)!= 0: + while len(self.queue1) != 0: + self.queue2.append(self.queue1[0]) + self.queue1.pop(0) + self.queue1.append(item) + while len(self.queue2) != 0: self.queue1.append(self.queue2[0]) self.queue2.pop(0) + def pop(self) -> int: + """ + Pop the top item from the stack and return it. - - - def pop(self): + >>> stack = StackUsingQueues() + >>> stack.push(1) + >>> stack.push(2) + >>> stack.pop() + 2 + >>> stack.pop() + 1 + >>> stack.pop() + 0 + """ if len(self.queue1) != 0: - """ - The first value of queue 1 is being popped here as it - has been implemented in a way that the - first value of the queue which gets popped is the last value of the stack. - And since stack follows Last In First Out (L.I.F.O) order the - following has been implemented. - """ popped = self.queue1.pop(0) return popped else: - return None - - - + return 0 - - - - def peek(self): + def peek(self) -> int: """ - Function to see the last value inserted in the stack - implemented using queue here, which has the last - value of the stack as its first. + Return the top item from the stack without removing it. + + >>> stack = StackUsingQueues() + >>> stack.push(1) + >>> stack.push(2) + >>> stack.peek() + 2 + >>> stack.pop() + 2 + >>> stack.peek() + 1 + >>> stack.pop() + 1 + >>> stack.peek() + 0 """ if len(self.queue1) != 0: return self.queue1[0] else: - return None + return 0 From 609aeff8bde47e0141345b2adb453e6a30e13cb7 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 08:40:04 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/stacks/TwoQueuedStack.py | 1 - 1 file changed, 1 deletion(-) diff --git a/data_structures/stacks/TwoQueuedStack.py b/data_structures/stacks/TwoQueuedStack.py index 5b3f00cdbbf1..bbf692403326 100644 --- a/data_structures/stacks/TwoQueuedStack.py +++ b/data_structures/stacks/TwoQueuedStack.py @@ -69,4 +69,3 @@ def peek(self) -> int: return self.queue1[0] else: return 0 - From 24931d390a75954b4a2e81443ec08b47ce61fcb3 Mon Sep 17 00:00:00 2001 From: Souhardyo Date: Sun, 8 Oct 2023 16:29:44 +0530 Subject: [PATCH 6/7] Added type to argument --- data_structures/stacks/TwoQueuedStack.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/stacks/TwoQueuedStack.py b/data_structures/stacks/TwoQueuedStack.py index 5b3f00cdbbf1..ee06ce2909fc 100644 --- a/data_structures/stacks/TwoQueuedStack.py +++ b/data_structures/stacks/TwoQueuedStack.py @@ -1,14 +1,14 @@ class StackUsingQueues: def __init__(self) -> None: """ - Initialize the stack using two queues. + Initializing the stack using two queues. >>> stack = StackUsingQueues() """ self.queue1 = [] self.queue2 = [] - def push(self, item) -> None: + def push(self, item:int) -> None: """ Push an item onto the stack. From d68b4087c9c6c2daaafd597e8b15f9665cc808d5 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 11:01:02 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/stacks/TwoQueuedStack.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/stacks/TwoQueuedStack.py b/data_structures/stacks/TwoQueuedStack.py index 46db77e28e17..5912e4c8417a 100644 --- a/data_structures/stacks/TwoQueuedStack.py +++ b/data_structures/stacks/TwoQueuedStack.py @@ -8,7 +8,7 @@ def __init__(self) -> None: self.queue1 = [] self.queue2 = [] - def push(self, item:int) -> None: + def push(self, item: int) -> None: """ Push an item onto the stack.