From 6c302277a7be8859d7a58f62288000041b71352a Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Wed, 15 Nov 2023 20:11:01 -0500 Subject: [PATCH 1/2] Start 1388 --- python/Daily-Problem-1388/main.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 python/Daily-Problem-1388/main.py diff --git a/python/Daily-Problem-1388/main.py b/python/Daily-Problem-1388/main.py new file mode 100644 index 0000000..99e708b --- /dev/null +++ b/python/Daily-Problem-1388/main.py @@ -0,0 +1,13 @@ +""" +Tyler Perkins +15-11-23 + +Good morning! Here's your coding interview problem for today. + +This problem was asked by Apple. + +Implement a queue using two stacks. Recall that a queue is a FIFO (first-in, first-out) data structure with the following methods: enqueue, which inserts an element into the queue, and dequeue, which removes it. + +""" + +print("Hello World!") From 7ca8885f3e0aae16f3ea1b944f382272324d961c Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Wed, 15 Nov 2023 20:41:14 -0500 Subject: [PATCH 2/2] Solve 1388 --- python/Daily-Problem-1388/README.md | 37 ++++++++++++ python/Daily-Problem-1388/main.py | 88 ++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 python/Daily-Problem-1388/README.md diff --git a/python/Daily-Problem-1388/README.md b/python/Daily-Problem-1388/README.md new file mode 100644 index 0000000..71b719d --- /dev/null +++ b/python/Daily-Problem-1388/README.md @@ -0,0 +1,37 @@ +# Question 1388 + +Good morning! Here's your coding interview problem for today. + +This problem was asked by Apple. + +Implement a queue using two stacks. Recall that a queue is a FIFO (first-in, first-out) data structure with the following methods: enqueue, which inserts an element into the queue, and dequeue, which removes it. + +## Implementation + +``` +Command: Push(1,2,3) + +LHS | 3 2 1 +RHS | + +Command: Pop() -> Expect val of 1 + +if RHS is not empty + return RHS.pop() + +while LHS is not empty: + RHS.push(LHS.pop()) + +LHS | +RHS | 1 2 3 + +return RHS.pop() + +RHS | 2 3 + + + + + + +``` diff --git a/python/Daily-Problem-1388/main.py b/python/Daily-Problem-1388/main.py index 99e708b..dae79ed 100644 --- a/python/Daily-Problem-1388/main.py +++ b/python/Daily-Problem-1388/main.py @@ -10,4 +10,90 @@ Implement a queue using two stacks. Recall that a queue is a FIFO (first-in, fir """ -print("Hello World!") +class Stack: + def __init__(self): + self._data = [] + + def push(self, val): + self._data.append(val) + + def pop(self): + return self._data.pop() + + def peek(self): + return self._data[-1] + + def empty(self) -> bool: + return len(self._data) == 0 + + +class Queue: + """ + Standard FILO Container + """ + def __init__(self): + self._lhs = Stack() + self._rhs = Stack() + print("Made a new Queue") + + def push(self, val): + """ + Push a value into the back of the queue + """ + print(f"Pushed value {val}") + self._lhs.push(val) + + def pop(self): + """ + Get the oldest value + """ + if not self._rhs.empty(): + ret = self._rhs.pop() + print(f"Pop value {ret}") + return ret + + while not self._lhs.empty(): + self._rhs.push(self._lhs.pop()) + + ret = self._rhs.pop() + print(f"Pop value {ret}") + return ret + + def print(self): + print("Queue State") + print(f"self._lhs {self._lhs._data}") + print(f"self._rhs {self._rhs._data}") + + +print("Testing Queue") +q = Queue() + +q.push(1) +q.push(2) +q.push(3) + +q.print() + +assert q.pop() == 1 +assert q.pop() == 2 +assert q.pop() == 3 + +q.print() + +q.push(4) +q.push(5) + +q.print() + +assert q.pop() == 4 + +q.print() + +q.push(6) + +q.print() + +assert q.pop() == 5 + +q.print() +