![]() |
Here we will create a lambda function to find the maximum among three numbers in Python. Example: Input: a=10, b=24, c=15 Finding Maximum Among Three Numbers in PythonBelow are some of the ways by which we can find the maximum among three numbers in Python.
Finding Maximum Among Three Numbers Using lambda with max() FunctionThe below code defines a lambda function find_max that takes three parameters and uses the max() function to determine the maximum among them. The result is then printed, demonstrating a concise approach for finding the maximum among three numbers using a lambda function with the max(). Python3
Output
Maximum Number : 10 Time Complexity: O(1) Finding Maximum Among Three Numbers Using lambda with if-else blockThe below code defines a lambda function find_max that takes three parameters and uses an inline if-else block to find the maximum among them. It then applies this lambda function to three numbers (num1, num2, num3) and prints the result, indicating the maximum number. Python3
Output
Maximum number is: 16 Time Complexity: O(1) Finding Maximum Among Three Numbers Using lambda with reduce() FunctionThe below approach code uses the reduce() function from the functools module along with a lambda function to iteratively compare pairs of numbers and find the maximum among three numbers (num1, num2, num3). The final result is then printed, indicating the maximum number. Python3
Output
Maximum number is: 18 Time Complexity: O(n) Finding Maximum Among Three Numbers Using lambda with sorted() FunctionThe below approach code defines a lambda function within big_in_three that uses the sorted() function to arrange the given arguments in ascending order and returns the maximum. It then applies this function to three numbers (num1, num2, num3) and prints the result, indicating the maximum number. Python3
Output
Maximum Number: 25 Time Complexity: O(n log n) ConclusionIn conclusion, lambda functions offer a simple and efficient way to find the maximum among three numbers in Python. The use of lambda with the max() function provides a straightforward and readable approach, while the lambda with the if-else block and reduce() function offers alternative methods. However, it’s important to consider the time and space complexities of each approach. The lambda with max() function stands out for its simplicity and optimal time and space complexity, making it an elegant choice for this specific task. |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |