Merge pull request 'origin/daily-problem-1388' (#5) from origin/daily-problem-1388 into master

Reviewed-on: #5
This commit is contained in:
Tyler Perkins 2023-11-16 01:44:34 +00:00
commit cf6d79e9ca
2 changed files with 136 additions and 0 deletions

View File

@ -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
```

View File

@ -0,0 +1,99 @@
"""
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.
"""
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()