Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

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

View file

@ -0,0 +1,2 @@
Statistics:-Mode([1, 2.1, 2.1, 3]);
Statistics:-Mode([1, 2.1, 2.1, 3.2, 3.2, 5]);

View file

@ -1,34 +1,29 @@
/*REXX program finds the mode (most occurring element) of a vector. */
/*────────vector────────── ───show vector─── ────show result────── */
v='1 8 6 0 1 9 4 6 1 9 9 9' ; say 'vector='v; say 'mode='mode(v); say
v='1 2 3 4 5 6 7 8 9 10 11' ; say 'vector='v; say 'mode='mode(v); say
v='8 8 8 2 2 2' ; say 'vector='v; say 'mode='mode(v); say
/*════════vector══════════ ═══show vector═══ ════show result══════ */
v= 1 8 6 0 1 9 4 6 1 9 9 9 ; say 'vector='v; say 'mode='mode(v); say
v= 1 2 3 4 5 6 7 8 9 11 10 ; say 'vector='v; say 'mode='mode(v); say
v= 8 8 8 2 2 2 ; say 'vector='v; say 'mode='mode(v); say
v='cat kat Cat emu emu Kat' ; say 'vector='v; say 'mode='mode(v); say
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────MAKEARRAY subroutine────────────────*/
makeArray: procedure expose @.; parse arg v; @.0=words(v) /*make array*/
do k=1 for @.0; @.k=word(v,k); end /*k*/
return
/*──────────────────────────────────ESORT subroutine────────────────────*/
esort: procedure expose @.; h=@.0 /*exchange sort.*/
do while h>1; h=h%2
do i=1 for @.0-h; j=i; k=h+i
do while @.k<@.j;t=@.j;@.j=@.k;@.k=t;if h>=j then leave;j=j-h;k=k-h;end
end /*i*/
end /*while h>1*/
Esort: procedure expose @.; h=@.0 /* [↓] exchange sort. */
do while h>1; h=h%2 /*% is integer divide.*/
do i=1 for @.0-h; j=i; k=h+i /* [↓] perform exchange*/
do while @.k<@.j & h<j; _=@.j; @.j=@.k; @.k=_; j=j-h; k=k-h; end
end /*i*/
end /*while h>1*/
return
/*──────────────────────────────────MODE subroutine─────────────────────*/
mode: procedure expose @.; parse arg x /*finds the MODE of a vector. */
call makeArray x /*make an array out of the vector*/
call esort @.0 /*sort the array elements. */
@.0=words(x) /* [↓] make an array from vector.*/
do k=1 for @.0; @.k=word(x,k); end /*k*/
call Esort @.0 /*sort the elements in the array.*/
?=@.1 /*assume 1st element is the mode.*/
freq=1 /*the frequency of the occurance.*/
do j=1 for @.0; _=j-freq
if @.j==@._ then do
freq=freq+1 /*bump the frequency count. */
?=@.j /*this is the one. */
freq=1 /*the frequency of the occurrence*/
do j=1 for @.0; _=j-freq /*traipse through the elements. */
if @.j==@._ then do /*this element same as previous? */
freq=freq+1 /*bump the frequency counter. */
?=@.j /*this element is the mode,so far*/
end
end /*j*/
return ?
end /*j*/
return ? /*return the node to the invoker.*/