Even numbers in even places
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- loops
- indexes
Problem
The places in a list are counted from 0: the first number sits in place 0, the second one in place 1, the third one in place 2, and so on.
Write a function that takes a list of whole numbers and returns the sum of the ones that meet two conditions at the same time: the number is even and its place is even too.
With the list 5, 6, 12, 1, 18 and 8 you look at places 0, 2 and 4, where 5, 12 and 18 are sitting. The 5 is odd, so it is left out: you add 12 and 18 and return 30.
Look closely at the difference between the two conditions. The 6 is even, but it sits in place 1, which is odd, so it does not count. And the 5 sits in an even place, but the number is odd, so it does not count either.
If none of them meet both conditions, you return 0. The numbers can be negative, and a negative number can be even too.
Examples
Two of them count
[5, 6, 12, 1, 18, 8] → 30
Ten numbers
[3, 20, 17, 9, 2, 10, 18, 13, 6, 18] → 26
Four numbers
[5, 6, 12, 1] → 12
A single number
[4] → 4
None of them count
[1, 4, 7, 2] → 0
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def sum_even_places(numbers):
passJavaScript
function sumEvenPlaces(numbers) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.