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
20 changes: 20 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Pytest

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
- name: Test with pytest
run: |
PYTHONPATH=. pytest
104 changes: 104 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are created by a PyInstaller bundle and not checked into
# git, but they may be mistakenly added when commands are run outside of a
# virtualenv.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyderworkspace

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
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:
"""
Returns the length of the longest run of consecutive values strictly greater than 0.

An empty list returns 0.

Non-positive numbers (0 or negative) break the streak.

Must be deterministic and pure: no randomness, prints, or global state.
"""
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
26 changes: 26 additions & 0 deletions tests/test_streak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest
from streak import longest_positive_streak

def test_empty_list():
assert longest_positive_streak([]) == 0

def test_no_positive_numbers():
assert longest_positive_streak([-1, -2, 0]) == 0

def test_single_streak():
assert longest_positive_streak([1, 2, 3]) == 3

def test_multiple_streaks():
assert longest_positive_streak([1, 2, 0, 1, 2, 3, 0, 1]) == 3

def test_streaks_with_negatives():
assert longest_positive_streak([1, 2, -1, 1, 2, 3, -2, 1]) == 3

def test_zeros_breaking_streaks():
assert longest_positive_streak([1, 0, 2, 3, 0, 4, 5, 6]) == 3

def test_leading_zeros():
assert longest_positive_streak([0, 0, 1, 2, 3]) == 3

def test_trailing_zeros():
assert longest_positive_streak([1, 2, 3, 0, 0]) == 3