Compare commits
2 Commits
345464d0d4
...
3f369e1e70
Author | SHA1 | Date | |
---|---|---|---|
3f369e1e70 | |||
a3819c9b26 |
@ -138,3 +138,23 @@ class Roman:
|
||||
total += cls.ALL[c]
|
||||
|
||||
return total
|
||||
|
||||
|
||||
class MinStack:
|
||||
def __init__(self):
|
||||
self._v: list[int] = []
|
||||
self._min: list[int] = []
|
||||
|
||||
def push(self, val: int) -> None:
|
||||
self._v.append(val)
|
||||
self._min.append(min(val, self._min[-1] if self._min else val))
|
||||
|
||||
def pop(self) -> None:
|
||||
self._v.pop(-1)
|
||||
self._min.pop(-1)
|
||||
|
||||
def top(self) -> int:
|
||||
return self._v[-1]
|
||||
|
||||
def getMin(self) -> int: # no qa
|
||||
return self._min[-1]
|
||||
|
@ -28,3 +28,52 @@ def test_find_sqrt_ish(n: int, expected: int):
|
||||
)
|
||||
def test_int_to_roman(n: int, expected: str):
|
||||
assert stuff.Roman.i2r(n) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("ops", "expected"),
|
||||
[
|
||||
(
|
||||
(
|
||||
[
|
||||
("new",),
|
||||
("push", -2),
|
||||
("push", 0),
|
||||
("push", -3),
|
||||
("getMin",),
|
||||
("pop",),
|
||||
("top",),
|
||||
("getMin",),
|
||||
]
|
||||
),
|
||||
[
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
-3,
|
||||
None,
|
||||
0,
|
||||
-2,
|
||||
],
|
||||
)
|
||||
],
|
||||
)
|
||||
def test_min_stack(ops: list[tuple[str] | tuple[str, int]], expected: list[int | None]):
|
||||
returned: list[int | None] = []
|
||||
inst: stuff.MinStack | None = None
|
||||
|
||||
for op in ops:
|
||||
if len(op) == 1:
|
||||
if op[0] == "new":
|
||||
inst = stuff.MinStack()
|
||||
returned.append(None)
|
||||
continue
|
||||
|
||||
returned.append(getattr(inst, op[0])())
|
||||
continue
|
||||
|
||||
method, arg = op
|
||||
returned.append(getattr(inst, method)(arg))
|
||||
|
||||
assert returned == expected
|
||||
|
Loading…
Reference in New Issue
Block a user