MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / find_median

Function find_median

maths/interquartile_range.py:14–31  ·  view source on GitHub ↗

This is the implementation of the median. :param nums: The list of numeric nums :return: Median of the list >>> find_median(nums=([1, 2, 2, 3, 4])) 2 >>> find_median(nums=([1, 2, 2, 3, 4, 4])) 2.5 >>> find_median(nums=([-1, 2, 0, 3, 4, -4])) 1.5 >>> find_medi

(nums: list[int | float])

Source from the content-addressed store, hash-verified

12
13
14def find_median(nums: list[int | float]) -> float:
15 """
16 This is the implementation of the median.
17 :param nums: The list of numeric nums
18 :return: Median of the list
19 >>> find_median(nums=([1, 2, 2, 3, 4]))
20 2
21 >>> find_median(nums=([1, 2, 2, 3, 4, 4]))
22 2.5
23 >>> find_median(nums=([-1, 2, 0, 3, 4, -4]))
24 1.5
25 >>> find_median(nums=([1.1, 2.2, 2, 3.3, 4.4, 4]))
26 2.65
27 """
28 div, mod = divmod(len(nums), 2)
29 if mod:
30 return nums[div]
31 return (nums[div] + nums[(div) - 1]) / 2
32
33
34def interquartile_range(nums: list[int | float]) -> float:

Callers 1

interquartile_rangeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected