Skip to content

Count Evens

Sar Champagne Bielert edited this page Apr 5, 2024 · 7 revisions

Unit 1 Session A

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Are all elements in the list positive integers?
    • Yes.
  • Can the list be empty?
    • Yes. In that case, your function should return zero.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Create a function that counts how many even numbers are in a list of integers.

1) Create a count variable with an initial count of zero
2) For each element in the list
  a) Check if the number is even (divisible by 2)
  b) If so, increment the count by 1
3) Return the count

⚠️ Common Mistakes

  • A number is even when it is evenly divisible by zero. How can we check that in python?

I-mplement

def count_evens(lst):
	count = 0
	for num in lst:
		if num % 2 == 0:
			count += 1
	return count
Clone this wiki locally