Quantcast
[ 3 / biz / cgl / ck / diy / fa / g / ic / jp / lit / sci / tg / vr ] [ index / top / reports / report a bug ] [ 4plebs / archived.moe / rbt ]

Maintenance is complete! We got more disk space.
Become a Patron!

/sci/ - Science & Math


View post   

[ Toggle deleted replies ]
File: 14 KB, 261x467, 543254325324.png [View same] [iqdb] [saucenao] [google] [report]
9766284 No.9766284 [Reply] [Original] [archived.moe]

>> No.9766293

>>9766284
Max if distinct, duplicates with multiplicity otherwise.

>> No.9766296

not science or math

>> No.9766306

>>9766284
a waste of time and code, most programming languages should have something like a "max" function that returns the largest value in an array

>> No.9766312

>>9766284
Is this really the most efficient way to write that function?

>> No.9766323

>>9766312
For modern computers, it's way less lines of code to just sort them and output the final value in the array. It's only three numbers and a bubble sort is better.

>> No.9766398
File: 16 KB, 480x368, deeply concerned.jpg [View same] [iqdb] [saucenao] [google] [report]
9766398

>>9766284
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

>> No.9766404

int ret_max (int a, int b){
if(a>b)
return a;
return b;
}

int ret_max(int a, int b, int c){
return ret_max(a, ret_max(b,c));
}

//This good enough?

>> No.9766407

>>9766312
Absolutely not. It's a thoroughly retarded way of doing it. It's also plain wrong.

>> No.9766410

How in the world is that a valid typing for a case statement

Perhaps I need to look at Python more, that's wild

>> No.9766419

>>9766404
int max(int a, int b) return (a>b)?a:b;
int max3(int a, int b, int c) return max(a, max(b, c));

But this version isn't equivalent to the abomination in OP.

>> No.9766424

bool opFaggot = true

>> No.9766430

>>9766284
Also, it's just plain wrong, isn't it?
I mean
>if x==y
>return [x, y]
nothing about that implies that they are larger than z. for {x=1,y=1,z=15} this fails to return the largest.

>> No.9766442

>>9766323
you can just write three if statements using 'and' and output the final value, or is that less efficient?

>> No.9766446

>>9766404
template<typename T1, typename T2>
constexpr std::common_type_t<T1, T2> max(T1 a, T2 b){
return a>b ? a : b;
}
template<typename T1, typename T2, trpename... Ts>
constexpr auto max(T1 a, T2 b, Ts... cs){
return max(max(a, b), cs...);
}

//this is better :^)

>> No.9766449

>>9766410
python doesn't have case/switch style flow control. the closest it has is packaging possible values as keys in a dictionary and then indexing the dictionary with your input

see: https://bytebaker.com/2008/11/03/switch-case-statement-in-python/

why doesn't it have proper case switching? probably the same reason it doesn't have do/while and lambdas are hamstrung: the developers think the syntax you'd need is ugly or unpythonic so they refuse to implement it

>> No.9767585

>>9766306
If you had read the function, you would realize it doesn't just calculate the max.
Or maybe you just meant that the bottom 6 cases should have been replaced with return max(x, y, z), which is probably true.

>> No.9767601

>>9767585
It's still sloppy cause it's not upgradable code. Add a W or more variables and you have to recode everything with an exponentially growing code.

>> No.9767633

>>9766284
>largest(5,5,10e6)
returns 5

>> No.9767678

>>9767633
Actually it returns [5, 5]

>> No.9767694

>>9766284
shit-tier code

fuck off, pajeet

>> No.9767725

>>9766284

def largest(x, y, z):
return max([x, y, z])

>> No.9767758

>>9767725
Doesn't replicate the function's behavior, returning any duplicates takes precedence over returning max.

>> No.9767764
File: 86 KB, 1920x1080, maxresdefault.jpg [View same] [iqdb] [saucenao] [google] [report]
9767764

>>9766284
Numale-tier language

>> No.9767899

unpythonic

def largest(*args):
return [x for x in args if x>=max(args)]

>> No.9767927

>>9766410
Python is dynamically typed

>> No.9767929

>>9767601
Best bet would probably be just to do max(array) and then count the number of entries that have that value. Replicates all behavior and doesn't have a retarded else if tree.

>> No.9768000

>>9766449
Python does have a "while" loop.

>> No.9768026

def largest(array): return filter(lambda x, max_x=max(array): x == max_x, array)

>His language of choice can't solve it with one-liner

>> No.9768272

>>9766284
Black, white, green, and red

>> No.9768288

