langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,75 @@
/* NetRexx */
options replace format comments java crossref symbols nobinary
run_samples()
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method mode(lvector = java.util.List) public static returns java.util.List
seen = 0
modes = ''
modeMax = 0
loop v_ = 0 to lvector.size() - 1
mv = Rexx lvector.get(v_)
seen[mv] = seen[mv] + 1
select
when seen[mv] > modeMax then do
modeMax = seen[mv]
modes = ''
nx = 1
modes[0] = nx
modes[nx] = mv
end
when seen[mv] = modeMax then do
nx = modes[0] + 1
modes[0] = nx
modes[nx] = mv
end
otherwise do
nop
end
end
end v_
modeList = ArrayList(modes[0])
loop v_ = 1 to modes[0]
val = modes[v_]
modeList.add(val)
end v_
return modeList
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method mode(rvector = Rexx[]) public static returns java.util.List
return mode(ArrayList(Arrays.asList(rvector)))
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method show_mode(lvector = java.util.List) public static returns java.util.List
modes = mode(lvector)
say 'Vector:' (Rexx lvector).space(0)', Mode(s):' (Rexx modes).space(0)
return modes
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method show_mode(rvector = Rexx[]) public static returns java.util.List
return show_mode(ArrayList(Arrays.asList(rvector)))
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method run_samples() public static
show_mode([Rexx 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) -- 10 9 8 7 6 5 4 3 2 1
show_mode([Rexx 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0.11]) -- 0
show_mode([Rexx 30, 10, 20, 30, 40, 50, -100, 4.7, -11e+2]) -- 30
show_mode([Rexx 30, 10, 20, 30, 40, 50, -100, 4.7, -11e+2, -11e+2]) -- 30 -11e2
show_mode([Rexx 1, 8, 6, 0, 1, 9, 4, 6, 1, 9, 9, 9]) -- 9
show_mode([Rexx 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) -- 1 2 3 4 5 6 7 8 9 10 11
show_mode([Rexx 8, 8, 8, 2, 2, 2]) -- 8 2
show_mode([Rexx 'cat', 'kat', 'Cat', 'emu', 'emu', 'Kat']) -- emu
show_mode([Rexx 0, 1, 2, 3, 3, 3, 4, 4, 4, 4, 1, 0]) -- 4
show_mode([Rexx 2, 7, 1, 8, 2]) -- 2
show_mode([Rexx 2, 7, 1, 8, 2, 8]) -- 8 2
show_mode([Rexx 'E', 'n', 'z', 'y', 'k', 'l', 'o', 'p', 'ä', 'd', 'i', 'e']) -- E n z y k l o p ä d i e
show_mode([Rexx 'E', 'n', 'z', 'y', 'k', 'l', 'o', 'p', 'ä', 'd', 'i', 'e', 'ä']) -- ä
show_mode([Rexx 3, 1, 4, 1, 5, 9, 7, 6]) -- 1
show_mode([Rexx 3, 1, 4, 1, 5, 9, 7, 6, 3]) -- 3, 1
show_mode([Rexx 1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17]) -- 6
show_mode([Rexx 1, 1, 2, 4, 4]) -- 4 1
return

View file

@ -0,0 +1,13 @@
let mode lst =
let seen = Hashtbl.create 42 in
List.iter (fun x ->
let old = if Hashtbl.mem seen x then
Hashtbl.find seen x
else 0 in
Hashtbl.replace seen x (old + 1))
lst;
let best = Hashtbl.fold (fun _ -> max) seen 0 in
Hashtbl.fold (fun k v acc ->
if v = best then k :: acc
else acc)
seen []

View file

@ -0,0 +1,26 @@
#import <Foundation/Foundation.h>
@interface NSArray (Mode)
- (NSArray *)mode;
@end
@implementation NSArray (Mode)
- (NSArray *)mode {
NSCountedSet *seen = [NSCountedSet setWithArray:self];
int max = 0;
NSMutableArray *maxElems = [NSMutableArray array];
NSEnumerator *enm = [seen objectEnumerator];
id obj;
while( (obj = [enm nextObject]) ) {
int count = [seen countForObject:obj];
if (count > max) {
max = count;
[maxElems removeAllObjects];
[maxElems addObject:obj];
} else if (count == max) {
[maxElems addObject:obj];
}
}
return maxElems;
}
@end

View file

@ -0,0 +1,29 @@
function m = mode2(v)
sv = sort(v);
% build two vectors, vals and c, so that
% c(i) holds how many times vals(i) appears
i = 1; c = []; vals = [];
while (i <= numel(v) )
tc = sum(sv==sv(i)); % it would be faster to count
% them "by hand", since sv is sorted...
c = [c, tc];
vals = [vals, sv(i)];
i += tc;
endwhile
% stack vals and c building a 2-rows matrix x
x = cat(1,vals,c);
% sort the second row (frequencies) into t (most frequent
% first) and take the "original indices" i ...
[t, i] = sort(x(2,:), "descend");
% ... so that we can use them to sort columns according
% to frequencies
nv = x(1,i);
% at last, collect into m (the result) all the values
% having the same bigger frequency
r = t(1); i = 1;
m = [];
while ( t(i) == r )
m = [m, nv(i)];
i++;
endwhile
endfunction

View file

@ -0,0 +1,7 @@
a = [1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17];
mode2(a)
mode(a)
a = [1, 1, 2, 4, 4];
mode2(a) % returns 1 and 4
mode(a) % returns 1 only

View file

@ -0,0 +1,17 @@
declare
fun {Mode Xs}
Freq = {Dictionary.new}
for X in Xs do
Freq.X := {CondSelect Freq X 0} + 1
end
MaxCount = {FoldL {Dictionary.items Freq} Max 0}
in
for Value#Count in {Dictionary.entries Freq} collect:C do
if Count == MaxCount then
{C Value}
end
end
end
in
{Show {Mode [1 2 3 3 2 1 1]}}
{Show {Mode [1 2 3 3 2 1]}}

View file

@ -0,0 +1,16 @@
mode(v)={
my(count=1,r=1,b=v[1]);
v=vecsort(v);
for(i=2,#v,
if(v[i]==v[i-1],
count++
,
if(count>r,
r=count;
b=v[i-1]
);
count=1
)
);
if(count>r,v[#v],b)
};

View file

@ -0,0 +1,6 @@
sub mode (@a) {
my %counts;
++%counts{$_} for @a;
my $max = [max] values %counts;
return map { .key }, grep { .value == $max }, %counts.pairs;
}

View file

@ -0,0 +1,3 @@
$data = @(1,1,1,2,3,4,5,5,6,7,7,7)
$groups = $data | group-object | sort-object count -Descending
$groups | ? {$_.Count -eq $groups[0].Count}

View file

@ -0,0 +1,38 @@
Procedure mean(Array InArray(1))
Structure MyMean
Value.i
Cnt.i
EndStructure
Protected i, max, found
Protected NewList MyDatas.MyMean()
Repeat
found=#False
ForEach MyDatas()
If InArray(i)=MyDatas()\Value
MyDatas()\Cnt+1
found=#True
Break
EndIf
Next
If Not found
AddElement(MyDatas())
MyDatas()\Value=InArray(i)
MyDatas()\cnt+1
EndIf
If MyDatas()\Cnt>max
max=MyDatas()\Cnt
EndIf
i+1
Until i>ArraySize(InArray())
ForEach MyDatas()
If MyDatas()\Cnt=max
For i=1 To max
Print(Str(MyDatas()\Value)+" ")
Next
EndIf
Next
EndProcedure

View file

@ -0,0 +1,5 @@
s@(Sequence traits) mode
[| sortedCounts |
sortedCounts: (s as: Bag) sortedCounts.
(sortedCounts mapSelect: [| :count :elem | sortedCounts last count = count]) valueSet
].

View file

@ -0,0 +1,15 @@
#!/bin/bash
function mode {
declare -A map
max=0
for x in "$@"; do
tmp=$((${map[$x]} + 1))
map[$x]=$tmp
((tmp > max)) && max=$tmp
done
for x in "${!map[@]}"; do
[[ ${map[$x]} == $max ]] && echo -n "$x "
done
echo
}

View file

@ -0,0 +1,2 @@
mode 1 2 1 2 a b a b a 2
a 2

View file

@ -0,0 +1,7 @@
#import std
mode = ~&hS+ leql$^&h+ eql|=@K2
#cast %nLW
examples = mode~~ (<1,3,6,6,6,7,7,12,12,17>,<1,1,2,4,4>)

View file

@ -0,0 +1,25 @@
BOF // Copy all data to a new buffer
Reg_Copy(10, ALL)
Buf_Switch(Buf_Free)
Reg_Ins(10)
Sort(0, File_Size) // Sort the data
BOF
repeat(ALL) { // Count & delete duplicate lines
#1 = 1
while(Match("^{.*}\N\1$", REGEXP)==0) {
Del_Line(1)
#1++
}
Num_Ins(#1, NOCR) // Insert item count at the beginning of line
Ins_Char(9) // TAB
Line(1, ERRBREAK) // Next (different) line
}
Sort(0, File_Size, REVERSE) // Sort according to the count
BOF // Display the results
Reg_Copy_Block(10, CP, EOL_pos)
Buf_Quit(OK)
Statline_Message(@10)