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

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

Max if distinct, duplicates with multiplicity otherwise.

>> No.9766296

not science or math

>> No.9766306

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

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

>> No.9766323

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]


>> No.9766404

int ret_max (int a, int 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

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

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

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

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

>> No.9766446

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

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

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

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

returns 5

>> No.9767678

Actually it returns [5, 5]

>> No.9767694

shit-tier code

fuck off, pajeet

>> No.9767725


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

>> No.9767758

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]

Numale-tier language

>> No.9767899


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

>> No.9767927

Python is dynamically typed

>> No.9767929

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

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

Black, white, green, and red

>> No.9768288

No but what do you expect of cs majors?
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; \\

>> No.9768459

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

this guy gets it

>> No.9768466

>not understanding preserving behavior

>> No.9768468

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

>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

is that python or what is it

>> No.9768561

How does it feel to be retarded?

>> No.9768585

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] 


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


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

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

>> No.9768735

>you'd probably have to evaluate most of those comparisons in the worst case of a sorting algorithm anyway.
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


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

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

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

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

>> No.9769282 [DELETED] 


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

>"I-it's meant to l-look for duplicates..."

>> No.9769300 [DELETED] 

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

>> No.9769314

CS capstone project

>> No.9769315

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

R is cutest language

>> No.9769844

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

>> No.9769846

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

>> No.9769848

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]


>> No.9769874

What is the output of
largest (9, 7, 7)

>> No.9769890

The output would be: [y, z]

>> No.9769909

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]

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

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

>> No.9769997

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

>> No.9770004

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

>> No.9770009


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?

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


Or for your retarded version

>>> 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))

>> No.9770014

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

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

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]

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]

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

>> No.9770336

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

>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

>not understanding the joke

>> No.9771571

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.
Functionally? Yes, though it's ugly.
Won't do the same thing.
>duplicate y == z check
>doesn't even check all cases
The code in OP is superior, engie.

>> No.9771640


>> 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

>return type changes based on arguments

>> No.9771912

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] 

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

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

>> No.9771958 [DELETED] 

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

>>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] 

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;
》return max;

>> No.9772036 [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;
》》》》》max = check;
》》》》》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;
》》》》》max = check;
》》》》》i = values.length;
》return max;

>> No.9772059

Come the fuck on.


do {
} while (condition = True)

is really less preferable to this?

while (flag) {
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]


>> No.9772269

>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

import numpy
import math

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

>> No.9772331

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

>> No.9772337

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

>> No.9772358

yeah: >>9769538

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

>> No.9772375

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]



>DOing anything ever

>> No.9772530


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

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

>》if (x == 10){
>》》flag = true;

wtf is do while even for

>> No.9773083

>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

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

>> No.9773190

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

>> No.9773297


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

can you explain the last two lines to me ?

>> No.9773729

lol, other anon is a dumbass

>> No.9773747

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

>What is list.sort()

>> No.9774312

>says algorithm is unpythonic
>writes unpythonic algorithm


>> No.9774328

>int x = 0;
>》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

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
flag = 1

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


position = 0
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]

>ywn goto ever again

>> No.9774827

Doesnt work, has to return duplicates and equal elements

>> No.9775180


>> No.9775196

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

>> No.9775416

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

Who said that x, y, z are integers?

>> No.9775693

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

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

>> No.9776039

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

>> No.9776088


>> No.9776091

meme languages /g/ uses

>> No.9777034


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

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

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)
Password [?]Password used for file deletion.