>>9766312
No but what do you expect of cs majors?
[math]
\rm
def ~ largest(x,~y,~z): \\
~~~if ~ x == y: \\
~~~~~~if ~ y==z: ~ return ~ [x,~y,~z]; \\
~~~~~~return ~[x,~y]; \\
~~~if ~ y==z: ~ return ~ [y,~z]; \\
~~~if ~ x>y: \\
~~~~~~if ~ x>z: ~ return ~ x; \\
~~~~~~return ~ z; \\
~~~if ~ y>z: ~ return ~y; \\
~~~return ~ z; \\
[/math]

>> No.9768459

>>9768288
>writes an equally shitty and broken function
you're a cs major too i take it?

>>9767899
this guy gets it

>> No.9768466

>>9768459
>not understanding preserving behavior

>> No.9768468

>>9768000
You didn't read. I said "do/while". It's a while loop that checks its exit conditions at the end of the loop instead of the start.

>> No.9768483

>>9766449
>the closest it has is packaging possible values as keys in a dictionary and then indexing the dictionary with your input
That's what gcc does with a switch statement anyway.

>> No.9768551

>>9766284
is that python or what is it

>> No.9768561

>>9768288
How does it feel to be retarded?

>> No.9768585

>>9766284
The result of years of idiot programmers who hate mathematics, telling people who hate mathematics and want to get into programming "no, you don't need mathematics in programming".

>> No.9768680 [DELETED] 

>>9766293
this

>>9766312
it actually might be, even though it's ugly. remember that python does lazy boolean evaluations, and that you'd probably have to evaluate most of those comparisons in a sorting algorithm anyway. calling sort() or other other methods also involves overhead. you could in-line a sorting algorithm, but i think it's most efficient as op wrote it.

>> No.9768683

>>9766293
this

>>9766312
it actually might be, even though it's ugly. you'd probably have to evaluate most of those comparisons in the worst case of a sorting algorithm anyway. calling sort() or other other methods also involves overhead. you could in-line a sorting algorithm, but i think it's most efficient as op wrote it.

>> No.9768724

>>9768483
Yeah but you don't have to write the dictionary yourself.

>> No.9768735

>>9768683
>you'd probably have to evaluate most of those comparisons in the worst case of a sorting algorithm anyway.
cont.
or at least for fixed n=3, it's probably not terribly inefficient. to implement a general sorting algorithm, you'd have to set up another list object, which might not be worth it if you're only comparing three numbers. might be able to cut out a few of those comparisons, but the OPs function is hardly the worst way to do this if you're really only ever going to have three inputs.

>> No.9768823

>>9768288

of course, this is the cleanest way.

>> No.9768905

A defined functiin which takes 3 int variables and checks to see if they are equal and if not which is the greatest, returning the largest variable or variables

>> No.9769165

>>9766312
as written the proper way to do it is >>9768288 which takes 3 comparisons on average to complete

>>9767601
as retarded as it sounds sometime it's acceptable to overload the same function multiple times when the generalization is less efficient for small or special input sizes

>> No.9769193

>>9768683
Don't be stupid. The function isn't just inefficient, its output is wrong.

>> No.9769282 [DELETED] 

>>9766284
Retarded

>> No.9769296 [DELETED] 
File: 9 KB, 211x239, 1513971000563.png [View same] [iqdb] [saucenao] [google] [report]
9769296

>>9767758
>largest()
>"I-it's meant to l-look for duplicates..."

>> No.9769300 [DELETED] 

>>9768468
Whats the benefit of that. You can exit anywhere at any time in any kind of loop with break.

>> No.9769314

>>9766284
CS capstone project

>> No.9769315

>>9766323
You don't even need to sort the list.
You can pass through the unsorted list just once and keep track of the largest element as well as the multiplicity.
O(n) is better than O(nlog(n))
I know it doesn't matter for 3 elements but for larger lists, sorting is a waste.

>> No.9769538

array[array==max(array)]
R is cutest language

>> No.9769844

>>9768468
This behavior can be achieved with a while/else loop and half a brain

>> No.9769846

>>9768724
Yeah but you can use anything as a key, so its more like pattern matching

>> No.9769848

>>9769538
APLs don't count for problems like this, the sheer terseness and expressability is unfair

>> No.9769862
File: 30 KB, 311x429, cs graduate.png [View same] [iqdb] [saucenao] [google] [report]
9769862

>>9766284

>> No.9769874

>>9766284
What is the output of
largest (9, 7, 7)
?

>> No.9769890

>>9769874
The output would be: [y, z]

>> No.9769909

>>9769890
Yeah, I mistakenly assumed the code was wanting to return the largest number.

