How to resolve the algorithm Averages/Mode step by step in the S-lang programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Averages/Mode step by step in the S-lang programming language
Table of Contents
Problem Statement
Write a program to find the mode value of a collection. The case where the collection is empty may be ignored. Care must be taken to handle the case where the mode is non-unique. If it is not appropriate or possible to support a general collection, use a vector (array), if possible. If it is not appropriate or possible to support an unspecified value type, use integers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Averages/Mode step by step in the S-lang programming language
Source code in the s-lang programming language
private variable mx, mxkey, modedat;
define find_max(key) {
if (modedat[key] > mx) {
mx = modedat[key];
mxkey = {key};
}
else if (modedat[key] == mx) {
list_append(mxkey, key);
}
}
define find_mode(indat)
{
% reset [file/module-scope] globals:
mx = 0, mxkey = {}, modedat = Assoc_Type[Int_Type, 0];
foreach $1 (indat)
modedat[string($1)]++;
array_map(Void_Type, &find_max, assoc_get_keys(modedat));
if (length(mxkey) > 1) {
$2 = 0;
() = printf("{");
foreach $1 (mxkey) {
() = printf("%s%s", $2 ? ", " : "", $1);
$2 = 1;
}
() = printf("} each have ");
}
else
() = printf("%s has ", mxkey[0], mx);
() = printf("the most entries (%d).\n", mx);
}
find_mode({"Hungadunga", "Hungadunga", "Hungadunga", "Hungadunga", "McCormick"});
find_mode({"foo", "2.3", "bar", "foo", "foobar", "quality", 2.3, "strnen"});
You may also check:How to resolve the algorithm Dot product step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Terminal control/Clear the screen step by step in the C / C++ programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Dyalect programming language
You may also check:How to resolve the algorithm XML/Output step by step in the Slate programming language
You may also check:How to resolve the algorithm Taxicab numbers step by step in the Swift programming language