Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 2 11 Arithmetic
- ''' Algorithm
- Average is the Sum of the values divided by the number of items, the Count.
- Using the list of data
- Count the number of items
- Add all the items together
- Calculate the Average
- A list of numbers numbers is supplied.
- Loop through the list to count the number of items as *count_n*
- Loop through the list to add all the numbers together as *add*
- Calculate the average as = *add/count_n*
- Extra
- Loop through the list to find the greatest number as biggest
- Loop through the list to find the smallest number as smallest
- Calculate mean = (biggest + smallest) /2
- '''
- #daily watermeter readings
- list = [0.06, 1.29, 0.70, 0.76, 0.08, 0.85, 0.79, 0.57, 0.68]
- print("list =",list)
- print("Low readings probably due to municipal water not available.")
- print("High meter reading probably due to filling tanks after a shortage.")
- #* Count = number of items in the list like len()
- count = 0
- for item in list:
- count += 1
- print("Count of items =", count) # Answer to be 15
- #* Add = sum of value of items
- add = 0
- for item in list:
- add = add + item
- print("List Adds up to =", add) # Answer to be 40
- #* Occurence = number of times each unique item appears.
- occur = {}
- for item in list:
- occur[item] = occur.get(item, 0) + 1
- print("Item : Occurs",occur)
- #* Smallest item
- smallest = 1 # what if smallest in list is < 1
- for item in list:
- if item < smallest:
- smallest = item
- print("Smallest item:", smallest)
- #* Biggest item
- biggest = 0
- for item in list:
- if item > biggest:
- biggest = item
- print("Biggest item :", biggest)
- #* Average *; sum/count
- ave = add/count
- print("Average :",ave)
- # mean = (min + max)/ 2. is a handy rapid mental calculation; close enough.
- minmax = smallest + biggest
- mean = (smallest + biggest)/2
- print("mean = (min + max)/2 =", mean)
- ''' >>>
- list = [0.06, 1.29, 0.7, 0.76, 0.08, 0.85, 0.79, 0.57, 0.68]
- Low readings probably due to municipal water not available.
- High meter reading probably due to filling tanks after a shortage.
- Count of items = 9
- List Adds up to = 5.779999999999999
- 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}
- Smallest item: 0.06
- Biggest item : 1.29
- Average : 0.6422222222222221
- mean = (min + max)/2 = 0.675
- >>> '''
Add Comment
Please, Sign In to add comment