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..5912e4c8417a --- /dev/null +++ b/data_structures/stacks/TwoQueuedStack.py @@ -0,0 +1,71 @@ +class StackUsingQueues: + def __init__(self) -> None: + """ + Initializing the stack using two queues. + + >>> stack = StackUsingQueues() + """ + self.queue1 = [] + self.queue2 = [] + + def push(self, item: int) -> None: + """ + Push an item onto the stack. + + >>> stack = StackUsingQueues() + >>> stack.push(1) + >>> stack.push(2) + >>> stack.push(3) + >>> stack.peek() + 3 + """ + 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. + + >>> stack = StackUsingQueues() + >>> stack.push(1) + >>> stack.push(2) + >>> stack.pop() + 2 + >>> stack.pop() + 1 + >>> stack.pop() + 0 + """ + if len(self.queue1) != 0: + popped = self.queue1.pop(0) + return popped + else: + return 0 + + def peek(self) -> int: + """ + 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 0