List Comprehensions
~9 mincode completion
Write squares_of_evens(numbers) that returns the squares of only the even numbers, in the order they appeared.
[1, 2, 3, 4] returns [4, 16], because 2 and 4 are even and their squares are 4 and 16.
Examples
Only the even numbers are squared
- Input
- squares_of_evens([1, 2, 3, 4])
- Output
- [4, 16]
All-odd input gives an empty list
- Input
- squares_of_evens([1, 3, 5])
- Output
- []
Zero is even, and its square is 0
- Input
- squares_of_evens([0, 1, 2])
- Output
- [0, 4]
Hints
Hint 1
The remainder operator gives what is left after the division.
Hint 2
Watch for this: kept odd numbers.
Requirements
numbers: a list of whole numbersReturn a list of the squares of the even numbers, in the original order.
Constraints
Standard library only, no imports required
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~9 min
••••••
3 employers weight this skill
2 frontier labs, 1 AI product company. Top match scores 80.
Python
def squares_of_evens(numbers):
"""
Square the even numbers and ignore the odd ones.
Args:
numbers: a list of whole numbers
Returns:
A list of the squares of the even numbers, in the original order.
"""
# YOUR CODE HERE
pass