Skip to content

Commit

Permalink
"Build an Array With Stack Operations" solution
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelote committed Nov 3, 2023
1 parent d524477 commit bbf88d0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/build_an_array_with_stack_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
def buildArray(self, target: list[int], n: int) -> list[str]:
stream = 1
stack: list[int] = []
ops: list[str] = []

while not stack or len(stack) != len(target):
stack.append(stream)
ops.append("Push")

if stack[-1] != target[len(stack) - 1]:
stack.pop()
ops.append("Pop")

stream += 1

return ops
15 changes: 15 additions & 0 deletions tests/test_build_an_array_with_stack_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest

from src.build_an_array_with_stack_operations import Solution


@pytest.mark.parametrize(
"target,n,expected",
(
([1, 3], 3, ["Push", "Push", "Pop", "Push"]),
([1, 2, 3], 3, ["Push", "Push", "Push"]),
([1, 2], 4, ["Push", "Push"]),
),
)
def test_solution(target, n, expected):
assert Solution().buildArray(target, n) == expected

0 comments on commit bbf88d0

Please sign in to comment.