Skip to content
Merged
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", "3.11", "3.12"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
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
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Python
__pycache__/
*.pyc
*.pyo
*.pyd

# Environment
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# IDE
.idea/
.vscode/

# Other
*.log
*.swp
Binary file added __pycache__/streak.cpython-312.pyc
Binary file not shown.
20 changes: 20 additions & 0 deletions streak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def longest_positive_streak(nums: list[int]) -> int:
"""
Calculates the length of the longest streak of consecutive positive numbers.

Args:
nums: A list of integers.

Returns:
The length of the longest streak of consecutive 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.
39 changes: 39 additions & 0 deletions tests/test_streak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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_all_positive():
"""Test a simple case with all positive numbers."""
assert longest_positive_streak([1, 2, 3, 4, 5]) == 5

def test_with_negatives():
"""Test that negative numbers break the streak."""
assert longest_positive_streak([1, 2, -1, 4, 5, 6]) == 3

def test_with_zeros():
"""Test that zeros break the streak."""
assert longest_positive_streak([1, 2, 0, 4, 5, 6]) == 3

def test_multiple_streaks():
"""Test that the longest of multiple streaks is returned."""
assert longest_positive_streak([1, 2, -5, 1, 2, 3, 0, 1, 2]) == 3

def test_no_positive_numbers():
"""Test a list with no positive numbers."""
assert longest_positive_streak([-1, -2, -3, 0]) == 0

def test_streak_at_end():
"""Test a case where the longest streak is at the end of the list."""
assert longest_positive_streak([1, -2, 1, 2, 3, 4]) == 4

def test_single_element_list():
"""Test lists with a single element."""
assert longest_positive_streak([5]) == 1
assert longest_positive_streak([-5]) == 0

def test_example_from_prompt():
"""Test the primary example from the problem description."""
assert longest_positive_streak([2, 3, -1, 5, 6, 7, 0, 4]) == 3
Loading