Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

java, index of maximum element in array

Name: Anonymous 2009-01-26 12:02

public static int maximum(double[] a){
  double max = a[0];
  int position = 0;
  for (int i = 1; i < a.length; i++) {
    if (a[i] > max) {
      max = a[i];
      position = i;
    }
  }
  return position;
}

//DONE.

Name: Anonymous 2009-01-26 12:40

NO EXCEPTIONS

Name: Anonymous 2009-01-26 12:43

python, index of maximum element in array

from numpy import argmax

Name: Anonymous 2009-01-26 12:43

*
"GRUNNUR"
;

Name: Anonymous 2009-01-26 12:47

PHP, index of maxium element in ary

$maximum = -1;

function maxxiumElementsindex($length, $array){
global $maximum; //so u can acses this outside the functon
$valu = false;
foreach($array as $k => $v)
{
         if ($valu === false | $valu < $v
         ) {$valu=$v;//assign v to valu
         $mxaimum = $k;
         }
}}

Name: Anonymous 2009-01-26 12:52

>>5
Your next objective: get this included in the PHP standard library.

Name: Anonymous 2009-01-26 12:53

>>6
You know what? It probably already is.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !FrOzEn2BUo 2009-01-26 13:34

aa=[0,1,21,41,5];aa.toString();aa.indexOf(eval('Math.max('+aa+')'))

_________________________
orbis terrarum delenda est
 http://xs135.xs.to/xs135/09042/av922.jpg

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !FrOzEn2BUo 2009-01-26 13:36

Optimized:(conversion not needed)
aa=[0,1,21,41,5];aa.indexOf(eval('Math.max('+aa+')'))

_________________________
orbis terrarum delenda est
 http://xs135.xs.to/xs135/09042/av922.jpg

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !FrOzEn2BUo 2009-01-26 13:39

function maxarindex(array){return array.indexOf(eval('Math.max('+array+')'))}

_________________________
orbis terrarum delenda est
 http://xs135.xs.to/xs135/09042/av922.jpg

Name: Anonymous 2009-01-26 14:00

Haskell, index of maximum element in array

maximum >>= elemIndex

Name: Anonymous 2009-01-26 14:03

Mr. Void, would you consider using code tags? Or giving me a reason why you won't?

Name: Anonymous 2009-01-26 14:04

>>12
He's at least indenting code here, which is something he doesn't do on his BLAG.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !FrOzEn2BUo 2009-01-26 14:15

>>12
 "Indenting" code? This isn't Python.
I'm striving to  compact code not to spread it all over. 

_________________________
orbis terrarum delenda est
 http://xs135.xs.to/xs135/09042/av922.jpg

Name: Anonymous 2009-01-26 14:19

You require additional ENTERPRISE

public static int maximum(double[] a){
  if (a == null || a.length == 0)
    throw new IllegalArgumentException();
  else {
    Double max = Double.NEGATIVE_INFINITY;
    int position = 0;
    List<Double> list = Arrays.asList(a);
    for (ListIterator iter = list.listIterator(); iter.hasNext()) {
      double current = iter.next();
      if (current > max) {
        max = current;
        position = iter.prevIndex();
      }
    }
    return position;
  }
}

Name: Anonymous 2009-01-26 14:20

>>14
No, damn code tags, so your code is actually readable. Observe:

public static int maximum(double[] a){
  double max = a[0];
  int position = 0;
  for (int i = 1; i < a.length; i++) {
    if (a[i] > max) {
      max = a[i];
      position = i;
    }
  }
  return position;
}

//DONE.

Code tags:

public static int maximum(double[] a){
  double max = a[0];
  int position = 0;
  for (int i = 1; i < a.length; i++) {
    if (a[i] > max) {
      max = a[i];
      position = i;
    }
  }
  return position;
}

//DONE.


It's nicer to read.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !FrOzEn2BUo 2009-01-26 14:46

>>16 They look the same on my screen

_________________________
orbis terrarum delenda est
 http://xs135.xs.to/xs135/09042/av922.jpg

Name: Anonymous 2009-01-26 14:48

>>17
But they don't look the same on mine. You should be friendly and make others' lives easier if it costs you nothing.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !FrOzEn2BUo 2009-01-26 14:50