>> No.9769942
File: 169 KB, 272x338, sck.png [View same] [iqdb] [saucenao] [google] [report]
9769942

>>9769909
No, obviously there is no largest if any two elements are equal and therefor it would be quite illogical to assume that condition can not happen. The code is just covering all of the bases.

>> No.9769984

>>9768288
Is this bait?
What if x==z.?
Fucking baboon, next time you shit on the best major in the univserse kys.

>> No.9769997

>>9766312
max([x,y,z]) and it’s a built in function for python

>> No.9770004

>>9769997
0/10, only returns singular values even if multiple values match the maximum

>> No.9770009

Retarded.

Why would you return a string if there is only one but a list of there are multiple? Wouldn't you want the same object type regardless?

[math]
>>> def largest(x,y,z):
... l = max([x,y,z])
... return [l] * [x,y,z].count(l)
...
>>> print(largest(3,2,3))
[3, 3]

[/math]

Or for your retarded version

[math]
>>> def largest(x,y,z):
... l = max([x,y,z])
... if [x,y,z].count(l) > 1:
... return [l] * [x,y,z].count(l)
... else:
... return l
...
>>> print(largest(3,2,1))
3
[/math]

>> No.9770014

>>9769844
of course it can. it's also a kludge and it requires you to declare it as [while True:], which is just bad practice, and requiring the user to manually control the loop logic with an if block at the end of the loop is just asking for bugs

>>9769300
it's useful any time you want your exit conditions to be dependent on the state of a variable created during the loop. if your loops always check at the start of the loop, you have a couple major options:

1. perform a single round of the loop outside the loop so the variable exists and is queued up for the first loop pass.
2. put an if/break statement at the end of the loop
3. use a flag variable that gets checked and modified as necessary at the end of the loop

None of these cases are ideal. 1 duplicates your work, always a bad idea. 2 and 3 are two means to the same basic end, and they both work, but you're cluttering up your code unnecessarily.

it's a niche issue, certainly, but it's not one without its uses

>> No.9770019

>>9770014
and to sum it up even more simply: A do-while block ensures that the encapsulated code is always executed at least once, while you cannot make assumptions about whether the content of a while-do block has been executed.

>> No.9770024
File: 4 KB, 259x194, 1111.jpg [View same] [iqdb] [saucenao] [google] [report]
9770024

>>9770019
spaghetti monster.
Only use for loops.

>> No.9770298
File: 25 KB, 720x304, Star_Wars_The_Force_Awakens_2015_HD-CAM_XViD_HQMic_AC3-CPG.avi_snapshot_01.48.02_[2015.12.23_21.18.13].jpg [View same] [iqdb] [saucenao] [google] [report]
9770298

>mfw /sci/ doesn't do things the pythonic way

>> No.9770336

>>9766284
answer = x ;
if answer < y { answer = y } ;
if answer < z { answer = z } ;

Boring and simple, but effective and accurate. If you must, you can include a count of how many had the same value, or save the names of the largest values, by simple modification of that code.

>> No.9771315

>>9769165
>as retarded as it sounds sometime it's acceptable to overload the same function multiple times when the generalization is less efficient for small or special input sizes

this. but there's so much overhead in python anyway it's probably not worth it.

i know it's a stupid nonsense function that op wrote, but it might actually be better if they used a tuple containing the number and its multiplicity.

>> No.9771393

>>9768466
>not understanding the joke

>> No.9771571

>>9766284
A function which compares the values of three variables, and returns the largest. For variables with tied values, it returns a list including both of them. I'd be worried about the parsing of the x == y == z case, as it might actually check if x == true if Python doesn't support the syntax he used.
Looks like it uses an "elif" at the end instead of an else. In the case that elif is actually necessary, which I doubt, an else should be put below it to throw an error so the code returns the same type every time.
>>9766312
Functionally? Yes, though it's ugly.
>>9766306
>>9766442
Won't do the same thing.
>>9768288
>duplicate y == z check
>doesn't even check all cases
The code in OP is superior, engie.

>> No.9771640

>>9769862
this.

>> No.9771650

What bothers me most isn't the poor style or unnecessary if statements, it's that the return type isn't predictable.

>> No.9771883

>>9766284
>return type changes based on arguments

>> No.9771912

>>9771650
This. You have to check if it's a str or list after every return. That's why the first example in >>9770009 is the best answer.

>> No.9771943 [DELETED] 

>>9770024
>>9770019
Yes this is dumb.
Use for loops for every possible situation reliant on number analysis. Use while loops for situations where you want a result without knowing how many steps it will take to get it, so

while(!known){
...
if (satisfying condition){
known = true;
}
}
accomplishes just about anything you'd need in While looping.

