Open In App

Python Program Maximum of Three Number using Lambda

Last Updated : 09 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Here we will create a lambda function to find the maximum among three numbers in Python.

Example:

Input: a=10, b=24, c=15
Output: 24 # using Lambda

Finding Maximum Among Three Numbers in Python

Below are some of the ways by which we can find the maximum among three numbers in Python.

  • Using lambda with max() Function
  • Using lambda with nested if-else block
  • Using lambda with reduce() Function
  • Using lambda with sorted() Function

Finding Maximum Among Three Numbers Using lambda with max() Function

The 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




# using lambda with max() function
find_max = lambda a, b, c: max(a, b, c)
 
result = find_max(10, 5, 8)
 
print("Maximum Number :", result)


Output

Maximum Number : 10



Time Complexity: O(1)
Space complexity: O(1)

Finding Maximum Among Three Numbers Using lambda with if-else block

The 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




# Define lambda function
find_max = lambda x, y, z: (x if (x > y and x > z) else y if y > z else z)
 
num1 = 14
num2 = 15
num3 = 16
 
# Find maximum using lambda function
result = find_max(num1, num2, num3)
 
#printing result
print("Maximum number is:", result)


Output

Maximum number is: 16



Time Complexity: O(1)
Space complexity: O(1)

Finding Maximum Among Three Numbers Using lambda with reduce() Function

The 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




from functools import reduce
 
num1 = 14
num2 = 18
num3 = 16
 
# reduce() with lambda to find maximum among three numbers
result = reduce(lambda x, y: x if x > y else y, [num1, num2, num3])
 
# print the result
print("Maximum number is:", result)


Output

Maximum number is: 18



Time Complexity: O(n)
Space complexity: O(1)

Finding Maximum Among Three Numbers Using lambda with sorted() Function

The 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




def big_in_three(*args):
    return sorted(args, key=lambda x: x)[-1]
 
# Example usage
num1 = 10
num2 = 25
num3 = 15
 
result = big_in_three(num1, num2, num3)
print(f"Maximum Number: {result}")


Output

Maximum Number: 25



Time Complexity: O(n log n)
Space Complexity: O(n)

Conclusion

In 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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads