From ab31f1a3a89fbc49c3bf77866d626c4afc653b9e Mon Sep 17 00:00:00 2001 From: Isidro Arias Date: Fri, 18 Apr 2025 10:11:28 +0200 Subject: [PATCH 1/4] Add median() function using Quickselect --- searches/quick_select.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/searches/quick_select.py b/searches/quick_select.py index c8282e1fa5fc..6236074ffc54 100644 --- a/searches/quick_select.py +++ b/searches/quick_select.py @@ -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): + """One common application of Quickselect is finding the median, which is + the middle element (or average of the two middle elements) in a dataset. It + works efficiently on unsorted lists by partially sorting the data without + fully sorting the entire list. + + >>> import random + >>> random.seed(0) + + >>> d = [2, 2, 3, 9, 9] + >>> random.shuffle(d) + >>> d + [3, 2, 2, 9, 9] + >>> median(d) + 3 + + >>> d = [2, 2, 3, 9, 9, 9] + >>> random.shuffle(d) + >>> median(d) + 6.0 + + """ + mid, rest = divmod(len(data), 2) + if rest: + return quick_select(data, mid) + else: + low_mid = quick_select(data, mid - 1) + high_mid = quick_select(data, mid) + return (low_mid + high_mid) / 2 From f8899cd83fb0575e7adc3500eab8dc14c8b0a727 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 21 May 2025 21:24:37 +0300 Subject: [PATCH 2/4] Update quick_select.py --- searches/quick_select.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/searches/quick_select.py b/searches/quick_select.py index 6236074ffc54..9fdd424bce27 100644 --- a/searches/quick_select.py +++ b/searches/quick_select.py @@ -62,10 +62,11 @@ def quick_select(items: list, index: int): return quick_select(larger, index - (m + count)) -def median(data: list): - """One common application of Quickselect is finding the median, which is - the middle element (or average of the two middle elements) in a dataset. It - works efficiently on unsorted lists by partially sorting the data without +def median(items: list): + """ + One common application of Quickselect is finding the median, which is + the middle element (or average of the two middle elements) in a sorted dataset. + It works efficiently on unsorted lists by partially sorting the data without fully sorting the entire list. >>> import random @@ -80,14 +81,15 @@ def median(data: list): >>> d = [2, 2, 3, 9, 9, 9] >>> random.shuffle(d) + >>> d + [2, 2, 3, 9, 9, 9] >>> median(d) 6.0 - """ - mid, rest = divmod(len(data), 2) - if rest: - return quick_select(data, mid) + mid, rest = divmod(len(items), 2) + if rest != 0: + return quick_select(items=items, index=mid) else: - low_mid = quick_select(data, mid - 1) - high_mid = quick_select(data, mid) + low_mid = quick_select(items=items, index=mid - 1) + high_mid = quick_select(items=items, index=mid) return (low_mid + high_mid) / 2 From 1dc1b4da444aad989068dd57f8fe61694495c2a6 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 21 May 2025 21:26:09 +0300 Subject: [PATCH 3/4] Update quick_select.py --- searches/quick_select.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/searches/quick_select.py b/searches/quick_select.py index 9fdd424bce27..901cd044517f 100644 --- a/searches/quick_select.py +++ b/searches/quick_select.py @@ -86,8 +86,8 @@ def median(items: list): >>> median(d) 6.0 """ - mid, rest = divmod(len(items), 2) - if rest != 0: + mid, rem = divmod(len(items), 2) + if rem != 0: return quick_select(items=items, index=mid) else: low_mid = quick_select(items=items, index=mid - 1) From fc4977c9e454ce978ac3c4a76e45ec526fffc7ce Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 21 May 2025 21:28:20 +0300 Subject: [PATCH 4/4] Update quick_select.py --- searches/quick_select.py | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/searches/quick_select.py b/searches/quick_select.py index 901cd044517f..f67f939c88c3 100644 --- a/searches/quick_select.py +++ b/searches/quick_select.py @@ -69,21 +69,10 @@ def median(items: list): It works efficiently on unsorted lists by partially sorting the data without fully sorting the entire list. - >>> import random - >>> random.seed(0) - - >>> d = [2, 2, 3, 9, 9] - >>> random.shuffle(d) - >>> d - [3, 2, 2, 9, 9] - >>> median(d) + >>> median([3, 2, 2, 9, 9]) 3 - >>> d = [2, 2, 3, 9, 9, 9] - >>> random.shuffle(d) - >>> d - [2, 2, 3, 9, 9, 9] - >>> median(d) + >>> median([2, 2, 9, 9, 9, 3]) 6.0 """ mid, rem = divmod(len(items), 2)