>> No.9771958 [DELETED] 

>>9766284
There is little value in returning a list of duplicates. If you just simply return a single largest value, you get an answer. If you return duplicate largest values, there is no index indentity for the values from the original input, so you still only have one usable answer. Sure, X and Z were both the largest, but the return doesn't say which one is X and which one is Z, so its irrelevant to the scope of the function to return duplicates when the return is just [largest, largest]

If the OP code was meant to perform a useful function, it doesn't and is shit.

>> No.9771965

>>9771883
>>return type changes based on arguments
Perl (which this isn't) can do this.
Technically, Perl functions always return an array, just it's usually empty or has only one element.

>> No.9772028 [DELETED] 

>>9766284
Why wouldn't you just write the function to take a list of arguments.

float largest(float[] values){
》float check;
》float max;
》for (int i=0; i<values.length; i++){
》》check = values;
》》for (int k=i; k<values.length; k++){
》》》if (k != i){
》》》》if (check < values[k]){
》》》》》max = values[k];
》》》》》i = k-1;
》》》》》break;
》》》》}
》》》}
》》}
》}
》return max;
}

>> No.9772036 [DELETED] 

>>9772028

float largest(float[] values){
》float check;
》float max;
》for (int i=0; i<values.length; i++){
》》check = values;
》》for (int k=i; k<values.length; k++){
》》》if (k != i){
》》》》if (check < values[k]){
》》》》》max = values[k];
》》》》》i = k-1;
》》》》》break;
》》》》}else{
》》》》》max = check;
》》》》》break;
》》》》》i = values.length;
》》》》}
》》》}
》》}
》}
》return max;
}

>> No.9772042 [DELETED] 

float largest(float[] values){
》float check;
》float max;
》for (int i=0; i<values.length; i++){
》》check = values;
》》for (int k=i; k<values.length; k++){
》》》if (k != i){
》》》》if (check < values[k]){
》》》》》max = values[k];
》》》》》i = k-1;
》》》》》break;
》》》》}else{
》》》》》max = check;
》》》》》i = values.length;
》》》》》break;
》》》》}
》》》}
》》}
》}
》return max;
}

>> No.9772059

>>9771943
Come the fuck on.

This:

do {
commands
} while (condition = True)

is really less preferable to this?

while (flag) {
commands
if (condition = True) {
flag = False
}
}

If you seriously think the second is better, you're an idiot.

>> No.9772068
File: 1.03 MB, 1019x746, 1526516994940.png [View same] [iqdb] [saucenao] [google] [report]
9772068

>{

>> No.9772269

>>9766284
>returns an array instead of x+", "+y+", "+z
I don't do programming.
Is there not a more efficient way, or am I just retarded?

>> No.9772326

>>9772269
===============
import numpy
import math

def largest(array):
m = array.max()
n = array.count(m)
array = array.empty(n)
array.fill(m)
return array
====================
Okay, I might actually be retarded.
There has to be something more efficient.

>> No.9772331

>>9766284
just have a for loop where it checks if the next element is larger.

>> No.9772337

>>9772331
The problem is you need to know how many duplicates there are.
So it could be an array with 1-3 elements.

>> No.9772358

>>9772326
yeah: >>9769538

i think numpy has boolean indexing too but im not sure how easy it is to work with

>> No.9772375

>>9772337
A double for loop and a counter could let you keep track of duplicates, though it's pretty slow since you would go through the list for each element.

>> No.9772396
File: 7 KB, 420x420, b36.png [View same] [iqdb] [saucenao] [google] [report]
9772396

>>9772059
>do{
>x+=1;
>}while(x<10);

>while(x<10){
>x+=1;
>}

>DOing anything ever

>> No.9772530

>>9766284

max = x - ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)))

Just move the electrons yourself, faggot

>> No.9772647

What exactly is the desired result from this function?

>> No.9773028

>>9772396
>do{
>》x+=1;
>》if (x == 10){
>》》flag = true;
>》}
>} while(!flag);

>while(!flag){
>》x+=1;
>》if (x == 10){
>》》flag = true;
>》}
>}

wtf is do while even for

>> No.9773083

>>9766284
>returning two different types
A nightmare of checking for types

should be nested

if x==y: if y == z: return [x,y,z] else: return [x, y]
elif x==z: return [x,z]
elif y==z: return [y,z]
else: return [max([x,y,z])]

>> No.9773087

>>9772530
Not bad, but
max = x > y ? x : y
would be faster on cpus that have a conditional move instruction

>> No.9773190

>>9766446
What if T's are not copy constructible? Universal references exist for a reason, anon.

