# Why should we use a naive approach?

Just did this problem, and while I had little issue solving it I can’t help but think this solution is very, well, clunky.

Given a set of number does anyone have the computationally fastest way to solve it? Or would it just be a coders choice of which sort algorithm to use?

I feel like this could be done recursively but I’m garbage at recursive and don’t know.

To any code gods could get back to me that would be Great!

If that is all that a beginner was taught there would be a shortage of programmers. Ideally, we start at the bottom of the curve with what may well be the slowest code, but the most educational. Show a learner an advanced solution and they begin to believe that naive approaches have no value, which we know to be categorically false.

The intuitive way would be to sort, but the naive way would be to compare. Best that we exploit all that the naive approach can reveal before we toss out the baby with the bath water. Let the learner develop their own intuition.

1 Like

I’m not a code god, just a learner. But I was thinking the same thing. I’ve been looking at the notes on W3schools at the same time as working through the Codecademy course, since encountering Codecademy’s use of untaught string formatting in an earlier solution. Something I saw on there while browsing through was the max() method which I used here. It’s comparatively short and seems to work:

def max_num(num1, num2, num3):
if num1 == max(num2, num3) or max(num1, num2) == num3:
return “It’s a tie!”
else:
return max(num1, num2, num3)

First, learn how to do long division, then pick up that calculator!

Python has a great many built-in functions, among them max() and min(). One tried and true teaching method is to have students code functions which are (more or less) the equivalent of certain built-ins, on the theory that it is always good exercise to learn to do something using loops and conditionals.

Should a learner be penalized for taking the initiative to reach out to Dr Google for advice? No, in my opinion, it is to be encouraged. But there is no harm (and possibly some good?) in knowing that you can do something “the hard way”, also.

num = (100, 2, 5, 99, 24, 300, 88, 72)
print(max(num))
#Output: 300

What if num contains variables? How do we find the max() function then?

There is plenty of documentation to explain how `max()` works. The purpose of this exercise is not to study the built in function, but to create a one of our own that performs the same task, using an algorithm.

1 Like

This was my code:
def max_num(num1, num2, num3):
if num1 == num2 or num1 == num3 or num2 == num3:
return “It’s a tie!”
return max(num1, num2, num3)
It works

I believe this is the most efficient way to make a max_num function:

``````def max_num(num1,num2,num3):
nums = [num1, num2, num3]
i=0
while i<len(nums):
if (nums[i]==nums[i-1]==max(nums)):
return "It's a tie!"
else:
i+=1
else:
return max(nums)
``````

What is this? If you’re going to reach for the built-in, may as well go for the gusto,

``````if count(max(nums)) > 1:
return "It's a tie!"
return max(nums)``````
1 Like

You’re totally right. My mistake. I’m pretty new to python.

1 Like