Skip to content

Commit ca6f01e

Browse files
feat: Implement longest_positive_streak function
Adds the `longest_positive_streak` function and accompanying tests. Includes a GitHub Actions workflow to run tests on push and pull request. Adds a .gitignore file to exclude python cache files.
1 parent f4aef47 commit ca6f01e

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed

.github/workflows/pytest.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Pytest
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- name: Set up Python
11+
uses: actions/setup-python@v2
12+
with:
13+
python-version: '3.x'
14+
- name: Install dependencies
15+
run: |
16+
python -m pip install --upgrade pip
17+
pip install pytest
18+
- name: Test with pytest
19+
run: |
20+
pytest

.gitignore

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
27+
# PyInstaller
28+
# Usually these files are created by a PyInstaller bundle and not checked into
29+
# git, but they may be mistakenly added when commands are run outside of a
30+
# virtualenv.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
.pytest_cache/
49+
50+
# Translations
51+
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
local_settings.py
57+
db.sqlite3
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
68+
69+
# PyBuilder
70+
target/
71+
72+
# Jupyter Notebook
73+
.ipynb_checkpoints
74+
75+
# pyenv
76+
.python-version
77+
78+
# celery beat schedule file
79+
celerybeat-schedule
80+
81+
# SageMath parsed files
82+
*.sage.py
83+
84+
# Environments
85+
.env
86+
.venv
87+
env/
88+
venv/
89+
ENV/
90+
env.bak/
91+
venv.bak/
92+
93+
# Spyder project settings
94+
.spyderproject
95+
.spyderworkspace
96+
97+
# Rope project settings
98+
.ropeproject
99+
100+
# mkdocs documentation
101+
/site
102+
103+
# mypy
104+
.mypy_cache/

streak.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def longest_positive_streak(nums: list[int]) -> int:
2+
"""
3+
Returns the length of the longest run of consecutive values strictly greater than 0.
4+
5+
An empty list returns 0.
6+
7+
Non-positive numbers (0 or negative) break the streak.
8+
9+
Must be deterministic and pure: no randomness, prints, or global state.
10+
"""
11+
max_streak = 0
12+
current_streak = 0
13+
for num in nums:
14+
if num > 0:
15+
current_streak += 1
16+
else:
17+
max_streak = max(max_streak, current_streak)
18+
current_streak = 0
19+
max_streak = max(max_streak, current_streak)
20+
return max_streak

tests/test_streak.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
from streak import longest_positive_streak
3+
4+
def test_empty_list():
5+
assert longest_positive_streak([]) == 0
6+
7+
def test_no_positive_numbers():
8+
assert longest_positive_streak([-1, -2, 0]) == 0
9+
10+
def test_single_streak():
11+
assert longest_positive_streak([1, 2, 3]) == 3
12+
13+
def test_multiple_streaks():
14+
assert longest_positive_streak([1, 2, 0, 1, 2, 3, 0, 1]) == 3
15+
16+
def test_streaks_with_negatives():
17+
assert longest_positive_streak([1, 2, -1, 1, 2, 3, -2, 1]) == 3
18+
19+
def test_zeros_breaking_streaks():
20+
assert longest_positive_streak([1, 0, 2, 3, 0, 4, 5, 6]) == 3
21+
22+
def test_leading_zeros():
23+
assert longest_positive_streak([0, 0, 1, 2, 3]) == 3
24+
25+
def test_trailing_zeros():
26+
assert longest_positive_streak([1, 2, 3, 0, 0]) == 3

0 commit comments

Comments
 (0)