Python Program Maximum of Three Number using Lambda
Last Updated :
09 Feb, 2024
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)
OutputMaximum 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)
OutputMaximum 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)
OutputMaximum 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}")
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.
Similar Reads
Python program maximum of three The task of finding the maximum of three numbers in Python involves comparing three input values and determining the largest among them using various techniques. For example, if a = 10, b = 14, and c = 12, the result will be 14.Using max()max() is the straightforward approach to find the maximum of
3 min read
Python Program for Number of pairs with maximum sum Write a python program for a given array arr[], count number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i < j. Example: Input : arr[] = {1, 1, 1, 2, 2, 2}Output: 3Explanation: The maximum possible pair sum where i<j is 4, which is given by 3 pairs, so the answer is 3 the
3 min read
Python Program to print a specific number of rows with Maximum Sum Given a Matrix, the following article extracts a specifies number of rows that has a maximum sum. Input : test_list = [[3, 4, 5, 6], [1, 4, 6], [199], [2, 3, 4, 5, 6], [7, 3, 1]], K = 3Â Output : [[199], [2, 3, 4, 5, 6], [3, 4, 5, 6]]Â Explanation : 199 > 20 > 18, 3 maximum elements rows are e
5 min read
Python Program Maximum Of Four Python, a versatile and widely used programming language, offers multiple ways to find the maximum value among four given numbers. Whether you're a beginner or an experienced developer, understanding different approaches can enhance your problem-solving skills. In this article, we will explore simpl
2 min read
Python Program for Maximum and Minimum in a square matrix. Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive Method : We find maximum and minimum of matrix
3 min read
Python program to Sort Tuples by their Maximum element Given a Tuple List sort tuples by maximum element in a tuple. Input : test_list = [(4, 5, 5, 7), (1, 3, 7, 4), (19, 4, 5, 3), (1, 2)] Output : [(19, 4, 5, 3), (4, 5, 5, 7), (1, 3, 7, 4), (1, 2)] Explanation : 19 > 7 = 7 > 2, is order, hence reverse sorted by maximum element. Input : test_list
5 min read
Python program to find the key of maximum value tuples in a dictionary Given a dictionary with values as tuples, the task is to write a python program to find the key of maximum value tuples. Examples: Input : test_dict = {'gfg' : ("a", 3), 'is' : ("c", 9), 'best' : ("k", 10), 'for' : ("p", 11), 'geeks' : ('m', 2)}Output : forExplanation : 11 is maximum value of tuple
6 min read
Python - Maximum product using K elements Sometimes, while working with Python lists, we can have a problem in which we need to maximize certain numbers. There can be many conditions of maximizing. Example of that is maximizing product by using K elements. Lets discuss certain ways in which this can be performed. Method #1 : Using max() + s
3 min read
Python Program to Find Largest Number in a List Finding the largest number in a list is a common task in Python. There are multiple way to do but the simplest way to find the largest in a list is by using Python's built-in max() function:Using max()Python provides a built-in max() function that returns the largest item in a list or any iterable.
3 min read
Python Program that displays the key of list value with maximum range Given a Dictionary with keys and values that are lists, the following program displays key of the value whose range in maximum. Range = Maximum number-Minimum number Input : test_dict = {"Gfg" : [6, 2, 4, 1], "is" : [4, 7, 3, 3, 8], "Best" : [1, 0, 9, 3]} Output : Best Explanation : 9 - 0 = 9, Maxim
5 min read