-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
"Build an Array With Stack Operations" solution
- Loading branch information
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |