Attached: file.png (261x467, 26K)
Describe this image as best as you can
Adam Roberts
Ian Hall
no
Eli Sanders
Well-intended, above all.
Andrew Green
Misleading function name.
Matthew Morgan
return the largest number, is it that complicated
Christian James
It does its job
Henry Gray
elif should be replaced by if
Austin Ortiz
OOP summarized.
Benjamin Rivera
Pain in the ass
Julian Thomas
was not this posted on sci a few days ago?
Landon Walker
>largest(2, 2, 3) returns [2, 2]
Jeremiah Hall
First, it should be called "most" instead of largest. Second, there's only 3 more returns than there needs to be, so it's not too bad. 7.5/10
Luke Walker
sort([x,y,z], reverse=True])[0]
Asher Gray
>returns two different types (string and list)
>elif when if will do, since it's all returns
>10x longer than it needs to be
>needlessly complicated
>only accepts 3 values instead of an arbitrary list
>if x = y, but z is larger, then it still returns [x,y]
>if x = z, but y is larger, then it still returns [x,z]
>if y = z, but x is larger, then it still returns [y,z]
Luke Smith
Henry Harris
No it doesn't. It returns [x, y] when z > x == y.
Julian Carter
Pain
Agony
James Gonzalez
This is a pure functional program you utter retard
Joseph Cruz
You don't get a list in that case.
>>> def largest(x,y,z):
l = max([x,y,z])
return [l] * [x,y,z].count(l)
...
>>> print(largest(3,2,3))
[3, 3]
Austin Richardson
Why?
Nathaniel Torres
Nothing came as close to the elegance of this in the /sci/ thread.
Ethan Sanders
Wrong.
In Haskell,
largest x y z = max x (max y z)
Juan Long
Proper way to do this: (NB: always returning arrays to make it less retarded)
if(x == y) {
return y == z ? [x,y,z] : ( x > z ? [x,y] : [z])
} else if (x > y) {
return x == z ? [x,z] : (x > z ? [x] : [z])
} else {
return y == z ? [y,z] : (y > z ? [y] : [z])
}
Sebastian Foster
Don't listen to him.
It makes it harder to read.
Lucas Diaz
Ok, I suck. Time to study and left this place...
Nicholas Green
incorrect
x=5, y=5, z=10
result would be [5, 5]
Camden Campbell
someone needs to learn loop logic.
Owen Roberts
This. It returns either doubles or triples and if all numbers are unique it returns the largest value.
Jack Reed
>not unrolling your loops completely for optimal performance
Hunter Evans
impossible if the number of iterations is determined at runtime
Luke Evans
Will that return an array if multiple at the same, say 5, 5, 5?
Logan Miller
>not coding exclusively in assembly for even better performance
Ian Lewis
Dynamic typing was a mistake
Austin Howard
types are a social construct, don't be a bigot shitlord. an int can identify as a float if it wants to.
Julian Wright
>coding in c+=
Anthony Jackson
non-functional case tree
Cooper Watson
100% this
a programmer decides mentally what types each of their variables should be when they write code. throwing away that information by automatically deducing types at runtime is fucking stupid
>hurr hurr noobs will get confused if they have to declare types themselves! we should make the interpreter assign types automatically, making it harder to actually debug your code!
what a fucking mess. who thought this was a good idea?
and don't get me started on weak typing or the absolute abomination known as duck typing
Carter Russell
>duck typing
the hell is that
Jaxson Bailey
difficult to debug and use since it fucking returns differently typed values.
Carson Ward
Types : the training wheels of programming languages.
Aiden Robinson
Stop posting this thread and fuck off back to /sci/
Nicholas Price
everyone says this until they have to maintain a 500k LOC python abomination with a variety of undocumented custom shit.
Your code completion engine could at least help you a little in strongly typed languages because they actually provide useful type information in the source code, but you're 100% fucked in dynamic languages
James Campbell
that's what docstrings are for though, really. I honestly haven't seen any major project with significant type issues.
Nolan Williams
>docstrings as a replacement for type information
i strongly dislike that idea, but it would work half decently if the programmers were extremely vigilant with including type information in their docstrings in the correct format, all the time.
But this unfortunately doesn't happen in industry, especially on a time crunch before a deadline
Robert Bailey
It returns variables that are the same value, or the one that is the largest.
Isaac Ross
I'd also add type checking at all public interface entry points of your modules. It assuages the issue to some degree.
Charles King
>if it looks like a duck and quacks like a duck, then it's a duck
Christopher Wood
>i strongly dislike that idea
meh. it just comes with the territory in these shit langs like javascript, etc.
metatyping and shiet.
Tyler Gomez
>if the programmers were extremely vigilant with including type information in their docstrings in the correct format, all the time.
literally what? if the shit doesn't return what is documented then there is a bug anyhow. this is literally a nonissue. the only real issue here is that someone's CI doesn't make sure the documentation has syntax errors.
Aaron Green
>pajeet code
>functional
Whatever you say, Rajesh...
David Rivera
CompSci degree
Colton Thompson
[ $# -lt 1 ] && exit -1
max=$1
shift
for n in $@; do
if [ $n -eq $max ]; then
max=$n
let c++
elif [ $n -gt $max ]; then
max=$n
c=0
fi
done
for (( i=0; i
Charles Brooks
>Not knowing what he means when he says "functional"
Time to end your life
Hunter Miller
I don't think that word means what you think it means user.
Jaxson Jenkins
Nice
David Wood
...
Noah Brooks
int ret = x;
if (y > ret)
ret = y;
if (z > ret)
ret = z;
return ret;
Oliver Cook
This should be a 2 argument function to reduce it to 3 checks, or take a list as argument. Not this garbage.
David Miller
not him, that just returns the largest of 3 numbers. normal haskell would be
largest ls = filter (== maximum ls) ls
Jayden Edwards
Guys.
>x > y > z
This doesn't actually work correctly, does it?
Sebastian Lewis
Look again.
largest(1, 1, 2) == [1, 1]
Liam Martinez
perfect
Connor Torres
>cs grads
Luke Thompson
That doesn't do half of what the function is doing (returning a list with multiple values if it occurs multiple times).
Liam Williams
function largest(x, y, z) {
let ret = [x];
for (const val of [y, z]) {
if (val === ret[0]) {
ret.push(val);
} else if (val > ret[0]) {
ret = [val];
}
}
return ret;
}
Isaac Morris
template
T largest(const std::vector& v) {
return std::max_element(v.begin(), v.end());
}
or any container that provide .begin() and .end(), or the pointer at the beginning of the array and a pointer one element past the last if you use C-style arrays
Or, naive method if you want to do it by hand
template T largest(std::vector a) {
auto res = a[0];
for(auto n : a)
if(n > res)
n = res;
return res;
}
And if you insist on the three integer arguments
int largest(int a, int b, int c) {
if(a > b)
return (a > c) ? a : c;
return (b > c) ? b : c;
}
Hunter Davis
int largest(int x, int y){
if(x > y){
return x;
}else if(y > x){
return y;
}else{
return x;
}
}
do largest(largest(x, y), z); to get result of 3 variables
Wyatt Nelson
Is this code from Yandere Simulator?
Lincoln Morgan
You can save lines and speed by writing it like so:
int largest(int x, int y) {
if(x >= y) {
return x;
}
return y;
}
Nathaniel Young
(or just if(x > y), it works too)
Oliver Peterson
def largest(*args):
l = max(args)
return [l] * args.count(l)
Levi Price
You can make it unary if you wanted, but I wanted to keep the intent clear. Something as trivial as this shouldn't cause performance issues.
if (x > y)? return x : return y;
Luke Hall
I mean ternary lol
Ethan Butler
Even better. I don't know that I've ever formatted the args in such a retarded way as the OP, so I didn't even think of doing it that way.
Hunter Rogers
Yep indeed, and even shorten it even more
return (x > y) ? x : y;
But I see your point
Jonathan Scott
Jaxson Edwards
Thinking about it a little more, why the fuck would you need a list with 2/3 objects if they were "equal?" If they're primitives, it's even more pointless.
Caleb Scott
def largest(x, y, z):
values = [x, y, z].sort()
result = [ values[-1] ]
for i in results[:-1]:
if i == result[0]:
result.append(i)
# you could just return a list but whatever
if len(result) == 1:
return result[0]
return result
Noah Watson
what in gods name are you trying to do here
Levi Allen
Exactly what the function intends. But I admit is the most elegant and is the most efficient (although it doesn't do that retarded thing where it changes int to list if the value occurs more than once, but that's fixable without increasing complexity).
Julian Russell
how can a functional advocate be this stupid?
you're just making it harder for the rest of us
Luke Thomas
/sci/ loves to shit all over CS and call it easy but couldn't program their way out of a paper bag
Jose Parker
Actually I'm going to fix to make it exactly what OP wants and still be the most efficient, single-iteration algorithm:
def largest(x, y, z):
result = [ x ]
for val in [y, z]:
if val == result[0]:
result.append(val)
elif val > result[0]:
result = [ val ]
if len(result) == 1:
return result[0]
return result
Kayden Howard
beautiful
Hunter Sanchez
Post #66111431: 0.767340898514 usec
Post #66113263: 0.682909965515 usec
Post #66113455: 0.775645017624 usec
Isaiah Davis
"Works but there is a simpler way".png
Kevin Sullivan
Sure, but does 2 iterations, one for max and one for count. From a purely CS algorithm-on-paper standpoint, is the best.
Adam Harris
I like it
def largest(*args):
result = [ args[0] ]
for val in [args[1:]:
if val == result[0]:
result.append(val)
elif val > result[0]:
result = [ val ]
if len(result) == 1:
return result[0]
return result
Benjamin Nelson
From an algorithm-on-paper standpoint, your function is a nightmare hellscape compared to and . It's slower, more bloated (400% bigger!), more complicated to debug and modify, etc
Reinventing the wheel isn't a plus.
Dominic Williams
>It's slower, more bloated (400% bigger!), more complicated to debug and modify
All of these issues are language-dependent and have zero to do with specification. From an algorithmic standpoint, it's the best because it only iterates once. Both the other iterate twice.
Your concerns have to do with implementation. You don't seem to know what algorithm-on-paper means.
Jose Long
>All of these issues are language-dependent and have zero to do with specification
The problem in question is python. If you aren't going to use the features of python, why bother?
>From an algorithmic standpoint, it's the best because it only iterates once. Both the other iterate twice. Your concerns have to do with implementation. You don't seem to know what algorithm-on-paper means.
My concerns are with efficiency. Having less iterations is good, but not if the result is a slower, bloated, buggier piece of code.
Ryder Diaz
just uh write a simple typesystem. like C but less retard. inside your code, get it?
Jose Gonzalez
There's a missing close bracket on line 3
for val in [args[1:]:
Also
>>> def largest(*args):
... result = [ args[0] ]
... for val in [args[1:]]:
... if val == result[0]:
... result.append(val)
... elif val > result[0]:
... result = [ val ]
... if len(result) == 1:
... return result[0]
... return result
...
>>> largest(1,2,3)
(2, 3)
Cameron Allen
>The problem in question is python.
I never said my solution was the most efficient in Python, I said it was the most efficient on-paper algorithm.
Algorithmia doesn't give 2 shits what language you're using. If you're here to argue that using sort(), max() and count() are easier ways and your boss at whatever consulting company you work for will be happier, and even that those others run faster, I'll agree. But I'm thinking about something I could ask my students to do. Like tell them the problem is solvable in 1 iteration without using built-in functions and ask them to come up with a solution.
But hey mate, if you're going to argue efficiency in terms of measurable runtime, I'm going to ask why are you even using Python in the first place.
Jordan Thomas
>conditionally returns different data types based on the very same condition you're trying to test
python was a mistake
Gabriel Peterson
If you are teaching your students to do things in a more complicated, buggy way that isn't expandable and harder to debug, then you are the problem with modern programming.
Joseph Anderson
Thanks, fixed. The result was on line 3, where args[1:] was being packed into another list, and the eval was basically comparing a list to an int.
def largest(*args):
result = [ args[0] ]
for val in args[1:]:
if val == result[0]:
result.append(val)
elif val > result[0]:
result = [ val ]
if len(result) == 1:
return result[0]
return result
>>>largest(1,2,3)
3
>>>print(largest(1,2,4,3,7,2,7)
[7, 7]
I'm teaching my students to create algorithms, not to create algorithms using Python. My algorithm is better, even if in this particular language it runs slower.
Also, you're using an input of size 3. If you're going to be a little bitch about runtime, give it something in the order of 10^4 that second iteration and tell me how that second iteration is working for you.
Luke Barnes
You should teach the cost of operations too and not get them blinded by the sole fact that it iterates once. There's a ton of ifs goddamnit, it's an unreadable, inefficient, overly complicated and utterly useless function. You should know from Python that there should ideally be one obvious way to do things, and yours isn't mate. Especially in a educational context.
Levi Wood
Mate have you ever opened an algorithms book? Most of the ones we still use were written when Lisp was the big thing in CS. They never care about size as long as it provides the best efficiency, and they definitely don't care which language some codemonkey is going to implement it on.
>inefficient
Can't help but notice that you're still judging my algorithm's efficiency based on a size 3 input...
Go on, fire up /dev/random, fill an array with 10 million values and compare all 3 solutions. Because my solution is slower because max() and count() have C implementations and mine is basically pure python which is slower, but when you start dealing with larger inputs that gets less and less important.
Jackson Turner
No I'm judging it on the cost of branches and list insertions. The obvious way is to do a lt on each entry, keeping the highest number in one variable and incrementing a counter on identicals, reseting it to 1 on new max. Then return [max]*count if count isn't 1 otherwise max.
The len for arbitrarily large arrays is O(n) too, so that ain't good.