PPA-3

Question

Write a function maxval that accepts three integers a, b and c as arguments. It should return the maximum among the three numbers.


You do not have to accept input from the user or print output to the console. You just have to write the function definition.

Hint

This function accepts three arguments and returns a single number as output. Functions can have multiple arguments and this function is an example of that. Here is a code snippet to help you solve the problem:

def maxval(a, b, c):
    if (a > b) and (a > c):
        return a

This is obviously an incomplete solution and may even be incorrect. How will you extend this to solve the entire problem? How many return statements would you have to use if you proceed along these lines? Do you require these many return statements? Can you come up with a solution that has just one return statement?

Solution

Note that it is important to check for equality. Study what happens if all \(\geq\) are changed to \(>\).

def maxval(a, b, c):
    if (a >= b) and (a >= c):
        return a
    elif (b >= a) and (b >= c):
        return b
    return c

How is this different from solution-1?

def maxval(a, b, c):
    maxnum = c
    if (a >= b) and (a >= c):
        maxnum = a
    elif (b >= a) and (b >= c):
        maxnum = b
    return maxnum

Make \(a\) the largest of the three numbers by swapping it with \(b\) and \(c\) if required. What would happen if we change the second if into an elif? We don’t seem to be checking for equality here. Is that fine?

def maxval(a, b, c):
    if a < b:
        a, b = b, a
    if a < c:
        a, c = c, a
    return a

This is perhaps the simplest solution.

def maxval(a, b, c):
    return max(a, b, c)

Video Solution