Compare commits

...

2 Commits

Author SHA1 Message Date
3f369e1e70
Correct min stack (?) 2023-10-21 07:25:38 -04:00
a3819c9b26
A poorly-made min stack 2023-10-21 07:18:56 -04:00
2 changed files with 69 additions and 0 deletions

View File

@ -138,3 +138,23 @@ class Roman:
total += cls.ALL[c] total += cls.ALL[c]
return total 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]

View File

@ -28,3 +28,52 @@ def test_find_sqrt_ish(n: int, expected: int):
) )
def test_int_to_roman(n: int, expected: str): def test_int_to_roman(n: int, expected: str):
assert stuff.Roman.i2r(n) == expected 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