Question: In this problem you'll implement and revise the mean, median, mode, and frequencyTable functions. These functions will give you basic data analysis and visualization capability,
In this problem you'll implement and revise the mean, median, mode, and frequencyTable functions. These functions will give you basic data analysis and visualization capability, as well as practice with Python collections - lists and dictionaries.
A good programming practice, as we saw with montePi and isInCircle, for example, is to write auxiliary functions to take care of low level work. Write a function isEven with one parameter, n, that will return True if n is an even number, and False if n is odd. Revise median to call isEven.
Demonstrate that functions mean, median, mode, and isEven work correctly by including some test cases in their docstrings, and including code to call doctest.testmod in your program (.py file).
Function frequencyTable is harder to test using doctest.testmod (why?). For this function, it is ok to include small examples in the docstring preceded by > instead of >>>, for example
> frequencyTable([1, 3, 3, 2])
ITEM FREQUENCY
1 1
2 1
3 2
You will need to carefully test frequencyTable, for example, by executing it from the Shell with different arguments, or by writing a small test function that will repeatedly call frequencyTable with different arguments. (You can remove or comment out this code later.)
A good programming practice is to take a step back and look at your code to factor out parts that are used more than once. mode, frequencyTable, and frequencyChart all include code that creates a dictionary from an input list of numbers.
After you are confident that each of these functions is working correctly, write a new function, genFrequencyTable, with one parameter, alist, a list of numbers. genFrequencyTable should generate the dictionary of frequency counts that is currently being generated in mode and frequencyTable. genFrequencyTable will return a dictionary, for example:
>>> genFrequencyTable([1, 2, 3, 3, 1, 4, 5])
{1:2, 2:1, 3:2, 4:1, 5:1}
Function genFrequencyTable is also harder to test using doctest.testmod (why?).
When you are confident genFrequencyTable is working correctly, revise mode and frequencyTable to call genFrequencyTable. Re-test mode and frequencyTable to make sure that the changes have been implemented correctly.
When functions mean, median, mode, isEven, frequencyTable,and genFrequencyTable return correct results for your test cases, write a main function that tests the functions together by producing a frequency table and reporting the mean, median, and mode values for equakes. equakes is a list of earthquake data (magnitudes of earthquakes that occurred in one place over a number of years):
2.9, 3.3, 2.7, 2.8, 2.9, 2.6, 5.3, 6.0, 3.0, 5.3, 2.7, 4.3, 5.4, 4.4, 2.6, 2.8, 4.4, 4.3, 4.7, 3.3, 4.0, 2.5, 4.9, 4.9, 2.5, 4.8, 3.1, 4.9, 4.4, 6.6, 3.3, 2.5, 5.0, 4.8, 2.5, 4.2, 4.5, 2.6, 4.0, 3.3, 3.1, 2.6, 2.7, 2.9, 2.7, 2.9, 3.3, 2.8, 3.1, 2.5, 4.3, 3.2, 4.6, 2.8, 4.8, 5.1, 2.7, 2.6, 3.1, 2.9, 4.2, 4.8, 2.5, 4.5, 4.5, 2.8, 4.7, 4.6, 4.6, 5.1, 4.2, 2.8, 2.5, 4.5, 4.6, 2.6, 5.0, 2.8, 2.9, 2.7, 3.1, 2.6, 2.5, 3.2, 3.2, 5.2, 2.8, 3.2, 2.6, 5.3, 5.5, 2.7, 5.2, 6.4, 4.2, 3.1, 2.8, 4.5, 2.9, 3.1, 4.3, 4.9, 5.2, 2.6, 6.7, 2.7, 4.9, 3.0, 4.9, 4.7, 2.6, 4.6, 2.5, 3.2, 2.7, 6.2, 4.0, 4.6, 4.9, 2.5, 5.1, 3.3, 2.5, 4.7, 2.5, 4.1, 3.1, 4.6, 2.8, 3.1, 6.3]
Include code in your .py file to call the main function.
given code:
def mean(alist):
mean = sum(alist) / len(alist)
return mean
def median(alist):
copylist = alist[ : ]
copylist.sort()
if len(copylist) % 2 == 0:
rightmid = len(copylist) // 2
leftmid = rightmid - 1
median = (copylist [leftmid] + copylist[rightmid]) / 2
else:
mid = len(copylist) // 2
median = copylist[mid]
return median
def mode(alist):
countdict = { }
for item in alist:
if item in countdict:
countdict[item] = countdict[item] + 1
else:
countdict[item] +1
countlist = countdict.values()
maxcount = max(countlist)
modelist = [ ]
for item in countdict:
if countdict[item] == maxcount:
modelist.append(item)
return modelist
def frequencyTable(alist):
countdict = { }
for item in alist:
if item in countdict:
countdict[item] = countdict[item] + 1
else:
countdict[item] = 1
itemlist = list(countdict.keys())
itemlist.sort()
print("ITEM", "FREQUENCY")
for item in itemlist:
print(item, " ", countdict[item])
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