>> No.9773297

>>9773028
see:
>>9770014
>>9770019

>> No.9773705
File: 11 KB, 338x399, what.png [View same] [iqdb] [saucenao] [google] [report]
9773705

>>9768288
can you explain the last two lines to me ?

>> No.9773729

>>9773705
>largest(34,1,1)
>[1,1]
lol, other anon is a dumbass

>> No.9773747

>>9773705
if "if x>y:" hasn't run then y>x so if y>z then y is the largest else z>y>x and z is the largest.

>> No.9774186

>>9766284
>What is list.sort()

>> No.9774312

>>9767899
>says algorithm is unpythonic
>writes unpythonic algorithm

max(args)

>> No.9774328

>>9773297
>int x = 0;
>do{
>》x += 1;
>}while(x >= 1);
>
>x goes to infinity

>int x = 0;
>while (x >= 1){
>》x += 1;
>}
>
>x = 0, loop skipped altogether

Literally no good reason to DO WHILE anything if not for saving, at most, one line of code, which is already accounted for by do{ anyway.

What is the point. Post a snippet that actually needs do while.

>> No.9774355

>>9774328
again, you miss the point. the utility of a do-while is when your loop depends on the state of a variable created during the loop.

here's an example I've run into. I wrote some code where I was comparing the elements of two DNA sequences pair-wise (say, GCATGCAT versus GCATCCAT) and determine which position was the last shared element, left to right.

side note: this step occurs far after initial input processing steps that ensure, among many other cases, that at least the first base of the two sequences are identical.

position = 0
while flag is 0:
if firstSeq[position] == secondSeq[position]:
position = position + 1
else:
flag = 1

if position > min(len(firstSeq),len(secondSeq)):
flag = 1


vs

position = 0
do:
position = position + 1
while: firstSeq[position] == secondSeq[position] & position <= min(len(firstSeq,len(secondSeq)))

they're functionally nearly equivalent, but the second is miles more readable.

>> No.9774442
File: 87 KB, 487x473, CzYzJOBUkAAMjRK-1.jpg [View same] [iqdb] [saucenao] [google] [report]
9774442

>ywn goto ever again

>> No.9774827

>>9774312
Doesnt work, has to return duplicates and equal elements

>> No.9775180

>>9766284
outsourcing

>> No.9775196

>>9766284
Here's a little challenge for you: show us a function largest(a,b,c,d,e).

>> No.9775416

>>9775196
With a little effort one could probably make a function generator that assembles an equivalent function to OP for arbitrary numbers of inputs

>> No.9775674

>>9766404
Who said that x, y, z are integers?

>> No.9775693

>>9770009
>>9771912
x, y, z might be arbitrary objects for which the comparisons are defined (or for which the programmer has defined comparisons). For example, x,y,z could be complex numbers, and the comparison could be comparing the real part. Then obviously [x, x] would be very different from [x, y], even if x == y is true

>> No.9775708

>>9775196
The problem is what the behaviour should be if a == b =/= c == d

>> No.9776039

>>9766284
What language allows a function to return arrays in some cases and single values in other cases?

>> No.9776088

>>9776039
Python

>> No.9776091

>>9776039
meme languages /g/ uses

>> No.9777034

>>9766430
ye

>> No.9777685
File: 10 KB, 296x422, r example.png [View same] [iqdb] [saucenao] [google] [report]
9777685

>>9776039
in addition to python >>9776088, here's an example in R. single integers are represented in R as single-element vectors so instead I had it return a list or a character

>> No.9777691

>>9775196
def largest(a,b,c,d,e):
if a==b==c==d==e:
return [a,b,c,d,e]
elif a==b==c==d:
return [a,b,c,d]
elif a==b==c==e:
return [a,b,c,e]
elif a==b==d==e:
return [a,b,d,e]
elif a==c==d==e:
return [a,c,d,e]
elif b==c==d==e:
return [b,c,d,e]
elif a==b==c:
return [a,b,c]
elif a==b==d:
return [a,b,d]
elif a==b==e:
return [a,b,e]
elif a==c==d:
return [a,c,d]
elif a==c==e:
return [a,c,e]
elif b==c==d:
return [b,c,d]
elif b==c==e:
return [b,c,e]
elif c==d==e:
return [c,d,e]
....

>> No.9777704

function max(a, b){
return a==b?a:(a>b?a:b)
}
function maxPoly(){
return Array.from(arguments).reduce(max)
}

>>
Name (leave empty)
Comment (leave empty)
Name
E-mail
Subject
Comment
Password [?]Password used for file deletion.
reCAPTCHA
Action