Question: Write a program in a file named histogram.py that displays the histogram depicted in Figure 1 1 - 7 . File: stats.py
Write a program in a file named histogram.py that displays the histogram depicted in Figure
File: stats.py
Defines functions to compute the mean, median, std and mode
of a list of numbers.
import math
import random
def meanlyst:
Returns the mean of a list of numbers."""
return sumlyst lenlyst
def frequencieslyst:
Returns a dictionary keyed by the unique
numbers and their frequencies in lyst
# Obtain the set of unique numbers and their
# frequencies, saving these associations in
# a dictionary
theDictionary
for number in lyst:
freq theDictionary.getnumber
theDictionarynumber freq
return theDictionary
def modelyst:
Returns the mode of a list of numbers."""
theDictionary frequencieslyst
theMaximum maxtheDictionaryvalues
for key in theDictionary:
if theDictionarykey theMaximum:
result key
break
return result
def modeslyst:
Returns the modes of a list of numbers."""
# Obtain the set of unique numbers and their
# frequencies, saving these associations in
# a dictionary
theDictionary frequencieslyst
theMaximum maxtheDictionaryvalues
result
for key in theDictionary:
if theDictionarykey theMaximum:
result.appendkey
return result
def medianlyst:
Precondition: lyst is nonempty.
Returns the median of numbers in lyst
if lenlyst:
raise RuntimeErrorList must be nonempty.
copy listlyst
copy.sort
midpoint lenlyst
if lenlyst:
return copymidpoint
else:
return meancopymidpoint copymidpoint
def stdlyst:
Precondition: lyst is nonempty.
Returns the standard deviation of the numbers in lyst
if lenlyst:
raise RuntimeErrorList must be nonempty.
average meanlyst
differences maplambda x: x average, lyst
squares listmaplambda x: x differences
return math.sqrtmeansquares
def getRandomListsize lower, upper, unique False:
Returns a list of randomly generate numbers
within the given bounds."""
theList
for count in rangesize:
number random.randintlower upper
if unique:
while number in theList:
number random.randintlower upper
theList.appendnumber
return theList
def main:
Tests the functions."""
lyst
printList: lyst
printMode: modelyst
printMedian: medianlyst
printMean: meanlyst
printStandard deviation:", stdlyst
printFrequencies: frequencieslyst
lystsort
printlyst
lyst
printList: lyst
printMode: modelyst
printMedian: medianlyst
printMean: meanlyst
printStandard deviation:", stdlyst
printFrequencies: frequencieslyst
lystsort
printlyst
lyst getRandomList
printlyst
lyst getRandomList unique True
printlyst
# The entry point for program execution
if namemain:
mainFigure
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
