mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
490e645ed3
* updating DIRECTORY.md * types(maths): Fix pylance issues in maths * reset(vsc): Reset settings changes * Update maths/jaccard_similarity.py Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com> * revert(erosion_operation): Revert erosion_operation * test(jaccard_similarity): Add doctest to test alternative_union * types(newton_raphson): Add typehints to func bodies --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
42 lines
804 B
Python
42 lines
804 B
Python
from __future__ import annotations
|
|
|
|
|
|
def median(nums: list) -> int | float:
|
|
"""
|
|
Find median of a list of numbers.
|
|
Wiki: https://en.wikipedia.org/wiki/Median
|
|
|
|
>>> median([0])
|
|
0
|
|
>>> median([4, 1, 3, 2])
|
|
2.5
|
|
>>> median([2, 70, 6, 50, 20, 8, 4])
|
|
8
|
|
|
|
Args:
|
|
nums: List of nums
|
|
|
|
Returns:
|
|
Median.
|
|
"""
|
|
# The sorted function returns list[SupportsRichComparisonT@sorted]
|
|
# which does not support `+`
|
|
sorted_list: list[int] = sorted(nums)
|
|
length = len(sorted_list)
|
|
mid_index = length >> 1
|
|
return (
|
|
(sorted_list[mid_index] + sorted_list[mid_index - 1]) / 2
|
|
if length % 2 == 0
|
|
else sorted_list[mid_index]
|
|
)
|
|
|
|
|
|
def main():
|
|
import doctest
|
|
|
|
doctest.testmod()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|