forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaverage_median.py
41 lines (33 loc) · 1.14 KB
/
average_median.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Find median of a list of numbers.
Read more about medians:
https://en.wikipedia.org/wiki/Median
"""
def median(nums):
"""Find median of a list of numbers."""
# Sort list
sorted_list = sorted(nums)
print("List of numbers:")
print(sorted_list)
# Is number of items in list even?
if len(sorted_list) % 2 == 0:
# Find index for first middle value.
mid_index_1 = len(sorted_list) / 2
# Find index for second middle value.
mid_index_2 = -(len(sorted_list) / 2) - 1
# Divide middle values by 2 to get average (mean).
med = (sorted_list[mid_index_1] + sorted_list[mid_index_2]) / float(2)
return med # Return makes `else:` unnecessary.
# Number of items is odd.
mid_index = (len(sorted_list) - 1) / 2
# Middle index is median.
med = sorted_list[mid_index]
return med
def main():
"""Call average module to find median of a specific list of numbers."""
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()