This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,68 @@
#include <iterator>
#include <utility>
#include <algorithm>
#include <list>
#include <iostream>
// helper struct
template<typename T> struct referring
{
referring(T const& t): value(t) {}
template<typename Iter>
bool operator()(std::pair<Iter, int> const& p) const
{
return *p.first == value;
}
T const& value;
};
// requires:
// FwdIterator is a ForwardIterator
// The value_type of FwdIterator is EqualityComparable
// OutIterator is an output iterator
// the value_type of FwdIterator is convertible to the value_type of OutIterator
// [first, last) is a valid range
// provides:
// the mode is written to result
template<typename FwdIterator, typename OutIterator>
void mode(FwdIterator first, FwdIterator last, OutIterator result)
{
typedef typename std::iterator_traits<FwdIterator>::value_type value_type;
typedef std::list<std::pair<FwdIterator, int> > count_type;
typedef typename count_type::iterator count_iterator;
// count elements
count_type counts;
while (first != last)
{
count_iterator element = std::find_if(counts.begin(), counts.end(),
referring<value_type>(*first));
if (element == counts.end())
counts.push_back(std::make_pair(first, 1));
else
++element->second;
++first;
}
// find maximum
int max = 0;
for (count_iterator i = counts.begin(); i != counts.end(); ++i)
if (i->second > max)
max = i->second;
// copy corresponding elements to output sequence
for (count_iterator i = counts.begin(); i != counts.end(); ++i)
if (i->second == max)
*result++ = *i->first;
}
// example usage
int main()
{
int values[] = { 1, 2, 3, 1, 2, 4, 2, 5, 2, 3, 3, 1, 3, 6 };
median(values, values + sizeof(values)/sizeof(int),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
return 0;
}

View file

@ -0,0 +1,17 @@
import std.stdio, std.algorithm, std.array;
T[] mode(T)(T[] items) /*pure nothrow*/ {
int[T] aa;
foreach (item; items)
aa[item]++;
int m = aa.byValue.reduce!max();
return aa.byKey.filter!(k => aa[k] == m)().array();
}
void main() {
auto data = [1, 2, 3, 1, 2, 4, 2, 5, 3, 3, 1, 3, 6];
writeln("Mode: ", data.mode());
data ~= 2;
writeln("Mode: ", data.mode());
}

View file

@ -0,0 +1,9 @@
pragma.enable("accumulator")
def mode(values) {
def counts := [].asMap().diverge()
var maxCount := 0
for v in values {
maxCount max= (counts[v] := counts.fetch(v, fn{0}) + 1)
}
return accum [].asSet() for v => ==maxCount in counts { _.with(v) }
}

View file

@ -0,0 +1,2 @@
? mode([1,1,2,2,3,3,4,4,4,5,5,6,6,7,8,8,9,9,0,0,0])
# value: [4, 0].asSet()

View file

@ -0,0 +1,3 @@
def newCount := counts.fetch(v, fn { 0 }) + 1
counts[v] := newCount
maxCount := maxCount.max(newCount)

View file

@ -0,0 +1,44 @@
#define std'dictionary'*.
#define std'basic'*.
#define std'patterns'*.
#define std'routines'*.
#define std'collections'*.
#subject mode.
#symbol ModeValue : anArray =
[
#var aCountMap := Dictionary.
Scan::anArray run: anItem =
[
#if aCountMap@anItem += 1
| [
aCountMap append &dictionary_key:anItem &content:Integer::1.
].
].
aCountMap~esort run: aPair = (aPair former > aPair later).
#var aResult := List.
#var aMax := aCountMap first_item content.
Scan::aCountMap run: anItem =
[
#if (aMax == anItem)?
[ aResult += anItem dictionary_key. ].
].
^ aResult.
].
#symbol Program =
[
#var anArray1 := (1, 1, 2, 4, 4).
#var anArray2 := (1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17).
#var aMode1 := ModeValue::anArray1.
#var aMode2 := ModeValue::anArray2.
'program'output << "mode of (" << anArray1 << ") is (" << aMode1 << ")%n".
'program'output << "mode of (" << anArray2 << ") is (" << aMode2 << ")%n".
].

View file

@ -0,0 +1,39 @@
include misc.e
function mode(sequence s)
sequence uniques, counts, modes
integer j,max
uniques = {}
counts = {}
for i = 1 to length(s) do
j = find(s[i], uniques)
if j then
counts[j] += 1
else
uniques = append(uniques, s[i])
counts = append(counts, 1)
end if
end for
max = counts[1]
for i = 2 to length(counts) do
if counts[i] > max then
max = counts[i]
end if
end for
j = 1
modes = {}
while j <= length(s) do
j = find_from(max, counts, j)
if j = 0 then
exit
end if
modes = append(modes, uniques[j])
j += 1
end while
return modes
end function
constant s = { 1, "blue", 2, 7.5, 5, "green", "red", 5, 2, "blue", "white" }
pretty_print(1,mode(s),{3})