June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -6,24 +6,24 @@ extension op
{
mode
[
var aCountMap := Dictionary new default:0.
var aCountMap := Dictionary new(0).
self forEach(:anItem)
[
aCountMap[anItem] := aCountMap[anItem] + 1
].
aCountMap := aCountMap array_list; sort(:p:n)(p > n).
aCountMap := aCountMap values; sort(:p:n)(p > n).
var aMax := aCountMap firstMember.
^ aCountMap
filterBy(:kv)(aMax equal:kv);
filterBy(:kv)(aMax equal(kv value));
selectBy(:kv)(kv key);
toArray.
]
}
program =
public program =
[
var anArray1 := (1, 1, 2, 4, 4).
var anArray2 := (1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17).

View file

@ -3,3 +3,7 @@ sub mode (*@a) {
my $max = %counts.values.max;
return |%counts.grep(*.value == $max).map(*.key);
}
# Testing with arrays:
say mode [1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17];
say mode [1, 1, 2, 4, 4];

View file

@ -1,2 +1,11 @@
sub mode (*@a) {
return |(@a
.Bag # count elements
.classify(*.value) # group elements with the same count
.max(*.key) # get group with the highest count
.value.map(*.key); # get elements in the group
);
}
say mode [1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17];
say mode [1, 1, 2, 4, 4];

View file

@ -0,0 +1,45 @@
# Project : Averages/Mode
# Date : 2018/04/11
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
a = [1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17]
b = [1, 2, 4, 4, 1]
amodes = list(12)
see "mode(s) of a() = " + nl
for i1 = 1 to modes(a,amodes)
see "" + amodes[i1] + " "
next
see nl
see "mode(s) of b() = " + nl
for i1 = 1 to modes(b,amodes)
see "" + amodes [i1] + " "
next
see nl
func modes(a,amodes)
max = 0
n = len(a)
if n = 0
amodes[1] = a[1]
return 1
ok
c = list(n)
for i = 1 to n
for j = i+1 to n
if a[i] = a[j]
c[i] = c[i] + 1
ok
next
if c[i] > max
max = c[i]
ok
next
j = 0
for i = 1 to n
if c[i] = max
j = j + 1
amodes[j] = a[i]
ok
next
return j