Začněte nyníZačněte zdarma

Převod funkcí na lambda výrazy

Převeď tyto tři běžně definované funkce na lambda výrazy:

# Returns a bigger of the two numbers
def func1(x, y):
    if x >= y:
        return x

    return y
# Returns a dictionary counting characters in a string
def func2(s):
    d = dict()
    for c in set(s):
        d[c] = s.count(c)

    return d
# Returns a squared root of a sum of squared numbers
def func3(*nums):
    squared_nums = [n**2 for n in nums]
    sum_squared_nums = sum(squared_nums)

    return math.sqrt(sum_squared_nums)

Toto cvičení je součástí kurzu

Procvičování otázek z kódovacích pohovorů v Pythonu

Zobrazit kurz

Interaktivní cvičení na vyzkoušení si v praxi

Vyzkoušejte si toto cvičení dokončením tohoto ukázkového kódu.

# Convert func1() to a lambda expression
lambda1 = ____
print(str(func1(5, 4)) + ', ' + str(lambda1(5, 4)))
print(str(func1(4, 5)) + ', ' + str(lambda1(4, 5)))
Upravit a spustit kód