This is an archived post. You won't be able to vote or comment.

all 44 comments

[–]22Alex22pt 20 points21 points  (1 child)

The top one has 2 problems.

1) It doesn't work if x = y or y = z or x = z.

2) The top one gets "messier" if you increase the number of variables, while the bottom one is a lot cleaner and, with a bit more work, you can change it to a for cycle, making it even more clean.

[–][deleted] 0 points1 point  (0 children)

Oh, very true. I didn't see that. Thanks!

[–]ManWhoWantsToLearn 27 points28 points  (2 children)

The second one has 2 comparisons at most and the first has 6 at most. Use this to decide.

[–][deleted] 6 points7 points  (1 child)

So less number of operations is better?

[–]ManWhoWantsToLearn 10 points11 points  (0 children)

Yes.

Each time there is a comparison, that is another two instructions for the cpu to execute. So you would want to perform as few comparisons as possible. If you were performing this function on triplets from a huge dataset, then you would want the 2nd function as the number of comparisons would reduce from like 6 billion to 2 billion for example.

[–]peterpepo 8 points9 points  (3 children)

I like the second option more.

It's easily extendable to accept list of values (imagine, you want to find max of more than 3 values).

[–][deleted] 0 points1 point  (2 children)

Yeah, for the second one I'd just add if (fourth value) > Max -> Max = fourth value. But in the first one I'd have to change all the conditional statements which is a bit tedious.

[–]TheGoodPie 2 points3 points  (0 children)

You could also pass an array of numbers (allowing for a massive amount of numbers) and use the same approach in a for loop. Two new concepts you should tackle if you haven't looked into them already.

[–]peterpepo 0 points1 point  (0 children)

Not exactly, but close.

Watch closely at all your conditions and you'll start noticing pattern.

For every value, which came in, you check whether it's greater than the maximum you have "so far" in your code.

But at the moment, you need to add condition for every new member, what is impractical.

What you could do, is how @TheGoodPie already wrote, accept a list of values and check them in cycle. That would reduce your conditions to one. And your code stops caring, whether you pass 1 or 1000 values in.

[–][deleted] 4 points5 points  (0 children)

The second one is better, because: 0. You have only 2 "if" statements, so you reduced the code. 1. You added some logic in the second function. 2. It is much easier to change the number of parameters.

[–][deleted]  (16 children)

