easy +10 pts

Map, Filter, Reduce

Apply Python's core functional tools to transform, filter, and aggregate numbers.

Write three functions that showcase Python's built-in `map`, `filter`, and `functools.reduce`. 1. `double_all(numbers)` – Return a list where every integer in `numbers` is doubled. Use `map` with a lambda. Order must be preserved. 2. `even_only(numbers)` – Return a list containing only the even integers from `numbers` in their original order. Use `filter` with a lambda. 3. `sum_all(numbers)` – Return the sum of all integers in `numbers`. Use `reduce` from `functools` with a lambda. If `numbers` is empty, return `0` (use an initializer). All functions must use the specified built-in function (`map`, `filter`, `reduce`) as the primary operation. Do not use simple loops or list comprehensions. The input list will contain only integers and will not be `None`.

Constraints

- `0 <= len(numbers) <= 1000` - Each element is an integer between `-10^6` and `10^6`. - For `sum_all`, the sum fits within a standard Python `int`. - Time complexity: O(n).

Example

>>> double_all([1, 2, 3])
[2, 4, 6]
>>> even_only([1, 2, 3, 4])
[2, 4]
>>> sum_all([1, 2, 3, 4])
10
>>> sum_all([])
0
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Remember that `map` returns an iterator — wrap it with `list()`.
Write the lambda for filtering as `lambda x: x % 2 == 0`.
Use `reduce(lambda acc, x: ..., numbers, 0)` for the sum.
Check: `sum_all([])` must return `0`, not raise an error.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.