-3

i need to def a functioon that eccepts 3 parameters that are numbers and returns two values -the biggest number and the smallest number calling the function with 2,3,4 will return 2 4 etc dbut I am not allowed to use the max eval and min functoins so i wrote this *

*def max_and_smallest(num1,num2,num3):
    if num1>=num2 and num2>=num3:
        return num1, num3
    elif num2>=num1 and num1>=num3:
        return num2, num3
    elif num3>=num2 and num2>=num1:
        return num3, num1**"

but it doesnt cover all the option such as if numbers in function calling are 2,1,2 what options i need to write to cover all?

shrili
  • 1
  • 2
  • 1
    @shrili besides spelling errors that make the post painful to read, you also have some typos in your actual code (that cause it not to work without fixes) - please try to post questions that at least have no typos in the code. – Grismar Oct 23 '21 at 06:25
  • [How do I ask a good question?](//stackoverflow.com/help/how-to-ask) – wovano Oct 23 '21 at 06:41
  • See also: [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – wovano Oct 23 '21 at 06:43
  • Does this answer your question? [NumPy: function for simultaneous max() and min()](https://stackoverflow.com/questions/12200580/numpy-function-for-simultaneous-max-and-min) – wovano Oct 23 '21 at 06:45
  • NB, ^ this is a slightly different question (it asks even more than what's asked here), but it contains several correct answers to this question. – wovano Oct 23 '21 at 06:46

3 Answers3

0

What about this:

  1. Add the numbers to a list
  2. Sort the list
  3. Return the first and the last list element
Klaus-Dieter Warzecha
  • 2,265
  • 2
  • 27
  • 33
0

I can think of close to half dozen ways of finding the max and min among three numbers without using max and min functions. Here are a few, one of which I already commented about:

  1. Sort the numbers and return the first (min) and last (max).
  2. Compare the numbers pairwise and update min max at each iteration.
  3. Repeatedly partition the array of the numbers choosing one as the pivot. Say you’ve 2, 1, 2 and you partition using the first. The resultant array would be 1, 2, 2, and you know 1 is the smallest (left of the pivot). Then you partition 2, 2 again using 2.

I suspect you didn’t understand the question. It’s trivial to do this without using min/max function. Lot harder to do this without using comparison operators.

I will leave the code to you.

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
0

For this trivial case you could do this:

def minmax(a, b, c):
    minval = 0
    maxval = 0
    for t in (a, b, c):
        if t < minval:
            minval = t
        if t > maxval:
            maxval = t
    return minval, maxval
wovano
  • 4,543
  • 5
  • 22
  • 49