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

2 Likes

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

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

My code is kinda similar to nktn.lx’s code. I do get the correct answers in the task. But, could any of the experts take a look and tell me whether it’s correct?
def max_num(a, b, c):
if a > b and a > c:
return a
elif b > a and b > c:
return b
elif c > a and c > b:
return c
else:
return “It’s a tie!”

I’m by no means an “expert”, but assuming the indentation is correct (you can preserve that by using the </> button when posting), your code is correct.

1 Like

Thank you dr_victoria. And thank you for the indentation tip!

1 Like

This is the solution I came up with now:

``````def max_num(num1, num2, num3):
numlist = [num1, num2, num3]
if numlist.count(max(num1, num2, num3)) >= 2:
return "It's a tie!"
else:
return max(num1, num2, num3)
``````
1. I add num1, num2 and num3 to a list
2. I check with if numlist.count(max(num1, num2, num3)) >=2 if the maximum value of num1, num2 and num3 appears more than twice in the list

If it the maximum value is counted more than twice, it returns “It’s a tie!”. If it isn’t counted more than twice, it returns the maximum value. This saves quite a few lines of code.

My only question right now is if Python offers a simpler way to do this without having to convert my arguments to a list: Can I use the .count method directly on arguments?

so first of all, this is great code, way cooler and cleaner than what i came up with!

what i came up with is also the solution provided, which i was proud of …however, it doesn’t say i got the answer correct with my code (or the same exact code in their solution)
my code:

``````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
else:
return "Its a tie!"
``````

with this code and then clicking “check answer” it gives me this weird message saying:

`max_num(5, 2, 5)` should have returned `"It's a tie!"` , and it returned Its a tie!

there is no ‘max_num(5, 2, 5)’ in there at all… soo ??
edit: I just realized it is saying that because I typed “Its” rather than “It’s”, still don’t know where it is getting “(5, 2, 5)”

whatever, i put your code in there and it says correct!

1 Like

In the challenges the website tests your code by inputting edge cases. It’s helping you debug errors by giving you the input that resulted in incorrect output. In this case it was just a typo, but other times it can really narrow down your efforts in locating errors.

1 Like

Hi all, total novice but I hope this helps! Went through some google-fu to get here though.

``````# Write your max_num function here:
def max_num(num1, num2, num3):
if\
num1 > num2 and num1 > num3 or\
num2 > num1 and num2 > num3 or\
num3 > num1 and num3 > num2:
return max(num1, num2, num3)
else:
return "It's a tie!"

print(max_num(-10, 0, 10))  #10
print(max_num(-10, 5, -30)) #5
print(max_num(-5, -10, -10)) #-5
print(max_num(2, 3, 3)) # It's a tie
print(max_num(3, 3, 3)) # It's a tie!

``````

The `\` introduces a line break to the code to make things easiar to look at (thanks Google). It took me a little bit, so if you need help with the logic: Think transitive property of inequality. If a > b and a < c then c>b . I also found that this code returns the same output for two and three-way ties.

I just want to share how I cracked this challenge. A bit wordy but it worked.

# Write your max_num function here:

def max_num(num1,num2,num3):
list1=[num1,num2,num3]
if list1.count(max(list1))==1:
if max(list1)==num1:
return(num1)
if max(list1)==num2:
return(num2)
if max(list1)==num3:
return(num3)
if list1.count(max(list1))==2:
return(“It’s a tie!”)