diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml new file mode 100644 index 0000000..883b28e --- /dev/null +++ b/.github/workflows/pytest.yml @@ -0,0 +1,23 @@ +name: Pytest + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10"] + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pytest + - name: Test with pytest + run: | + PYTHONPATH=. pytest \ 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..d15bd29 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..3d628a3 --- /dev/null +++ b/streak.py @@ -0,0 +1,23 @@ +from typing import List + +def longest_positive_streak(nums: List[int]) -> int: + """ + Calculates the length of the longest run of consecutive values + strictly greater than 0. + + Args: + nums: A list of integers. + + Returns: + The length of the longest streak of positive numbers. + """ + 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 \ No newline at end of file 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..5aa48e4 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..b9d2eb7 --- /dev/null +++ b/tests/test_streak.py @@ -0,0 +1,56 @@ +import pytest +from streak import longest_positive_streak + +def test_empty_list(): + """ + Test that an empty list returns a streak of 0. + """ + assert longest_positive_streak([]) == 0 + +def test_multiple_streaks(): + """ + Test that the function returns the longest of multiple streaks. + """ + assert longest_positive_streak([2, 3, -1, 5, 6, 7, 0, 4]) == 3 + +def test_all_positive(): + """ + Test a list where all numbers are positive. + """ + assert longest_positive_streak([1, 2, 3, 4, 5]) == 5 + +def test_all_non_positive(): + """ + Test a list with no positive numbers. + """ + assert longest_positive_streak([-1, -2, 0, -5]) == 0 + +def test_single_element_positive(): + """ + Test a list with a single positive number. + """ + assert longest_positive_streak([5]) == 1 + +def test_single_element_non_positive(): + """ + Test a list with a single non-positive number. + """ + assert longest_positive_streak([-5]) == 0 + +def test_streak_at_the_end(): + """ + Test a list where the longest streak is at the end. + """ + assert longest_positive_streak([-1, 0, 1, 2, 3, 4]) == 4 + +def test_streak_at_the_beginning(): + """ + Test a list where the longest streak is at the beginning. + """ + assert longest_positive_streak([1, 2, 3, 4, -1, 0]) == 4 + +def test_zeros_and_negatives(): + """ + Test a list containing a mix of zeros and negative numbers. + """ + assert longest_positive_streak([1, 2, 0, 3, 4, -5, 6]) == 2 \ No newline at end of file