This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1 @@
{ 11 9 4 9 4 9 } mode ! 9

View file

@ -0,0 +1,9 @@
mode := function(v)
local c, m;
c := Collected(SortedList(v));
m := Maximum(List(c, x -> x[2]));
return List(Filtered(c, x -> x[2] = m), y -> y[1]);
end;
mode([ 7, 5, 6, 1, 5, 5, 7, 12, 17, 6, 6, 5, 12, 3, 6 ]);
# [ 5, 6 ]

View file

@ -0,0 +1,9 @@
def mode(Iterable col) {
assert col
def m = [:]
col.each {
m[it] = m[it] == null ? 1 : m[it] + 1
}
def keys = m.keySet().sort { -m[it] }
keys.findAll { m[it] == m[keys[0]] }
}

View file

@ -0,0 +1,6 @@
def random = new Random()
def sourceList = [ 'Lamp', 42.0, java.awt.Color.RED, new Date(), ~/pattern/]
(0..10).each {
def a = (0..10).collect { sourceList[random.nextInt(5)] }
println "${mode(a)} == mode(${a})"
}

View file

@ -0,0 +1,14 @@
procedure main(args)
every write(!mode(args))
end
procedure mode(A)
hist := table(0)
every hist[!A] +:= 1
hist := sort(hist, 2)
modeCnt := hist[*hist][2]
every modeP := hist[*hist to 1 by -1] do {
if modeCnt = modeP[2] then suspend modeP[1]
else fail
}
end

View file

@ -0,0 +1 @@
mode=: ~. #~ ( = >./ )@( #/.~ )

View file

@ -0,0 +1,3 @@
mode: {(?x)@&n=|/n:#:'=x}
mode 1 1 1 1 2 2 2 3 3 3 3 4 4 3 2 4 4 4
3 4

View file

@ -0,0 +1,15 @@
MODE(X)
;X is assumed to be a list of numbers separated by "^"
;I is a loop index
;L is the length of X
;Y is a new array
;ML is the list of modes
;LOC is a placeholder to shorten the statement
Q:'$DATA(X) "No data"
Q:X="" "Empty Set"
NEW Y,I,L,LOC
SET L=$LENGTH(X,"^"),ML=""
FOR I=1:1:L SET LOC=+$P(X,"^",I),Y(LOC)=$S($DATA(Y(LOC)):Y(LOC)+1,1:1)
SET I="",I=$O(Y(I)),ML=I ;Prime the pump, rather than test for no data
FOR S I=$O(Y(I)) Q:I="" S ML=$SELECT(Y($P(ML,"^"))>Y(I):ML,Y($P(ML,"^"))<Y(I):I,Y($P(ML,"^"))=Y(I):ML_"^"_I)
QUIT ML

View file

@ -0,0 +1,2 @@
Commonest[{b, a, c, 2, a, b, 1, 2, 3}]
Commonest[{1, 3, 2, 3}]