Skip to content

Commit 4c760eb

Browse files
committed
Create median_mean_testing.py
Wanted to work through assembling a couple functions to find the median and mean of List elements. This is that "worksheet" script.
1 parent 09ac0f2 commit 4c760eb

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

median_mean_testing.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import datetime
2+
from zoneinfo import ZoneInfo
3+
import statistics
4+
5+
def are_list_elements_numbers(list):
6+
"""
7+
DESCRIPTION: Check if list contains only numbers.
8+
INPUT:
9+
OUTPUT:
10+
"""
11+
for item in list:
12+
return_value: bool = ""
13+
if isinstance(item, int):
14+
return_value = True
15+
elif isinstance(item, float):
16+
return_value = True
17+
else:
18+
return False
19+
if return_value == True:
20+
return True
21+
22+
def get_median_with_statistics(list):
23+
return statistics.median(list)
24+
25+
def get_median_with_code(list):
26+
"""
27+
DESCRIPTION:
28+
Calculate the median as described in https://en.wikipedia.org/wiki/Median
29+
Model from: https://www.codingem.com/python-calculate-median/
30+
Function steps:
31+
1. Accept a list as input.
32+
2. Sorts the list.
33+
3. If the list length is odd.
34+
a. Get the mid-value, then return it.
35+
4. If the list length is even.
36+
b. Get the two middle values, calculate their average, then return that.
37+
INPUT: list
38+
RETURN: value
39+
"""
40+
sorted_list = sorted(list)
41+
list_length = len(sorted_list)
42+
middle = (list_length - 1) // 2
43+
if middle % 2:
44+
return sorted_list[middle]
45+
else:
46+
return (sorted_list[middle] + sorted_list[middle + 1]) / 2.0
47+
48+
def get_average_with_code(list):
49+
return sum(list) / len(list)
50+
51+
def get_average_with_statistics(list):
52+
return statistics.mean(list)
53+
54+
55+
if __name__ == '__main__':
56+
# Example usage:
57+
# numbers = [1, 2, 3, 4, 5, 6, 7]
58+
# numbers = ["a", 1, 2.2, 3.14, 4, 5, 6, 7]
59+
# numbers = [1, 4, 3, 2, 1, 3, 7, 1, 4, 1]
60+
# numbers = [1, 1024, 4, 3, 2, 1, 3, 7, 1, 4, 1]
61+
numbers = [1, 61, 2, 127, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1024]
62+
# numbers = [55251, 59, 18, 63, 34, 17, 17, 48, 26, 16, 38, 21, 22, 50, 45, 12, 10, 8, 58, 15, 3, 56, 33, 4, 47, 36, 26, 51, 65, 8, 33, 70, 39, 34, 35, 2, 44, 1, 55, 31, 61, 3, 19, 45, 39, 28, 35, 23, 18, 6, 61, 5, 4, 14, 26, 57, 41, 22, 4, 18, 24, 28, 58, 52, 61, 6, 49, 57, 51, 10, 19, 6, 34, 40, 19, 55, 32, 14, 67, 47, 6, 25, 32, 21, 419, 228, 47, 186, 245, 284, 583, 522, 611, 60, 499, 578, 517, 106, 195, 64, 343, 402, 191, 155, 332]
63+
# numbers = ["a", "b", "c", "d", "e", "f"]
64+
if (are_list_elements_numbers(numbers)) == True:
65+
us_central_dt = datetime.datetime.now(tz=ZoneInfo("America/Chicago")).strftime('%Y-%m-%d %I:%M:%S%P %Z')
66+
print(f"Test run at: {us_central_dt}")
67+
# print(f"{datetime.datetime.today().strftime('%Y-%m-%d %I:%M:%S%P %Z')}")
68+
average = get_average_with_code(numbers)
69+
print(f"list = {numbers}")
70+
print(f"The list contains {len(numbers)} elements")
71+
72+
median = get_median_with_code(numbers)
73+
print(f"median value using code = {median:.2f}")
74+
median = get_median_with_statistics(numbers)
75+
print(f"median value using statistics = {median:.2f}")
76+
77+
average = get_average_with_code(numbers)
78+
print(f"average value using code = {average:.2f}")
79+
average = get_average_with_statistics(numbers)
80+
print(f"average value using statistics = {average:.2f}")
81+
else:
82+
print(f"Something went wrong. Your list must contain only int and/or float elements.\r\nThe list = {numbers}")

0 commit comments

Comments
 (0)