-
Notifications
You must be signed in to change notification settings - Fork 2
/
232.py
84 lines (61 loc) · 2.37 KB
/
232.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# [ LeetCode ] 232. Implement Queue using Stacks
from collections import deque
class Solution:
def __init__(self):
self.dequeue: deque = deque()
def push(self, x: int) -> None:
self.dequeue.append(x)
def pop(self) -> int:
return self.dequeue.popleft()
def peek(self) -> int:
return self.dequeue[0]
def empty(self) -> bool:
return len(self.dequeue) == 0
class AnotherSolution:
def __init__(self) -> None:
self.s1: list[int] = []
self.s2: list[int] = []
def push(self, x: int) -> None:
self.s1.append(x)
def pop(self) -> int:
if not self.s2:
while self.s1:
self.s2.append(self.s1.pop())
return self.s2.pop()
def peek(self) -> int:
if self.s2:
return self.s2[-1]
else:
return self.s1[0]
def empty(self) -> bool:
return self.s1 == [] and self.s2 == []
if __name__ == "__main__":
case: dict[str, list[str] | list[list[int]] | list[list[int] | bool]] = {
"input": {
"operations": ["MyQueue", "push", "push", "peek", "pop", "empty"],
"data": [[], [1], [2], [], [], []]
},
"output": [None, None, None, [1], [1], False]
}
solution_result: list[list[int] | bool] = [None]
another_solution_result: list[list[int] | bool] = [None]
for operation, data in zip(
case["input"]["operations"], case["input"]["data"]
):
if operation == "MyQueue":
solution = Solution()
another_solution = AnotherSolution()
elif operation == "push":
solution_result.append(solution.push(x=data))
another_solution_result.append(another_solution.push(x=data))
elif operation == "peek":
solution_result.append(solution.peek())
another_solution_result.append(another_solution.peek())
elif operation == "pop":
solution_result.append(solution.pop())
another_solution_result.append(another_solution.pop())
elif operation == "empty":
solution_result.append(solution.empty())
another_solution_result.append(another_solution.empty())
assert case["output"] == solution_result
assert case["output"] == another_solution_result