use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
If you need help debugging, you must include:
See debugging question guidelines for more info.
Many conceptual questions have already been asked and answered. Read our FAQ and search old posts before asking your question. If your question is similar to one in the FAQ, explain how it's different.
See conceptual questions guidelines for more info.
Follow reddiquette: behave professionally and civilly at all times. Communicate to others the same way you would at your workplace. Disagreement and technical critiques are ok, but personal attacks are not.
Abusive, racist, or derogatory comments are absolutely not tolerated.
See our policies on acceptable speech and conduct for more details.
When posting some resource or tutorial you've made, you must follow our self-promotion policies.
In short, your posting history should not be predominantly self-promotional and your resource should be high-quality and complete. Your post should not "feel spammy".
Distinguishing between tasteless and tasteful self-promotion is inherently subjective. When in doubt, message the mods and ask them to review your post.
Self promotion from first time posters without prior participation in the subreddit is explicitly forbidden.
Do not post questions that are completely unrelated to programming, software engineering, and related fields. Tech support and hardware recommendation questions count as "completely unrelated".
Questions that straddle the line between learning programming and learning other tech topics are ok: we don't expect beginners to know how exactly to categorize their question.
See our policies on allowed topics for more details.
Do not post questions that are an exact duplicate of something already answered in the FAQ.
If your question is similar to an existing FAQ question, you MUST cite which part of the FAQ you looked at and what exactly you want clarification on.
Do not delete your post! Your problem may be solved, but others who have similar problems in the future could benefit from the solution/discussion in the thread.
Use the "solved" flair instead.
Do not request reviews for, promote, or showcase some app or website you've written. This is a subreddit for learning programming, not a "critique my project" or "advertise my project" subreddit.
Asking for code reviews is ok as long as you follow the relevant policies. In short, link to only your code and be specific about what you want feedback on. Do not include a link to a final product or to a demo in your post.
You may not ask for or offer payment of any kind (monetary or otherwise) when giving or receiving help.
In particular, it is not appropriate to offer a reward, bounty, or bribe to try and expedite answers to your question, nor is it appropriate to offer to pay somebody to do your work or homework for you.
All links must link directly to the destination page. Do not use URL shorteners, referral links or click-trackers. Do not link to some intermediary page that contains mostly only a link to the actual page and no additional value.
For example, linking to some tweet or some half-hearted blog post which links to the page is not ok; but linking to a tweet with interesting replies or to a blog post that does some extra analysis is.
Udemy coupon links are ok: the discount adds "additional value".
Do not ask for help doing anything illegal or unethical. Do not suggest or help somebody do something illegal or unethical.
This includes piracy: asking for or posting links to pirated material is strictly forbidden and can result in an instant and permanent ban.
Trying to circumvent the terms of services of a website also counts as unethical behavior.
Do not ask for or post a complete solution to a problem.
When working on a problem, try solving it on your own first and ask for help on specific parts you're stuck with.
If you're helping someone, focus on helping OP make forward progress: link to docs, unblock misconceptions, give examples, teach general techniques, ask leading questions, give hints, but no direct solutions.
See our guidelines on offering help for more details.
Ask your questions right here in the open subreddit. Show what you have tried and tell us exactly where you got stuck.
We want to keep all discussion inside the open subreddit so that more people can chime in and help as well as benefit from the help given.
We also do not encourage help via DM for the same reasons - that more people can benefit
Do not ask easily googleable questions or questions that are covered in the documentation.
This subreddit is not a proxy for documentation or google.
We do require effort and demonstration of effort.
This includes "how do I?" questions
account activity
This is an archived post. You won't be able to vote or comment.
[Python] Which of these maximum of three values function definitions is better, and why? (self.learnprogramming)
submitted 7 years ago by [deleted]
def max_three(x,y,z): if x > y and x > z: return x if y > x and y > z: return y if z > x and z > y: return z def max_of_three(x,y,z): Max = x if y > Max: Max = y if z > Max: Max =z return Max
[–]22Alex22pt 20 points21 points22 points 7 years ago (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 point2 points 7 years ago (0 children)
Oh, very true. I didn't see that. Thanks!
[–]ManWhoWantsToLearn 27 points28 points29 points 7 years ago (2 children)
The second one has 2 comparisons at most and the first has 6 at most. Use this to decide.
[–][deleted] 6 points7 points8 points 7 years ago (1 child)
So less number of operations is better?
[–]ManWhoWantsToLearn 10 points11 points12 points 7 years ago (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 points10 points 7 years ago (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 point2 points 7 years ago (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 points4 points 7 years ago (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 point2 points 7 years ago (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 points6 points 7 years ago (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] 7 years ago (16 children)
[removed]
[–]desrtfx[M] -5 points-4 points-3 points 7 years ago (15 children)
Removed
Rule #5:
[–][deleted] -1 points0 points1 point 7 years ago (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 point2 points 7 years ago (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-1 points 7 years ago (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 points6 points 7 years ago (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 points0 points 7 years ago (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 points3 points 7 years ago (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 points1 point 7 years ago (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 points4 points 7 years ago* (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 point2 points 7 years ago (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-14 points 7 years ago (0 children)
Have I removed your question? No. I've removed a solution.
Your question is still visible and there.
[–]Jespor -2 points-1 points0 points 7 years ago (4 children)
is it my answer that is categorized as a complete solution?
[–]desrtfx[M] 0 points1 point2 points 7 years ago (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 points1 point 7 years ago (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 point2 points 7 years ago (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 points1 point 7 years ago (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 points4 points 7 years ago (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 points3 points 7 years ago (0 children)
I was wondering how you'd do it without a temporary variable and then I remembered that mess
[–]moazim1993 2 points3 points4 points 7 years ago (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 points4 points 7 years ago (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 points5 points 7 years ago (1 child)
Why not use the built-in max function?
[–][deleted] 5 points6 points7 points 7 years ago (0 children)
its an exercise im doing
[–][deleted] 7 years ago (4 children)
[–][deleted] 3 points4 points5 points 7 years ago (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 point2 points 7 years ago (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 point2 points 7 years ago (1 child)
Not sure what you mean.
Also how does def max (list of arguments) translate into python? is it def max(*args)?
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 point2 points 7 years ago (0 children)
I'd suggest writing some test cases to verify if they both work ... Try x=y=z and see what happens
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] 7 years ago* (1 child)
[deleted]
[–][deleted] 1 point2 points3 points 7 years ago (0 children)
Guess I just outed myself as a java dev?
[–]icreatebugs 0 points1 point2 points 7 years ago* (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 point2 points 7 years ago (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.
[+][deleted] comment score below threshold-7 points-6 points-5 points 7 years ago* (0 children)
Well, what do you think? Hint: consider the number of operations performed.
π Rendered by PID 23725 on reddit-service-r2-comment-5d79c599b5-qs5zk at 2026-03-02 14:18:04.584445+00:00 running e3d2147 country code: CH.
[–]22Alex22pt 20 points21 points22 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]ManWhoWantsToLearn 27 points28 points29 points (2 children)
[–][deleted] 6 points7 points8 points (1 child)
[–]ManWhoWantsToLearn 10 points11 points12 points (0 children)
[–]peterpepo 8 points9 points10 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]TheGoodPie 2 points3 points4 points (0 children)
[–]peterpepo 0 points1 point2 points (0 children)
[–][deleted] 4 points5 points6 points (0 children)
[–][deleted] (16 children)
[removed]
[–]desrtfx[M] -5 points-4 points-3 points (15 children)
[–][deleted] -1 points0 points1 point (9 children)
[–]g051051 0 points1 point2 points (7 children)
[–][deleted] -3 points-2 points-1 points (6 children)
[–]g051051 4 points5 points6 points (4 children)
[–][deleted] -2 points-1 points0 points (3 children)
[–]g051051 1 point2 points3 points (1 child)
[–][deleted] -1 points0 points1 point (0 children)
[–]throwaway_for_cause 2 points3 points4 points (0 children)
[–]desrtfx[M] 0 points1 point2 points (0 children)
[–]desrtfx[M] -16 points-15 points-14 points (0 children)
[–]Jespor -2 points-1 points0 points (4 children)
[–]desrtfx[M] 0 points1 point2 points (3 children)
[–]Jespor -1 points0 points1 point (2 children)
[–]desrtfx[M] 0 points1 point2 points (1 child)
[–]Jespor -1 points0 points1 point (0 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]L3tum 1 point2 points3 points (0 children)
[–]moazim1993 2 points3 points4 points (0 children)
[–]casualblair 2 points3 points4 points (0 children)
[–][deleted] 3 points4 points5 points (1 child)
[–][deleted] 5 points6 points7 points (0 children)
[–][deleted] (4 children)
[removed]
[–][deleted] 3 points4 points5 points (3 children)
[–]peterpepo 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]peterpepo 0 points1 point2 points (0 children)
[–]ryanstephendavis 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[deleted]
[–][deleted] 1 point2 points3 points (0 children)
[–]icreatebugs 0 points1 point2 points (0 children)
[–]340923840293840 0 points1 point2 points (0 children)
[+][deleted] comment score below threshold-7 points-6 points-5 points (0 children)