Hackerrank - HackerRank Python Built-in Functions and Lambdas Questions and Answers 1 — Questions and Answers
Question 1: A list of dictionaries needs to be sorted based on the 'age' of each person. Which of the following code snippets correctly performs this sort?
- sorted(people, key=lambda person: person['age']) (Correct answer)
- people.sort(lambda person: person['age'])
- sorted(people, 'age')
- people.sort(key='age')
Correct answer: sorted(people, key=lambda person: person['age'])
The built-in `sorted()` function returns a new sorted list. It takes a `key` argument, which should be a function that takes one element and returns a value to sort by. A lambda function `lambda person: person['age']` is the perfect tool for this, as it takes a dictionary (`person`) and returns the value associated with the 'age' key for comparison.
Question 2: What is the primary characteristic that distinguishes a lambda function from a standard function defined with `def`?
- A lambda function can be used as an argument to other functions, while a `def` function cannot.
- A lambda function is restricted to a single expression and implicitly returns its result. (Correct answer)
- A lambda function can have default argument values, but a `def` function cannot.
- A lambda function must be assigned to a variable, whereas a `def` function is automatically named.
Correct answer: A lambda function is restricted to a single expression and implicitly returns its result.
Lambda functions are syntactically restricted to a single expression. The value of this expression is what the function implicitly returns. In contrast, functions defined with `def` can contain multiple statements, including explicit `return` statements, loops, and assignments.
Question 3: Given a list of integers, `numbers = [10, 15, 21, 33, 42, 55]`, which line of code will produce a new list containing only the numbers divisible by 5?
- list(map(lambda x: x % 5 == 0, numbers))
- list(filter(lambda x: x % 5 == 0, numbers)) (Correct answer)
- filter(lambda x: x / 5, numbers)
- map(lambda x: x, numbers, key=lambda x: x % 5 == 0)
Correct answer: list(filter(lambda x: x % 5 == 0, numbers))
The `filter()` built-in function is used to construct an iterator from elements of an iterable for which a function returns true. The lambda function `lambda x: x % 5 == 0` correctly tests each number for divisibility by 5, and `filter()` includes only those that pass the test. `map()` would apply the function to every element, returning a list of booleans, not the filtered numbers.
Question 4: What will be the output of the following Python code snippet? ```python from functools import reduce items = result = reduce(lambda x, y: x * y, items) print(result) ```
- [1, 2, 6, 24]
- 10
- A TypeError because reduce is not a built-in function.
- 24 (Correct answer)
Correct answer: 24
The `reduce` function from the `functools` module applies a function of two arguments cumulatively to the items of an iterable. In this case, `lambda x, y: x * y` multiplies the elements. The process is: 1*2=2, then 2*3=6, then 6*4=24. The final accumulated value is 24.
Question 5: A developer needs to create a new list where each number from an original list is tripled. Which of the following is the most idiomatic use of a built-in function with a lambda to achieve this?
- list(map(lambda x: x * 3, original_list)) (Correct answer)
- list(filter(lambda x: x * 3, original_list))
- reduce(lambda x: x * 3, original_list)
- [x * 3 for x in original_list using lambda]
Correct answer: list(map(lambda x: x * 3, original_list))
The `map()` function applies a given function to each item of an iterable and returns an iterator of the results. The lambda `lambda x: x * 3` is the function that triples a number. `map()` applies this to every element, creating the desired transformed list. `filter()` is for selection, not transformation.
Question 6: In which scenario is using a lambda function most appropriate over a standard named function (`def`)?
- When a complex, multi-line function needs to be passed as an argument to a higher-order function.
- When a function needs to be reused in multiple parts of the program.
- When a simple, one-off function is needed as an argument for a higher-order function like `sorted()` or `map()`. (Correct answer)
- When a function requires type hints and a docstring for clarity.
Correct answer: When a simple, one-off function is needed as an argument for a higher-order function like `sorted()` or `map()`.
Lambda functions are ideal for situations where a small, anonymous function is needed for a short period, typically as an argument to a higher-order function. Their conciseness is a benefit here. For reusable, complex, or well-documented functions, a standard `def` is the better choice.
A list of dictionaries needs to be sorted based on the 'age' of each person.
Which of the following code snippets correctly performs this sort?