RosettaCodeData/Task/Averages-Mode/Seed7/averages-mode.seed7
2016-12-05 22:15:40 +01:00

51 lines
1.4 KiB
Text

$ 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: stri is "";
local
var elemType: anElement is elemType.value;
begin
for anElement range data do
stri &:= " " & str(anElement);
end for;
stri := stri[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;