Skip to content

Conversation

@BrianLusina
Copy link
Owner

@BrianLusina BrianLusina commented Dec 27, 2025

Describe your change:

Calculate moving average

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Summary by CodeRabbit

  • New Features
    • Added Moving Average family (standard, buffer-backed, exponential, weighted) and a fixed-size Circular Buffer with explicit full/empty error types.
  • Documentation
    • Added detailed Moving Average README with usage, examples and complexity notes.
  • Tests
    • Added parameterized unit tests covering Moving Average variants.
  • Chores
    • Consolidated package exports to expose these data structures consistently.

✏️ Tip: You can customize this high-level summary in your review settings.

@BrianLusina BrianLusina self-assigned this Dec 27, 2025
@BrianLusina BrianLusina added enhancement Algorithm Algorithm Problem Datastructures Datastructures Documentation Documentation Updates Stream labels Dec 27, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 27, 2025

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Adds a new moving-average family (MovingAverage, MovingAverageWithBuffer, ExponentialMovingAverage, WeightedMovingAverage) with docs and tests, and introduces a CircularBuffer implementation with explicit exceptions and package-level re-exports; updates package init exports and removes a duplicate Test Circular Buffer entry from DIRECTORY.md.

Changes

Cohort / File(s) Summary
Directory / Index
DIRECTORY.md
Updated entries: added Circular Buffer and multiple Moving Average items; removed duplicate Test Circular Buffer entry
Moving Average — package export
datastructures/streams/moving_average/__init__.py
Re-exports MovingAverage and MovingAverageWithBuffer, exposes public API via __all__
Moving Average — implementations
datastructures/streams/moving_average/moving_average.py, .../moving_average_with_buffer.py, .../exponential_moving_average.py, .../weighted_moving_average.py
New classes: MovingAverage, MovingAverageWithBuffer, ExponentialMovingAverage, WeightedMovingAverage — each exposes __init__(size) and next(val) with respective buffer/queue and update logic
Moving Average — docs & tests
datastructures/streams/moving_average/README.md, datastructures/streams/moving_average/test_moving_average.py
Added README describing algorithm/complexity and new unit tests validating behavior across windows and implementations
Circular Buffer — package export
datastructures/circular_buffer/__init__.py
Replaced in-module class definitions with imports from submodules and updated __all__ to export CircularBuffer, BufferFullException, BufferEmptyException
Circular Buffer — implementation & errors
datastructures/circular_buffer/circular_buffer.py, datastructures/circular_buffer/exceptions.py
New CircularBuffer implementation (fixed-size, read/write pointers, write/read/overwrite/clear) and two exception classes BufferFullException / BufferEmptyException

Sequence Diagram(s)

(Skipped — changes are self-contained library/data-structure implementations without multi-component runtime interactions.)

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

🐰 I hopped through bytes and tiny loops,
Built buffers, weights, and moving groups.
Windows slide and sums obey —
Rabbity averages save the day! 🎋

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding a moving average algorithm to the data structures streams module.
Description check ✅ Passed The PR description follows the template structure with a clear change description and most checklist items marked as complete. However, two checklist items are marked as checked (Fix a bug and Documentation change) when the PR primarily adds an algorithm, which appears inconsistent with the actual changeset.

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 156c662 and 33a26e0.

📒 Files selected for processing (11)
  • DIRECTORY.md
  • datastructures/circular_buffer/__init__.py
  • datastructures/circular_buffer/circular_buffer.py
  • datastructures/circular_buffer/exceptions.py
  • datastructures/circular_buffer/test_circular_buffer.py
  • datastructures/streams/moving_average/__init__.py
  • datastructures/streams/moving_average/exponential_moving_average.py
  • datastructures/streams/moving_average/moving_average.py
  • datastructures/streams/moving_average/moving_average_with_buffer.py
  • datastructures/streams/moving_average/test_moving_average.py
  • datastructures/streams/moving_average/weighted_moving_average.py

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
datastructures/streams/moving_average/test_moving_average.py (1)

22-22: Remove redundant statement.

Line 22 calls round(actual, 5) but doesn't use the result. The rounding is already performed in the assertion on line 23, making this line unnecessary.

🔎 Proposed fix
         for data, expected in data_to_expected:
             actual = moving_average.next(data)
-            round(actual, 5)
             self.assertEqual(expected, round(actual, 5))
DIRECTORY.md (1)

328-329: Consider adjusting indentation for markdown linting consistency.

The markdown linter expects 2 spaces for the first-level list item and 4 spaces for the nested item, but the current indentation uses 4 and 6 spaces respectively. While this doesn't affect functionality, adjusting it would align with the linting rules and maintain consistency with the rest of the file.

🔎 Proposed fix
-    * Moving Average
-      * [Test Moving Average](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/streams/moving_average/test_moving_average.py)
+  * Moving Average
+    * [Test Moving Average](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/streams/moving_average/test_moving_average.py)
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4aff926 and 156c662.

⛔ Files ignored due to path filters (1)
  • datastructures/streams/moving_average/images/examples/moving_average_example_1.png is excluded by !**/*.png
📒 Files selected for processing (4)
  • DIRECTORY.md
  • datastructures/streams/moving_average/README.md
  • datastructures/streams/moving_average/__init__.py
  • datastructures/streams/moving_average/test_moving_average.py
🧰 Additional context used
🧬 Code graph analysis (1)
datastructures/streams/moving_average/test_moving_average.py (1)
datastructures/streams/moving_average/__init__.py (2)
  • MovingAverage (5-34)
  • next (16-34)
🪛 markdownlint-cli2 (0.18.1)
DIRECTORY.md

328-328: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


329-329: Unordered list indentation
Expected: 4; Actual: 6

(MD007, ul-indent)

🔇 Additional comments (4)
datastructures/streams/moving_average/README.md (1)

1-62: LGTM! Well-structured documentation.

The documentation is comprehensive, clearly explaining the problem, solution approach, and complexity analysis. The step-by-step algorithm explanation aligns well with the implementation.

datastructures/streams/moving_average/__init__.py (2)

1-2: LGTM!

Imports are appropriate for the implementation.


16-34: LGTM! Clean implementation.

The next method correctly implements the sliding window logic with O(1) time complexity. The algorithm properly maintains the window size, updates the running sum, and calculates the moving average.

datastructures/streams/moving_average/test_moving_average.py (1)

7-13: LGTM! Comprehensive test cases.

The test data covers various scenarios including different window sizes and edge cases.

@BrianLusina BrianLusina merged commit dd14168 into main Dec 27, 2025
2 checks passed
@BrianLusina BrianLusina deleted the feat/datastructures-moving-average branch December 27, 2025 04:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Algorithm Algorithm Problem Datastructures Datastructures Documentation Documentation Updates enhancement Stream

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants