Source code for gt_average

"""
This program finds all values greater than average, 
then returns the number of such values, and the median of these values.

Pseudo code::

      List of co2_levels is given
      Find average value in the list
      Find the list of values greater than average, 
      Put them in a list
      Return the length of the list (number of such values)
      Find the median and print

"""

[docs]def median(L): """Given a list of values, returns the median value. """ vals = list(L) ## make a copy to make sure the original list is not changed vals.sort() numvalues = len(vals) medianindex = numvalues/2 if numvalues%2 == 1: ##Odd number of values return vals[medianindex] else: ## Even number of values, return the average of two middle values return sum(vals[medianindex-1:medianindex+1])/2.0 ######## Main program starts here.
co2_levels = [ 320.03, 322.16, 328.07, 333.91, 341.47,\ 348.92, 357.29, 363.77, 371.51, 382.47, 392.95 ] avg = float(sum(co2_levels))/len(co2_levels) gtavg = [] for value in co2_levels: if value > avg: gtavg.append(value) print "average is", avg print "Values greater than average", gtavg print "%d values greater than average" %(len(gtavg)) medianval = median(gtavg) print "Median value", medianval