Advertisement
fnogal

MinMaxMean

Dec 6th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. # -------------------------------------------------------------------------
  2. # Programming 102: Think Like a Computer Scientist Raspberry Pi Foundation
  3. # Section 2.11 challenge
  4. # Functions min, max, and mean
  5. # -------------------------------------------------------------------------
  6. from random import randint
  7.  
  8. # The function minimum returns the smallest value in a list of values
  9. # aList - a list of values
  10. def minimum(aList):
  11. current = aList[0] # current holds the smallest found so far init to list's first element
  12. for value in aList : # look at every value in the list
  13. if value < current : # if the value is smaller than the current one
  14. current = value # save it in current
  15. return current # return the current value
  16.  
  17. # The function maximum returns the largest value in a list of values
  18. # aList - a list of values
  19. def maximum(aList):
  20. current = aList[0] # current holds the largest found so far init to list's first element
  21. for value in aList : # look at every value in the list
  22. if value > current : # if the value is bigger than the current one
  23. current = value # save it in current
  24. return current # return the current value
  25.  
  26. # The function mean returns the arithmetic mean of a list of values (sum of the values / number of values)
  27. # aList - a list of values
  28. def mean(aList):
  29. n = len(aList) # get the number of elements on the list
  30. sum = 0 # initialize the sum to zero
  31. for value in aList : # look at every value in the list
  32. sum += value # add it to te sum
  33. return sum / n # return the current value
  34.  
  35. # temp - the observed maximum temperatures in Lisbon during November 2019
  36. 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]
  37.  
  38. print("Values: " , temp)
  39. print("The minimum is "
  40. + str(minimum(temp)) + ", the maximum is "
  41. + str(maximum(temp)) + ", and the mean is "
  42. + str(mean(temp)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement