59.3k views
1 vote
Complete the implementation of the function get_largeat_even that takes a list of numbers, and uses functional operations to returns the largest even number from 1st. You can assume the list contains at least one even number.

import functools
def get_largest_even (lst): n
>> get_largest_even([5, -1, 12, 10, 2, 8])
12
new_lst = list( _______ (lambda n: n o 2 ______ 0, _____))
return ______ (lamda a, b: _______ (a, b) , new_1st)

User Ecem
by
8.2k points

1 Answer

3 votes

Final answer:

The function get_largest_even is to be completed by using filter to get a list of even numbers and then functools.reduce to find the largest number among them. The correct implementation returns the largest even number from the given list.

Step-by-step explanation:

The student is asking for help with a Python function that needs to be completed. The function, get_largest_even, should take a list of numbers and return the largest even number using functional programming techniques. The partial code provided seems to contain placeholders for filtering even numbers and reducing to find the largest value.

To implement the function, you need to use filter to make a new list of even numbers (new_lst), and then apply functools.reduce to find the largest number in that new list. Here is the completed function:

import functools

def get_largest_even(lst):
new_lst = list(filter(lambda n: n % 2 == 0, lst))
return functools.reduce(lambda a, b: a if a > b else b, new_lst)

You can call this function with a list of numbers, and it will return the largest even number, as in the provided example with get_largest_even([5, -1, 12, 10, 2, 8]) which would correctly return 12.

User Ganesh Kalje
by
8.8k points