This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -5,13 +5,13 @@ typedef struct { double v; int c; } vcount;
int cmp_dbl(const void *a, const void *b)
{
double x = *(double*)a - *(double*)b;
double x = *(const double*)a - *(const double*)b;
return x < 0 ? -1 : x > 0;
}
int vc_cmp(const void *a, const void *b)
{
return ((vcount*)b)->c - ((vcount*)a)->c;
return ((const vcount*)b)->c - ((const vcount*)a)->c;
}
int get_mode(double* x, int len, vcount **list)

View file

@ -1,17 +1,17 @@
import std.stdio, std.algorithm, std.array;
T[] mode(T)(T[] items) /*pure nothrow*/ {
auto mode(T)(T[] items) /*pure nothrow*/ {
int[T] aa;
foreach (item; items)
aa[item]++;
int m = aa.byValue.reduce!max();
return aa.byKey.filter!(k => aa[k] == m)().array();
immutable m = aa.byValue.reduce!max;
return aa.byKey.filter!(k => aa[k] == m);
}
void main() {
auto data = [1, 2, 3, 1, 2, 4, 2, 5, 3, 3, 1, 3, 6];
writeln("Mode: ", data.mode());
writeln("Mode: ", data.mode);
data ~= 2;
writeln("Mode: ", data.mode());
writeln("Mode: ", data.mode);
}

View file

@ -0,0 +1,21 @@
-module( mode ).
-export( [example/0, values/1] ).
example() ->
Set = [1, 2, "qwe", "asd", 1, 2, "qwe", "asd", 2, "qwe"],
io:fwrite( "In ~p the mode(s) is(are): ~p~n", [Set, values(Set)] ).
values( Set ) ->
Dict = lists:foldl( fun values_count/2, dict:new(), Set ),
[X || {X, _Y} <- dict:fold( fun keep_maxs/3, [{0, 0}], Dict )].
keep_maxs( Key, Value, [{_Max_key, Max_value} | _] ) when Value > Max_value ->
[{Key, Value}];
keep_maxs( Key, Value, [{_Max_key, Max_value} | _]=Maxs ) when Value =:= Max_value ->
[{Key, Value} | Maxs];
keep_maxs( _Key, _Value, Maxs ) ->
Maxs.
values_count( Value, Dict ) -> dict:update_counter( Value, 1, Dict ).

View file

@ -0,0 +1,50 @@
use Collection;
class Mode {
function : Main(args : String[]) ~ Nil {
Print(Mode([1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17]));
Print(Mode([1, 2, 4, 4, 1]));
}
function : Mode(coll : Int[]) ~ IntVector {
seen := IntMap->New();
max := 0;
maxElems := IntVector->New();
each(i : coll) {
value := coll[i];
featched := seen->Find(value)->As(IntHolder);
if(featched <> Nil) {
seen->Remove(value);
seen->Insert(value, IntHolder->New(featched->Get() + 1));
}
else {
seen->Insert(value, IntHolder->New(1));
};
featched := seen->Find(value)->As(IntHolder);
if(featched->Get() > max) {
max := featched->Get();
maxElems->Empty();
maxElems->AddBack(value);
}
else if(featched->Get() = max) {
maxElems->AddBack(value);
};
};
return maxElems;
}
function : Print(out : IntVector) ~ Nil {
'['->Print();
each(i : out) {
out->Get(i)->Print();
if(i + 1 < out->Size()) {
", "->Print();
};
};
']'->PrintLine();
}
}

View file

@ -0,0 +1,18 @@
#lang racket
(define (mode seq)
(define frequencies (make-hash))
(for ([s seq])
(hash-update! frequencies
s
(lambda (freq) (add1 freq))
0))
(for/fold ([ms null]
[freq 0])
([(k v) (in-hash frequencies)])
(cond [(> v freq)
(values (list k) v)]
[(= v freq)
(values (cons k ms) freq)]
[else
(values ms freq)])))

View file

@ -0,0 +1,51 @@
$ include "seed7_05.s7i";
const proc: createModeFunction (in type: elemType) is func
begin
const func array elemType: mode (in array elemType: data) is func
result
var array elemType: maxElems is 0 times elemType.value;
local
var hash [elemType] integer: counts is (hash [elemType] integer).value;
var elemType: aValue is elemType.value;
var integer: maximum is 0;
begin
for aValue range data do
if aValue in counts then
incr(counts[aValue]);
else
counts @:= [aValue] 1;
end if;
if counts[aValue] > maximum then
maximum := counts[aValue];
maxElems := [] (aValue);
elsif counts[aValue] = maximum then
maxElems &:= aValue;
end if;
end for;
end func;
const func string: str (in array elemType: data) is func
result
var string: result is "";
local
var elemType: anElement is elemType.value;
begin
for anElement range data do
result &:= " " & str(anElement);
end for;
result := result[2 ..];
end func;
enable_output(array elemType);
end func;
createModeFunction(integer);
const proc: main is func
begin
writeln(mode([] (1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17)));
writeln(mode([] (1, 1, 2, 4, 4)));
end func;