TonyMo

2_11_ave_algorithim.py

Mar 28th, 2021 (edited)
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. # 2 11 Arithmetic
  2. ''' Algorithm
  3. Average is the Sum of the values divided by the number of items, the Count.
  4. Using the list of data
  5. Count the number of items
  6. Add all the items together  
  7. Calculate the Average
  8.  
  9. A list of numbers numbers is supplied.
  10. Loop through the list to count the number of items as *count_n*
  11. Loop through the list to add all the numbers together as *add*  
  12. Calculate the average as =  *add/count_n*
  13.  
  14. Extra
  15. Loop through the list to find the greatest number as biggest
  16. Loop through the list to find the smallest number as smallest
  17. Calculate mean = (biggest + smallest) /2
  18. '''
  19.  
  20. #daily watermeter readings
  21. list = [0.06, 1.29, 0.70, 0.76, 0.08, 0.85, 0.79, 0.57, 0.68]
  22. print("list =",list)
  23. print("Low readings probably due to municipal water not available.")
  24. print("High meter reading probably due to filling tanks after a shortage.")
  25.  
  26.  
  27. #*   Count = number of items in the list like len()
  28. count = 0
  29. for item in list:
  30.  count += 1
  31.  
  32. print("Count of items =", count) # Answer to be 15
  33.  
  34.  
  35. #* Add = sum of value of  items
  36. add = 0
  37. for item in list:
  38.     add = add + item
  39.  
  40. print("List Adds up to =", add)     # Answer to be 40
  41.  
  42.  
  43. #* Occurence = number of times each unique item appears.
  44. occur = {}
  45. for item in list:
  46.     occur[item] = occur.get(item, 0) + 1
  47. print("Item : Occurs",occur)
  48.  
  49.  
  50. #*   Smallest item
  51. smallest = 1 # what if smallest in list is < 1
  52. for item in list:
  53.     if item < smallest:
  54.         smallest = item
  55.  
  56. print("Smallest item:", smallest)
  57.  
  58.  
  59. #*  Biggest item
  60. biggest = 0
  61. for item in list:
  62.     if item > biggest:
  63.         biggest = item
  64.  
  65. print("Biggest item :", biggest)
  66.  
  67.  
  68. #* Average *; sum/count
  69. ave = add/count
  70. print("Average :",ave)
  71.  
  72.  
  73. # mean = (min + max)/ 2.  is a handy rapid mental calculation; close enough.
  74. minmax = smallest + biggest
  75. mean = (smallest + biggest)/2
  76. print("mean = (min + max)/2 =", mean)
  77.  
  78. ''' >>>
  79. list = [0.06, 1.29, 0.7, 0.76, 0.08, 0.85, 0.79, 0.57, 0.68]
  80. Low readings probably due to municipal water not available.
  81. High meter reading probably due to filling tanks after a shortage.
  82. Count of items = 9
  83. List Adds up to = 5.779999999999999
  84. Item : Occurs {0.68: 1, 0.79: 1, 1.29: 1, 0.76: 1, 0.06: 1, 0.08: 1, 0.57: 1, 0.7: 1, 0.85: 1}
  85. Smallest item: 0.06
  86. Biggest item : 1.29
  87. Average : 0.6422222222222221
  88. mean = (min + max)/2 = 0.675
  89. >>>   '''
  90.  
Add Comment
Please, Sign In to add comment