-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add median() function using Quickselect #12676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looks good. I have added few minor comments.
@@ -60,3 +60,34 @@ def quick_select(items: list, index: int): | |||
# must be in larger | |||
else: | |||
return quick_select(larger, index - (m + count)) | |||
|
|||
|
|||
def median(data: list): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is an application of the quick_select algorithm, can you move it to its own file?
else: | ||
low_mid = quick_select(data, mid - 1) | ||
high_mid = quick_select(data, mid) | ||
return (low_mid + high_mid) / 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: add comments.
Especially on the else block.
""" | ||
mid, rest = divmod(len(data), 2) | ||
if rest: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to remainder?
Describe your change:
Checklist: