Zacznij terazZacznij za darmo

Przekształcanie funkcji na wyrażenia lambda

Przekształć te trzy funkcje zdefiniowane w standardowy sposób na wyrażenia lambda:

# 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)

To ćwiczenie jest częścią kursu

Ćwiczenie pytań na rozmowach kwalifikacyjnych z programowania w Pythonie

Zobacz kurs

Interaktywne ćwiczenie praktyczne

Spróbuj tego ćwiczenia, uzupełniając ten przykładowy kod.

# 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)))
Edytuj i uruchom kod