Hi there!
I’m wondering it there a more elegant way to solve the task 5 “Max Number”:

Create a function called `max_num()` that has three parameters named `num1` , `num2` , and `num3` .
The function should return the largest of these three numbers. If any of two numbers tie as the largest, you should return `"It's a tie!"` .

My solution is (but I am a novice):

``````def max_num(num1, num2, num3):
if num1 > num2 and num1 > num3:
return num1
elif num2 > num1 and num2 > num3:
return num2
elif num3 > num1 and num3 > num2:
return num3
elif num1 == num2 and num1 > num3:
return "It's a tie!"
elif num1 == num3 and num1 > num2:
return "It's a tie!"
else:
return "It's a tie!"
print(max_num(-10, 0, 10))
print(max_num(-10, 5, -30))
print(max_num(-5, -10, -10))
print(max_num(2, 3, 3))
``````

1 Like

I would write it out in paper to make sure your logic uses the least amount of statements possible to satisfy all demands. Also remember you can use >=
and you don’t need an extra else if your logic is tight too. Try running your logic out on paper and see how many steps it takes, if you finish all the possibilities on paper and you still have code left over, trim it.

The more interesting thing is how do we tackle this if the list is longer (like 1,000 numbers). I think if you figure out that idea you will get more out of this exercise. (The thought exercise is great even if you don’t have the command of the code to implement it yet, because once you do, you already figured out the concept, and it’s just a matter of syntax).

1 Like

Actually yes, there is a more elegant way. Search the web for Sorting and you will find some algorithms. Now, please understand that “algorithm” word does not mean it is complex. It is quite easy. But still, there will be some people on the internet that will try to scare you with calling algorithms too difficult. My advice: Just Ignore them.

Yes, I can share the algorithm with you but, I won’t. You need to look that up yourself so as to develop your skills. (FINDING THE CORRECT STUFF TO LEARN IS AN IMPORTANT SKILL).

2 Likes

Thank you for your advice! With help of a pen and paper I managed to trim my code from 6 to 4 statements. But general logic looks still being the same.

def max_num(x,y,z):
if x > y and x > z:
return x
elif y > z and y > x:
return y
elif x == y or x == z or y == z:
return “It’s a tie!”
else:
return z

To tackle a longer list (like 1000 numbers) - I’m sure there should be some max number in-build operators or may be we can start a loop (not familiar with it’s concept yet). Maybe something else. But thank you for the idea that we have to see a bigger picture in each exercise. I’ll contemplate how to treat a longer list.

1 Like

Thank you for your advice and a hint in what direction to search! Definitely will check sorting algorithms and how they work.

Hi, again
I’ve tried sorting as you suggested. Now my code looks that way:

def max_num(x,y,z):
array = [x, y, z]
a = sorted(array)
if a[1] == a[2]:
return “It’s a tie!”
else:
return a[2]

What do you think? Am I going in the right direction?
Many thanks.

Greetings,

Yes, you are sort of in the right direction.
You have used an inbuilt function `sorted()`; which is not incorrect but, I still suggest that you make your OWN function to sort a list. This will also help you improve your logic building skills.

Yes, it does has something to do with loops and since, you are unfamiliar with same, you probably won’t be able to make your own sorting algorithm now.
But, right after you gain a sufficient proficiency in loops. I want you to look into sorting algorithms (Consider it a TASK).

(EXTRA: After loops, I want you to look into Recursions).

Just remember: Don’t leave the course in between. Finish what you start

Thanks
Nikhil Seth

Nice!
Keep on fighting the good fight! These small things add up over time.

One more idea: with algorithm one concept is considering it’s behavior when it moves towards infinity. It might not be applicable to everyday use, but certain ones will demand more of our machines. That’s one potential reason why we care about what happens when loops (and nested loops) are extended for considerably long periods of items There are a few basic types (around 6) that come up all the time and if you’re interested in learning more, the subject to look up is data structures. Anyone please correct me if I’m wrong with this!

Hey! Thank you for an idea. Added it to my “to do” list and will definitely study the subject of data structures and algorithms. Cheers!

Many thanks for your feedback and some suggestions where to go next. Really appreciate it.
Take care,
Alex

Wouldn’t this code produce a wrong result if z was the largest number and the other two numbers were equal and smaller than z.

1 Like

Hey! Thank you for your comment! You’re right. I haven’t checked that option back than.