mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 13:31:07 +00:00
37fbd8ca2e
added doctest, fixed TypeError: list indices must be integers or slices, not float error due to number/2 producing float as index.
35 lines
773 B
Python
35 lines
773 B
Python
def median(nums):
|
|
"""
|
|
Find median of a list of numbers.
|
|
|
|
>>> median([0])
|
|
0
|
|
>>> median([4,1,3,2])
|
|
2.5
|
|
|
|
Args:
|
|
nums: List of nums
|
|
|
|
Returns:
|
|
Median.
|
|
"""
|
|
sorted_list = sorted(nums)
|
|
med = None
|
|
if len(sorted_list) % 2 == 0:
|
|
mid_index_1 = len(sorted_list) // 2
|
|
mid_index_2 = (len(sorted_list) // 2) - 1
|
|
med = (sorted_list[mid_index_1] + sorted_list[mid_index_2]) / float(2)
|
|
else:
|
|
mid_index = (len(sorted_list) - 1) // 2
|
|
med = sorted_list[mid_index]
|
|
return med
|
|
|
|
def main():
|
|
print("Odd number of numbers:")
|
|
print(median([2, 4, 6, 8, 20, 50, 70]))
|
|
print("Even number of numbers:")
|
|
print(median([2, 4, 6, 8, 20, 50]))
|
|
|
|
if __name__ == '__main__':
|
|
main()
|