use the Python programming language for this question A group of statisticians a
ID: 3594207 • Letter: U
Question
use the Python programming language for this question
A group of statisticians at a local college has asked you to create a set of functions that compute the median and mode of a set of numbers, as defined. Define these functions in a module named stats.py. Also include a function named mean, which computes the average of a set of numbers. Each function should expect a list of numbers as an argument and return a single number. Each function should return 0 if the list is empty. Include the main function that tests the three statistical functions with a given list.
Explanation / Answer
def mean(numbers):
if len(numbers) == 0:
return 0
numberSum = 0
for num in numbers:
numberSum += num
return numberSum/(float)(len(numbers))
def median(numbers):
if len(numbers) == 0:
return 0
numbers.sort()
length = len(numbers)
median = numbers[length//2]
if length % 2 == 0:
median += numbers[(length//2) - 1]
median = median/2.0
return median
def mode(numbers):
if len(numbers) == 0:
return 0
return max(numbers, key = numbers.count)
def main():
numbers = [1, 2, 3, 3, 4, 5]
if mean(numbers) == 3:
print("mean is working properly")
if median(numbers) == 3:
print("median is working properly")
if mode(numbers) == 3:
print("mode is working properly")
main()
# copy pastable code link: https://paste.ee/p/S7wib
Sample run
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.