We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Reduce

The built-in functools.reduce() function takes a function and a list of values, and applies the function to each value in the list, accumulating a single result as it goes.

# import functools from the standard library
import functools


def add(sum_so_far: int, x: int) -> int:
    print(f"sum_so_far: {sum_so_far}, x: {x}")
    return sum_so_far + x


numbers: list[int] = [1, 2, 3, 4]
sum: int = functools.reduce(add, numbers)
# sum_so_far: 1, x: 2
# sum_so_far: 3, x: 3
# sum_so_far: 6, x: 4
# 10 doesn't print, it's just the final result
print(sum)
# 10

Notice that we're passing the function add without the ()! It means that reduce will take care of execution and pass the parameters for you. Think of passing add like handing someone a recipe (the instructions), instead of the finished dish (the result of the execution).

Assignment

Complete the join and the join_first_sentences functions.

      1. A doc_so_far accumulator string – similar to the sum_so_far variable in the example above.
      2. A sentence string – this is the next string we want to add to the accumulator.
    1. doc: str = "This is the first sentence"
      sentence: str = "This is the second sentence"
      print(join(doc, sentence))
      # This is the first sentence. This is the second sentence
      
      1. A list of sentence strings
      2. An integer n

Use list slicing to get the first n sentences.

Here's an example of the expected behavior:

joined: str = join_first_sentences(
    [
        "This is the first sentence",
        "This is the second sentence",
        "This is the third sentence",
    ],
    2,
)
print(joined)
# This is the first sentence. This is the second sentence.