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:
Anonymous2009-01-26 12:40
NO EXCEPTIONS
Name:
Anonymous2009-01-26 12:43
python, index of maximum element in array
from numpy import argmax
Name:
Anonymous2009-01-26 12:43
*
"GRUNNUR"
;
Name:
Anonymous2009-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:
Anonymous2009-01-26 12:52
>>5
Your next objective: get this included in the PHP standard library.
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:
Anonymous2009-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;
}
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;}
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.
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:
Anonymous2009-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:
Anonymous2009-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:
Anonymous2009-01-27 0:13
g(n) shit. Sort the array for a cost of O(logn) and get g(1) max for a total of O(logn).