This is much better and faster to write(and i can read this faster).
public static int maximum(double[] a){double max = a[0];int position = 0;for (int i = 1; i < a.length; i++) {
    if (a[i] > max) {max = a[i];position = i;}}return position;}

_________________________
orbis terrarum delenda est
 http://xs135.xs.to/xs135/09042/av922.jpg

Name: Anonymous 2009-01-26 15:05

STOP REPLYING TO INVISIBLE POSTS YOU IDIOTS

Name: Anonymous 2009-01-26 15:13

GOD FUCKING DAMN I MEAN USE [code] TAGS, NOT INDENTATION, I KNOW THAT ONE LINERS EASIER TO READ I AM A FUCKING HASKELLITE BUT FOR FUCK'S SAKE PROPORTIONAL FONT IS NOT FOR CODE AND BY USING [code] TAGS YOU ASSURE THAN YOUR CODE IS IN MONOSPACED FONT WAS THAT HARD TO GRASP.

This is my first reply to you and now I know why they deem you a troll.
Thanks, >>20, I sure won't.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !FrOzEn2BUo 2009-01-26 15:18

>>21
Can't you just set your browser to monospaced fonts,or make 4chan specific mono-style if you like it so much?

_________________________
orbis terrarum delenda est
 http://xs135.xs.to/xs135/09042/av922.jpg

Name: Anonymous 2009-01-26 16:32

OCAML, maximum element in a list (arrays are mutable)


let max_list l =
  let rec max_internal e = function
    [] -> e
  | x::xs -> if (e<x) then max_internal x xs else max_internal e xs
  in max_internal (List.hd l) (List.tl l);;


OCAML, index of maximum element in a list (arrays are mutable)


let max_list_idx (h::t) =
  let rec max_internal e c m = function
    [] -> m
  | x::xs -> if(x>e) then max_internal x (c+1) c xs else max_internal e (c+1) m xs
  in max_internal h 1 0 t;;


Both functions throw an exception if the list is null (could add support for a specifc exception, but didn't add it here), so the first will just throw an exception in hd or tl, and second will be just a matching failure.

OCAML,index of maximum element in an array(mutable)


exception NullArray;;
let max_array_index a =
  let l = Array.length a in
  if l = 0 then raise NullArray;
  let mi = ref 0 in
    for i = 1 to l - 1 do
      if a.(!mi)<a.(i) then mi := i
    done;
  !mi;

Name: Anonymous 2009-01-26 16:43

Make an class "element" with 2 properties, index and value

implement the class array which subclasses vector,

implement accessors and binary operators and sorting function.
sort the array based on value, keeping the original index.
output the index of the last value in the sorted list.

Name: Anonymous 2009-01-27 0:05

Haskell, index of maximum element in list

lol :: [a] -> Nothing | a
lol [] = Nothing
lol [x] = x
lol (x:xs) = if x > lol xs then x else lol xs

Name: Anonymous 2009-01-27 0:13

g(n) shit.  Sort the array for a cost of O(log n) and get g(1) max for a total of O(log n).

Name: Anonymous 2009-01-27 0:16

whoops forgot an n
orz

Name: Anonymous 2009-01-27 1:17

>>25
I don't think this works.

Name: Anonymous 2009-01-27 2:11

lol :: (Enum a, Num a, Ord b) => [b] -> a
lol = fst . head . sortBy (\(xi,xv) (yi,yv) -> compare yv xv) . zip [0..]

Name: Anonymous 2009-01-27 4:40

>>21
PROPORTIONAL FONT IS NOT FOR CODE
[citation needed]

Name: Anonymous 2009-01-27 6:52

>>9

fail

if I have two maxs in pos=0 and pos=index-1, it would return the one in the end, not the one in the begining (check op's code).

Name: Anonymous 2009-01-27 8:32

>>29
If you must insist on that approach:
lol = fst . maximumBy (comparing snd) . zip [0..]
>>11 is still superior, though.

Name: Anonymous 2009-01-27 10:51

>>28
I don't think this works.
Why not?

Name: Anonymous 2009-03-06 7:10

The managers crossplatform WORA   blah blah BS   BS etc keep   THIS INFORMATION PRIVATE.

Name: Vageene 2010-10-21 6:12

holla

Name: Anonymous 2013-01-19 23:50

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Don't change these.
Name: Email:
Entire Thread Thread List