diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml new file mode 100644 index 0000000..d4eb971 --- /dev/null +++ b/.github/workflows/pytest.yml @@ -0,0 +1,24 @@ +name: Python package + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pytest + - name: Test with pytest + run: | + pytest tests/ \ No newline at end of file diff --git a/__pycache__/streak.cpython-312.pyc b/__pycache__/streak.cpython-312.pyc new file mode 100644 index 0000000..396dde4 Binary files /dev/null and b/__pycache__/streak.cpython-312.pyc differ diff --git a/streak.py b/streak.py new file mode 100644 index 0000000..f196d56 --- /dev/null +++ b/streak.py @@ -0,0 +1,26 @@ +from typing import List +def longest_positive_streak(nums: List[int]) -> int: + """ + Calculates the length of the longest run of consecutive positive integers in a list. + + Args: + nums: A list of integers. + + Returns: + The length of the longest run of consecutive values strictly greater than 0. + Returns 0 for an empty list. + """ + if not nums: + return 0 + + max_streak = 0 + current_streak = 0 + for num in nums: + if num > 0: + current_streak += 1 + else: + max_streak = max(max_streak, current_streak) + current_streak = 0 + + max_streak = max(max_streak, current_streak) + return max_streak diff --git a/tests/__pycache__/test_streak.cpython-312-pytest-8.4.2.pyc b/tests/__pycache__/test_streak.cpython-312-pytest-8.4.2.pyc new file mode 100644 index 0000000..4dc53bb Binary files /dev/null and b/tests/__pycache__/test_streak.cpython-312-pytest-8.4.2.pyc differ diff --git a/tests/test_streak.py b/tests/test_streak.py new file mode 100644 index 0000000..96331d0 --- /dev/null +++ b/tests/test_streak.py @@ -0,0 +1,37 @@ +import sys +import os +import pytest + +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) + +from streak import longest_positive_streak + +def test_empty_list(): + assert longest_positive_streak([]) == 0 + +def test_all_positive(): + assert longest_positive_streak([1, 2, 3, 4, 5]) == 5 + +def test_all_non_positive(): + assert longest_positive_streak([-1, -2, 0, -5]) == 0 + +def test_mixed_numbers(): + assert longest_positive_streak([1, 2, -1, 4, 5, 6]) == 3 + +def test_streak_at_beginning(): + assert longest_positive_streak([1, 2, 3, -1, 0, 5]) == 3 + +def test_streak_at_end(): + assert longest_positive_streak([-1, 0, 5, 1, 2, 3]) == 4 + +def test_multiple_streaks(): + assert longest_positive_streak([1, 2, 0, 1, 2, 3, 0, 1]) == 3 + +def test_single_positive(): + assert longest_positive_streak([-1, -2, 5, -3, -4]) == 1 + +def test_zeros_in_list(): + assert longest_positive_streak([1, 0, 2, 3, 0, 4, 5, 6, 7]) == 4 + +def test_no_positive_numbers(): + assert longest_positive_streak([0, -1, -2, -3]) == 0 \ No newline at end of file