Skip to content

Exercises - map(), filter(), reduce()

Given a list of temperatures in Celsius, convert them all to Fahrenheit.

celsius = [0, 10, 20, 30, 40]
# expected output: [32.0, 50.0, 68.0, 104.0]
# formula: (c * 9/5) + 32
Solution 2.4.1
exercise_2_4_1.py
from typing import Iterator
def celsius_to_fahrenheit(celsius) -> Iterator[str]:
return map(lambda x: (x * 9 / 5) + 32, celsius)
test_exercise_2_4_1.py
from exercises.ch02_funcp.exercise_2_4_1 import celsius_to_fahrenheit
def test_celsius_to_fahrenheit():
# input
celsius = [0, 10, 20, 30, 40]
# lazy returned collection
lazy = celsius_to_fahrenheit(celsius)
# list will force lazy collection to evaluate
assert list(lazy) == [32.0, 50.0, 68.0, 86.0, 104.0]

Given a list of names, return them all in uppercase.

names = ["alice", "bob", "charlie", "diana"]
# expected output: ['ALICE', 'BOB', 'CHARLIE', 'DIANA']
Solution 2.4.2
exercise_2_4_2.py
from typing import Iterator
def capitalise_names(names: list[str]) -> Iterator[str]:
return map(lambda n: n.upper(), names)
test_exercise_2_4_2.py
from exercises.ch02_funcp.exercise_2_4_2 import capitalise_names
def test_capitalise_names():
# input
names = ["alice", "bob", "charlie", "diana"]
# lazy returned collection
lazy = capitalise_names(names)
# list will force lazy collection to evaluate
assert list(lazy) == ["ALICE", "BOB", "CHARLIE", "DIANA"]

Given two lists of numbers, return a list of their products paired by position.

a = [1, 2, 3, 4]
b = [10, 20, 30, 40]
# expected output: [10, 40, 90, 160]
# hint: map() can take two iterables

Given a list of numbers, return only the odd ones.

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# expected output: [1, 3, 5, 7, 9]

Given a list of words, return only the words longer than 4 characters.

words = ["cat", "elephant", "dog", "butterfly", "ant", "bear"]
# expected output: ['elephant', 'butterfly']

Given a list that contains some None values, return only the valid (non-None) values.

data = [1, None, 3, None, 5, None, 7]
# expected output: [1, 3, 5, 7]
# hint: filter() with None as the function removes falsy values

Given a list of numbers, find the maximum value using reduce() — without using max().

from functools import reduce
nums = [3, 1, 4, 1, 5, 9, 2, 6]
# expected output: 9

Given a list of words, join them into a single sentence using reduce() — without using join().

from functools import reduce
words = ["Python", "is", "fun", "to", "learn"]
# expected output: 'Python is fun to learn'

Given a list of lists, flatten it into a single list using reduce().

from functools import reduce
nested = [[1, 2], [3, 4], [5, 6], [7, 8]]
# expected output: [1, 2, 3, 4, 5, 6, 7, 8]

Given a list of numbers, find the sum of the squares of all odd numbers.

from functools import reduce
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# expected output: 165 (1 + 9 + 25 + 49 + 81)

Given a list of products, calculate the total price of only the items that are in stock.

from functools import reduce
products = [
{"name": "apple", "price": 1.5, "in_stock": True},
{"name": "banana", "price": 0.5, "in_stock": False},
{"name": "cherry", "price": 3.0, "in_stock": True},
{"name": "date", "price": 5.0, "in_stock": True},
{"name": "elder", "price": 2.0, "in_stock": False},
]
# expected output: 9.5 (1.5 + 3.0 + 5.0)

Try solving each one using lambda functions first, then see if a named def function makes it cleaner for the more complex ones.