Skip to content

Commit 906bb2c

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 5ba4b9f commit 906bb2c

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

Diff for: searches/median_of_medians.py

+25-16
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
5
2020
"""
2121

22-
def median_of_five(arr:list) -> int:
22+
23+
def median_of_five(arr: list) -> int:
2324
"""
2425
>>> median_of_five([2, 4, 5, 7, 899])
2526
5
@@ -35,27 +36,32 @@ def median_of_five(arr:list) -> int:
3536
:param arr: Array to find median of
3637
:return: median of arr
3738
"""
38-
arr=sorted(arr)
39-
return arr[len(arr)//2]
39+
arr = sorted(arr)
40+
return arr[len(arr) // 2]
41+
4042

41-
def median_of_medians(arr:list) -> int:
43+
def median_of_medians(arr: list) -> int:
4244
"""
4345
Return a pivot to partition data on by calculating
4446
Median of medians of input data
4547
:param arr: The data to be sorted (a list)
4648
:param k: The rank to be searched
4749
:return: element at rank k
4850
"""
49-
if len(arr) <= 5: return median_of_five(arr)
51+
if len(arr) <= 5:
52+
return median_of_five(arr)
5053
medians = []
51-
i=0
52-
while i<len(arr):
53-
if (i + 4) <= len(arr): medians.append(median_of_five(arr[i:].copy()))
54-
else: medians.append(median_of_five(arr[i:i+5].copy()))
55-
i+=5
54+
i = 0
55+
while i < len(arr):
56+
if (i + 4) <= len(arr):
57+
medians.append(median_of_five(arr[i:].copy()))
58+
else:
59+
medians.append(median_of_five(arr[i : i + 5].copy()))
60+
i += 5
5661
return median_of_medians(medians)
5762

58-
def quick_select(arr:list, k:int) -> int:
63+
64+
def quick_select(arr: list, k: int) -> int:
5965
"""
6066
>>> quick_select([2, 4, 5, 7, 899, 54, 32], 5)
6167
32
@@ -76,7 +82,7 @@ def quick_select(arr:list, k:int) -> int:
7682
"""
7783

7884
# Invalid Input
79-
if k>len(arr):
85+
if k > len(arr):
8086
return -1
8187

8288
# x is the estimated pivot by median of medians algorithm
@@ -94,7 +100,10 @@ def quick_select(arr:list, k:int) -> int:
94100
else:
95101
right.append(arr[i])
96102
rank_X = len(left) + 1
97-
if(rank_X==k): answer = x
98-
elif rank_X>k: answer = quick_select(left,k)
99-
elif rank_X<k: answer = quick_select(right,k-rank_X)
100-
return answer;
103+
if rank_X == k:
104+
answer = x
105+
elif rank_X > k:
106+
answer = quick_select(left, k)
107+
elif rank_X < k:
108+
answer = quick_select(right, k - rank_X)
109+
return answer

0 commit comments

Comments
 (0)