Anonymous user
Averages/Mode: Difference between revisions
→{{header|S-lang}}
Line 2,427:
I'm accepting strings and numbers, although I'm converting numbers to strings,
as S-Lang Assoc_Type only accepts strings as keys.
<lang S-lang>private variable
variable indat = {"foo", "2.3", "bar", "foo", "foobar", "quality", 2.3, "strnen"};▼
foreach $1 (indat)▼
modedat[string($1)]++;▼
define find_max(key) {
if (modedat[key] > mx) {
Line 2,444 ⟶ 2,439:
}
define find_mode(indat)
array_map(Void_Type, &find_max, assoc_get_keys(modedat));▼
{
if (length(mxkey) > 1) {▼
% reset [file/module-scope] globals:
$2 = 0;▼
mx = 0, mxkey = {}, modedat = Assoc_Type[Int_Type, 0];
() = printf("%s%s", $2 ? ", " : "", $1);▼
▲ modedat[string($1)]++;
▲ array_map(Void_Type, &find_max, assoc_get_keys(modedat));
▲ if (length(mxkey) > 1) {
▲ $2 = 0;
▲ () = printf("%s%s", $2 ? ", " : "", $1);
$2 = 1;
}
}
else▼
▲ () = printf(" each have ");
() = printf("%s has ", mxkey[0], mx);
}
▲else
▲ () = printf("%s has ", mxkey[0], mx);
▲() = printf("the most entries at %d\n", mx);</lang>
find_mode({"Hungadunga", "Hungadunga", "Hungadunga", "Hungadunga", "McCormick"});
▲
{{out}}
<pre>
{foo, 2.3} each have the most entries (2).</pre>
=={{header|Scala}}==
|