Skip to content

Conversation

@BrianLusina
Copy link
Owner

@BrianLusina BrianLusina commented Jan 26, 2026

Describe your change:

Continuous median using two heaps

  • 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 continuous median calculation capability to efficiently compute running medians as new values are inserted into the data stream.
  • Documentation

    • Updated directory structure with references to the new continuous median feature documentation.
  • Tests

    • Added comprehensive unit test coverage for continuous median calculations, validating insertion operations and median retrieval.

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

@BrianLusina BrianLusina self-assigned this Jan 26, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 26, 2026

📝 Walkthrough

Walkthrough

This PR introduces a new ContinuousMedianHandler class that maintains a running median using two heaps (max-heap for lower half, min-heap for upper half). The implementation includes insert and get_median methods, accompanied by test coverage and documentation updates in DIRECTORY.md.

Changes

Cohort / File(s) Summary
Documentation
DIRECTORY.md
Added "Continuous Median" subsection references under Browser History section (2 locations)
Implementation
design_patterns/continuous_median/__init__.py
New ContinuousMedianHandler class with insert() and get_median() methods using dual-heap approach for median tracking
Test Suite
design_patterns/continuous_median/test_continuous_median_handler.py
Parameterized unit tests validating insertion and median retrieval through the public API

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Two heaps pirouette in balanced grace,
Max and min find their rightful place,
Numbers inserted, medians bloom,
Running averages fill the room! 📊✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: adding a continuous median algorithm using two heaps (a data structure/algorithm addition).
Description check ✅ Passed The description follows the template with all required sections completed. The author checked all relevant checklist items and provided a clear summary of changes.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

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

🤖 Fix all issues with AI agents
In `@DIRECTORY.md`:
- Around line 591-592: The new list entry under "Continuous Median" has
incorrect indentation causing MD007; adjust the bullet for "[Test Continuous
Median Handler]" to be a nested list item (two spaces before the asterisk) so it
aligns under the "Continuous Median" parent bullet, ensuring list depth is
consistent with surrounding entries.
🧹 Nitpick comments (2)
design_patterns/continuous_median/test_continuous_median_handler.py (2)

6-10: Normalize test case shape to simplify inputs.

The current nested tuple/list structure is hard to read. Consider representing each step as (list_of_ints, expected) inside a single requests list.

♻️ Suggested test case shape
-CONTINUOUS_MEDIA_HANDLER_TESTS = [
-    (
-        ([(1, 2), 1.50000], [(3), 2.00000]),
-    ),
-]
+CONTINUOUS_MEDIA_HANDLER_TESTS = [
+    (
+        [([1, 2], 1.5), ([3], 2.0)],
+    ),
+]

15-23: Streamline the loop once inputs are consistent.

With the normalized cases above, you can remove the type branch and iterate uniformly.

♻️ Simplified loop and type hint
-    def test_continuous_median_handler(self, requests: List[Tuple[List[Tuple[int]] | int, int | float]]):
+    def test_continuous_median_handler(self, requests: List[Tuple[List[int], int | float]]):
         median_handler = ContinuousMedianHandler()
-        for request in requests:
-            data, expected = request
-            if type(data) is int:
-                median_handler.insert(data)
-            else:
-                for d in data:
-                    median_handler.insert(d)
+        for data, expected in requests:
+            for value in data:
+                median_handler.insert(value)
 
             actual = median_handler.get_median()
             self.assertEqual(expected, actual)

@BrianLusina BrianLusina merged commit 4e58c1c into main Jan 26, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants