Average, Maximum and Minimum
The following algorithms are useful. Know how to implement these! Find the sum of a list of numbers. Find the average of a list of numbers. Find the…
Algorithms to know
The following algorithms are useful. Know how to implement these!1. Find the sum of a list of numbers.
2. Find the average of a list of numbers.
3. Find the maximum/minimum of a list of numbers.
2. Find the average of a list of numbers.
3. Find the maximum/minimum of a list of numbers.
You built the summing pattern in the previous unit. This set takes it two steps further.
Sum of a list
Given a list, find the sum of its elements. We can do this by traversing through the list using a for loop. Whenever we have a piece of code that accomplishes a useful task, we should put it in a function.Average
Write a function that accepts a list of numbers as a parameter and returns its average. The average is the sum divided by how many numbers there are, and len() supplies that count.Conditional summing
Write a function that accepts a list of numbers and returns the sum of all even numbers in the list. Loop over everything, but only add when the condition holds.Finding the maximum
Write a function that accepts a nonempty list of numbers and returns its maximum value. Does the code below work?Here's the correct implementation of maximum. The minimum function is similar.