Update maths/interquartile_range.py

Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
Dipankar Mitra 2023-08-05 22:14:40 +05:30 committed by GitHub
parent 14a3fb419d
commit da1c06313e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -48,8 +48,9 @@ def interquartile_range(x: np.array) -> float:
if length == 0: if length == 0:
raise ValueError("The list is empty. Provide a non-empty list.") raise ValueError("The list is empty. Provide a non-empty list.")
x.sort() x.sort()
q1 = find_median(x[0 : length // 2]) div, mod = divmod(length, 2)
half_length = (length // 2) + 1 if length % 2 else length // 2 q1 = find_median(x[:div])
half_length = sum((div, mod))
q3 = find_median(x[half_length:length]) q3 = find_median(x[half_length:length])
return q3 - q1 return q3 - q1