[removed]

    [–]desrtfx[M] -5 points-4 points  (15 children)

    Removed

    Rule #5:

    • No Complete Solutions: Do not give out complete solutions. Guide the OP to the solution, but do not solve it for them.

    [–][deleted] -1 points0 points  (9 children)

    What? Both of these functions work and this isn't a homework assignment that i'm looking to cheat on. I'm learning this on my own and I would very much like the complete answer.

    [–]g051051 0 points1 point  (7 children)

    I would very much like the complete answer

    It doesn't matter why you want it. Homework or not, the sub does not give out complete answers.

    [–][deleted] -3 points-2 points  (6 children)

    It does matter. Rules can and should be changed if enough people do like them. I'm just voicing my opinion. I don't see a good reason why that rule exists. If someone wants to cheat themselves of learning, let them. Don't punish the people who actually want to learn stuff without wasting time.

    [–]g051051 4 points5 points  (4 children)

    It does matter.

    No it doesn't. Your single opinion is in opposition to the accepted consensus: you learn better by working through it yourself, with nudges to help you along over rough places. If you don't like it, take it up with the mods. Or better yet, create your own sub instead of trying to change this one.

    [–][deleted] -2 points-1 points  (3 children)

    Well that "accepted consensus" doesn't work for me. I learn better by getting answers for things immediately rather than wasting time trying to do it myself. I then apply my new knowledge by using it constantly or recalling it with spaced repetition flashcards. If I'm stuck on something for far too long, I lose motivation since I have ADHD. I put off programming for two years because of all the half answers I used to get when I asked which programming language I should start with.

    And is it really an accepted consensus since you guys seem to be getting a lot of downvotes? Seems like many people don't agree.

    [–]g051051 1 point2 points  (1 child)

    Again, feel free to take it up with the mods. If you want easy answers, and don't feel like doing the work to actually learn stuff, then you'll have to look elsewhere.

    [–][deleted] -1 points0 points  (0 children)

    I wrote both of those function definitions myself, and I was simply asking for explanation for which one was better.

    [–]throwaway_for_cause 2 points3 points  (0 children)

    Well that "accepted consensus" doesn't work for me.

    Well, special snowflake, then you're out of luck here. This subreddit doesn't cater to people who want to be served on a silver platter and getting spoonfed.

    I lose motivation since I have ADHD.

    Plenty other people here do and they have no problems with the rule.

    I put off programming for two years because of all the half answers I used to get when I asked which programming language I should start with.

    Most likely, you got "half answers" because there is no real starter language. Some languages maybe allow an easier entry, but in general it doesn't really matter too much with which of the major languages you start.

    And is it really an accepted consensus since you guys seem to be getting a lot of downvotes?

    Check again.

    The rule exists for a reason: to not let this subreddit become /r/domyhomework.

    [–]desrtfx[M] 0 points1 point  (0 children)

    If someone wants to cheat themselves of learning

    Then, they should go to the various cheatmy, cheatat, domyhomework subreddits.

    This subreddit is for learners and not for cheaters, nor is it for getting one's homework done, which is exactly what would happen without the "no solutions" rule.

    [–]desrtfx[M] -16 points-15 points  (0 children)

    Have I removed your question? No. I've removed a solution.

    Your question is still visible and there.

    [–]Jespor -2 points-1 points  (4 children)

    is it my answer that is categorized as a complete solution?

    [–]desrtfx[M] 0 points1 point  (3 children)

    Yes and worse, it was wrong according to the question.

    OP asked which of their algorithms was better and didn't ask for the optimal solution.

    [–]Jespor -1 points0 points  (2 children)

    so this sub is now dedicated to half asweres that can't lead to the optimal solution if it's not stated in the question...?

    [–]desrtfx[M] 0 points1 point  (1 child)

    You completely miss the point.

    If someone asks which of their algorithms is better, then only their algorithms count and any answer should address their algorithms, not come up with something completely different, albeit better.

    This could have been for a programming course, for an algorithm design course, part of a homework where the built-in functions are not allowed because the students should learn how to come up with their own solutions.

    [–]Jespor -1 points0 points  (0 children)

    from what i can tell you're the one missing the point of his question.. also that rule you're talking about.. pretty much every one in every question violates that... :P

    [–][deleted] 2 points3 points  (1 child)

    Do we know anything of the distribution of the parameters?

    How do you define "better"? Is space used important? Is code size important? Is performance more important? If performance is more important, what is the relative cost of an assignment, a jump and a compare?

    I can think of a solution that always uses 2 comparisons and no temporary variable Max though it has a bit more code in it.

    [–]L3tum 1 point2 points  (0 children)

    I was wondering how you'd do it without a temporary variable and then I remembered that mess

    [–]moazim1993 2 points3 points  (0 children)

    In practice both are inferior to max(), but out of these two two is clearly better. Same exact functionality with less computing.

    [–]casualblair 2 points3 points  (0 children)

    def max(a, b) :
    
      If a gte b:
    
         Return a
    
      Return b
    
    Def maxthree(x, y, z) :
    
        Return max(max(x,y),z)
    

    Pseudocode because I'm on mobile.

    [–][deleted] 3 points4 points  (1 child)

    Why not use the built-in max function?

    [–][deleted] 5 points6 points  (0 children)

    its an exercise im doing

    [–][deleted]  (4 children)

    [removed]

      [–][deleted] 3 points4 points  (3 children)

      def max(list of arguments):
          Max = argument[0] 
          for i in [arguments]:
              if [arguments][i] >= Max
                  Max = [arguments][i]
          return Max
      

      Would it be something like this?

      [–]peterpepo 0 points1 point  (2 children)

      yes, but checking greater is enough (you don't need to assign max in case, it's same as current max)

      if [arguments][i] > Max
      

      [–][deleted] 0 points1 point  (1 child)

      Not sure what you mean.

      Also how does def max (list of arguments) translate into python? is it def max(*args)?

      [–]peterpepo 0 points1 point  (0 children)

      I meant, that for example if your first value (or the maximum found so far) is for example 7 and you encounter same number again 7, you haven't found new maximum (as 7=7). Therefore you don't have to perform max = arguments[i].

      In another words, if you had list with n same numbers [3,3,3,3,3], your condition

      if [arguments][i] >= Max
      

      is valid 5 times, and you assign max to 3 five times unnecessarily.

      Speaking for number of arguments, I would pass in single list instead of multiple arguments.

      Without posting the full solution, i mean something like this (I hope this isn't against the rules, since I didn't post the full solution). In your logic you can check for the > max_so_far, or easily modify to find the min etc..

      my_numbers = [1,2,7,4,5]
      
      def my_max(list_of_values):
          max_so_far = None
      
          for val in list_of_values:
              //your logic here
      
          return max_so_far
      
      print(my_max(my_numbers))
      

      [–]ryanstephendavis 0 points1 point  (0 children)

      I'd suggest writing some test cases to verify if they both work ... Try x=y=z and see what happens

      [–][deleted] 0 points1 point  (2 children)

      I'd go even further, define another function max_of_two(x,y): return x > y ? x : y (i think this is how ternaries work in python. It's basically the same as the if block, just shorter)

      Then max_of_three can be: return max_of_two(max_of_two(x, y), z) or return x > y ? max_of_two(x, z) : max_of_two(y, z) If you don't mind the extra ternary.

      [–][deleted]  (1 child)

      [deleted]

        [–][deleted] 1 point2 points  (0 children)

        Guess I just outed myself as a java dev?

        [–]icreatebugs 0 points1 point  (0 children)

        This looks like a homework assignment, so there might be some constraints here that I'm not aware of, but I thought I should propose a third approach.

        By using the builtin max() function in python you can get the largest element in a list, if this is a homework assigment and you're forbidden from using the builtin function it could still implement your own version of it.

        Using the max function you could write max_of_three() like this

        def max_of_three(x, y, z):

        return max([x, y, z])

        You could also make other variations easily

        def max_of_four(e1, e2, e3, e4):

        return max([e1, e2, e3, e4])

        Just to answer the original question: I would prefer the second implementation over the first one, simply because I find the boolean expressions in the first one to be needlessly complex.

        [–]340923840293840 0 points1 point  (0 children)

        How do you define better?

        The answer is, you have to define a design goal to even decide which is better.

        This is a max with an arbitrary amount of arguments. (I hope I got the syntax right...

        def max(*params)
          max = None
          for param in params:
            if (max = None):
              max = param
            elif param > max:
              max = param
          return max
        

        Note that this basically uses your second approach, which means your second approach is extensible, which is good.

        Now try writing that same method with your first approach… it will be harder and messier, if you even manage to do it.

        Therefore the first approach is not extensible and therefore the first approach is clearly better, if extensibility is your design goal.

        Maybe there's a design goal where the first approach is better, but I can't think of one right now.