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
24 changes: 24 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -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/
Binary file added __pycache__/streak.cpython-312.pyc
Binary file not shown.
26 changes: 26 additions & 0 deletions streak.py
Original file line number Diff line number Diff line change
@@ -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
Binary file not shown.
37 changes: 37 additions & 0 deletions tests/test_streak.py
Original file line number Diff line number Diff line change
@@ -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