Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -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
Binary file added __pycache__/streak.cpython-312.pyc
Binary file not shown.
23 changes: 23 additions & 0 deletions streak.py
Original file line number Diff line number Diff line change
@@ -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
Binary file not shown.
56 changes: 56 additions & 0 deletions tests/test_streak.py
Original file line number Diff line number Diff line change
@@ -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