Skip to content

Commit

Permalink
test: add check to perm check challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
agmontilla committed Apr 13, 2024
1 parent ccfcf34 commit cfa6dd7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
20 changes: 4 additions & 16 deletions lesson4/perm_check.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
from typing import List


class PermCheck:
def solution(self, A: List) -> int:
N = max(A)
return (
1 if len(A) == len(set(A)) and sum(set(A)) - (N * (N + 1) // 2) == 0 else 0
)


if __name__ == "__main__":
# sample = [4, 1, 3, 2]
# sample = [4, 1, 3]
sample = [1, 1]
print(PermCheck().solution(sample))
async def solution(arr: list) -> int:
"""Returns 1 if the array is a permutation and 0 if it is not."""
max_value = max(arr)
return 1 if len(arr) == len(set(arr)) and sum(set(arr)) - (max_value * (max_value + 1) // 2) == 0 else 0
17 changes: 17 additions & 0 deletions tests/lesson4/test_perm_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from lesson4.perm_check import solution


@pytest.mark.parametrize(
"data_input, expected",
[
([4, 1, 3, 2], 1),
([4, 1, 3], 0),
([1, 1], 0),
],
)
@pytest.mark.asyncio
async def test_perm_check(data_input: list[int], expected: int):
"""Should return 1 if the array is a permutation and 0 if it is not."""
assert await solution(data_input) == expected

0 comments on commit cfa6dd7

Please sign in to comment.