mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
Compare commits
No commits in common. "90748c86aa3dcb9d9292ed5469426fffc0d90ba0" and "0e72de66a51b8e629a80c961b29206b317662ef5" have entirely different histories.
90748c86aa
...
0e72de66a5
@ -1,10 +1,10 @@
|
|||||||
"""
|
"""
|
||||||
An implementation of interquartile range (IQR) which is a measure of statistical
|
This is the implementation of inter_quartile range (IQR).
|
||||||
dispersion, which is the spread of the data.
|
|
||||||
|
|
||||||
The function takes the list of numeric values as input and returns the IQR.
|
function takes the list of numeric values as input
|
||||||
|
and return the IQR as output.
|
||||||
|
|
||||||
Script inspired by this Wikipedia article:
|
Script inspired from its corresponding Wikipedia article
|
||||||
https://en.wikipedia.org/wiki/Interquartile_range
|
https://en.wikipedia.org/wiki/Interquartile_range
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
@ -12,47 +12,52 @@ from __future__ import annotations
|
|||||||
|
|
||||||
def find_median(nums: list[int | float]) -> float:
|
def find_median(nums: list[int | float]) -> float:
|
||||||
"""
|
"""
|
||||||
This is the implementation of the median.
|
This is the implementation of median.
|
||||||
:param nums: The list of numeric nums
|
:param nums: The list of numeric nums
|
||||||
:return: Median of the list
|
:return: Median of the list
|
||||||
>>> find_median(nums=([1, 2, 2, 3, 4]))
|
>>> find_median(nums=([1,2,2,3,4]))
|
||||||
2
|
2
|
||||||
>>> find_median(nums=([1, 2, 2, 3, 4, 4]))
|
|
||||||
|
>>> find_median(nums=([1,2,2,3,4,4]))
|
||||||
2.5
|
2.5
|
||||||
>>> find_median(nums=([-1, 2, 0, 3, 4, -4]))
|
|
||||||
1.5
|
|
||||||
>>> find_median(nums=([1.1, 2.2, 2, 3.3, 4.4, 4]))
|
|
||||||
2.65
|
|
||||||
"""
|
"""
|
||||||
div, mod = divmod(len(nums), 2)
|
length = len(nums)
|
||||||
if mod:
|
if length % 2:
|
||||||
return nums[div]
|
return nums[length // 2]
|
||||||
return (nums[div] + nums[(div) - 1]) / 2
|
return float((nums[length // 2] + nums[(length // 2) - 1]) / 2)
|
||||||
|
|
||||||
|
|
||||||
def interquartile_range(nums: list[int | float]) -> float:
|
def interquartile_range(nums: list[int | float]) -> float:
|
||||||
"""
|
"""
|
||||||
Return the interquartile range for a list of numeric values.
|
This is the implementation of inter_quartile
|
||||||
:param nums: The list of numeric values.
|
range for a list of numeric.
|
||||||
:return: interquartile range
|
:param nums: The list of data point
|
||||||
|
:return: Inter_quartile range
|
||||||
|
|
||||||
>>> interquartile_range(nums=[4, 1, 2, 3, 2])
|
>>> interquartile_range(nums=[4,1,2,3,2])
|
||||||
2.0
|
2.0
|
||||||
>>> interquartile_range(nums = [-2, -7, -10, 9, 8, 4, -67, 45])
|
|
||||||
17.0
|
|
||||||
>>> interquartile_range(nums = [-2.1, -7.1, -10.1, 9.1, 8.1, 4.1, -67.1, 45.1])
|
|
||||||
17.2
|
|
||||||
>>> interquartile_range(nums = [0, 0, 0, 0, 0])
|
|
||||||
0.0
|
|
||||||
>>> interquartile_range(nums=[])
|
>>> interquartile_range(nums=[])
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: The list is empty. Provide a non-empty list.
|
ValueError: The list is empty. Provide a non-empty list.
|
||||||
|
|
||||||
|
>>> interquartile_range(nums = [-2,-7,-10,9,8,4, -67, 45])
|
||||||
|
17.0
|
||||||
|
|
||||||
|
>>> interquartile_range(nums = [0,0,0,0,0])
|
||||||
|
0.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if not nums:
|
length = len(nums)
|
||||||
|
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.")
|
||||||
nums.sort()
|
nums.sort()
|
||||||
length = len(nums)
|
|
||||||
div, mod = divmod(length, 2)
|
div, mod = divmod(length, 2)
|
||||||
q1 = find_median(nums[:div])
|
q1 = find_median(nums[:div])
|
||||||
half_length = sum((div, mod))
|
half_length = sum((div, mod))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user