Advanced functions and decorators · 15 / 15 Exercise Medium

Reduce a list to one value

Objective

Create a Python program that uses functools.reduce to combine all the elements of a list into a single value.

Instructions

  • Import reduce from functools. reduce(function, list) applies the two-argument function to the accumulated value and the next element, from left to right.
  • Read a line with integers (at least one) separated by spaces.
  • With reduce and lambda functions, compute the product, the sum and the largest (a if a > b else b).
  • Show Product: P, Sum: S and Largest: M on three lines.
Input
2 3 4
Output
Product: 24
Sum: 9
Largest: 4

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise