Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -------------------------------------------------------------------------
- # Programming 102: Think Like a Computer Scientist Raspberry Pi Foundation
- # Section 2.11 challenge
- # Functions min, max, and mean
- # -------------------------------------------------------------------------
- from random import randint
- # The function minimum returns the smallest value in a list of values
- # aList - a list of values
- def minimum(aList):
- current = aList[0] # current holds the smallest found so far init to list's first element
- for value in aList : # look at every value in the list
- if value < current : # if the value is smaller than the current one
- current = value # save it in current
- return current # return the current value
- # The function maximum returns the largest value in a list of values
- # aList - a list of values
- def maximum(aList):
- current = aList[0] # current holds the largest found so far init to list's first element
- for value in aList : # look at every value in the list
- if value > current : # if the value is bigger than the current one
- current = value # save it in current
- return current # return the current value
- # The function mean returns the arithmetic mean of a list of values (sum of the values / number of values)
- # aList - a list of values
- def mean(aList):
- n = len(aList) # get the number of elements on the list
- sum = 0 # initialize the sum to zero
- for value in aList : # look at every value in the list
- sum += value # add it to te sum
- return sum / n # return the current value
- # temp - the observed maximum temperatures in Lisbon during November 2019
- temp = [20.8,20.1,19.6,19.8,17.8,16.8,17.9,16.9,19,17.2,18.4,14.8,17.7,15.9,17.3,18,16.3,16.6,19.7,17.9,18.4,18.7,17.5]
- print("Values: " , temp)
- print("The minimum is "
- + str(minimum(temp)) + ", the maximum is "
- + str(maximum(temp)) + ", and the mean is "
- + str(mean(temp)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement