September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -1,13 +1,2 @@
mode = (arr) ->
# returns an array with the modes of arr, i.e. the
# elements that appear most often in arr
counts = {}
for elem in arr
counts[elem] ||= 0
counts[elem] += 1
max = 0
for key, cnt of counts
max = cnt if cnt > max
(key for key, cnt of counts when cnt == max)
console.log mode [1, 2, 2, 2, 3, 3, 3, 4, 4]
(defn modes [coll]
(->> coll frequencies (sort-by val >) (partition-by val) first (map key)))

View file

@ -1,15 +1,13 @@
(defun mode (sequence &rest hash-table-options)
(let ((frequencies (apply #'make-hash-table hash-table-options)))
(map nil (lambda (element)
(incf (gethash element frequencies 0)))
sequence)
(let ((modes '())
(hifreq 0 ))
(maphash (lambda (element frequency)
(cond ((> frequency hifreq)
(setf hifreq frequency
modes (list element)))
((= frequency hifreq)
(push element modes))))
frequencies)
(values modes hifreq))))
mode = (arr) ->
# returns an array with the modes of arr, i.e. the
# elements that appear most often in arr
counts = {}
for elem in arr
counts[elem] ||= 0
counts[elem] += 1
max = 0
for key, cnt of counts
max = cnt if cnt > max
(key for key, cnt of counts when cnt == max)
console.log mode [1, 2, 2, 2, 3, 3, 3, 4, 4]

View file

@ -0,0 +1,15 @@
(defun mode (sequence &rest hash-table-options)
(let ((frequencies (apply #'make-hash-table hash-table-options)))
(map nil (lambda (element)
(incf (gethash element frequencies 0)))
sequence)
(let ((modes '())
(hifreq 0 ))
(maphash (lambda (element frequency)
(cond ((> frequency hifreq)
(setf hifreq frequency
modes (list element)))
((= frequency hifreq)
(push element modes))))
frequencies)
(values modes hifreq))))

View file

@ -1,37 +1,37 @@
import system'routines.
import system'collections.
import extensions.
import system'routines;
import system'collections;
import extensions;
extension op
{
mode
[
var aCountMap := Dictionary new(0).
self forEach(:anItem)
[
aCountMap[anItem] := aCountMap[anItem] + 1
].
get Mode()
{
var countMap := Dictionary.new(0);
self.forEach:(item)
{
countMap[item] := countMap[item] + 1
};
aCountMap := aCountMap values; sort(:p:n)(p > n).
countMap := countMap.Values.sort:(p,n => p > n);
var aMax := aCountMap firstMember.
var max := countMap.FirstMember;
^ aCountMap
filterBy(:kv)(aMax equal(kv value));
selectBy(:kv)(kv key);
toArray.
]
^ countMap
.filterBy:(kv => max.equal(kv.Value))
.selectBy:(kv => kv.Key)
.toArray()
}
}
public program
[
var anArray1 := (1, 1, 2, 4, 4).
var anArray2 := (1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17).
var anArray3 := (1, "blue", 2, 7.5r, 5, "green", "red", 5, 2, "blue", "white").
public program()
{
var array1 := new int[] {1, 1, 2, 4, 4};
var array2 := new int[] {1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17};
var array3 := new object[] {1, "blue", 2, 7.5r, 5, "green", "red", 5, 2, "blue", "white"};
console
printLine("mode of (",anArray1,") is (",anArray1 mode,")");
printLine("mode of (",anArray2,") is (",anArray2 mode,")");
printLine("mode of (",anArray3,") is (",anArray3 mode,")");
readChar.
]
.printLine("mode of (",array1.asEnumerable(),") is (",array1.Mode,")")
.printLine("mode of (",array2.asEnumerable(),") is (",array2.Mode,")")
.printLine("mode of (",array3.asEnumerable(),") is (",array3.Mode,")")
.readChar()
}

View file

@ -1,7 +1,7 @@
sub mode (*@a) {
my %counts := @a.Bag;
my $max = %counts.values.max;
return |%counts.grep(*.value == $max).map(*.key);
%counts.grep(*.value == $max).map(*.key);
}
# Testing with arrays:

View file

@ -1,10 +1,8 @@
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
);
@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];

View file

@ -0,0 +1 @@
mode:{(key x) where value x=max x} count each group @

View file

@ -0,0 +1,34 @@
-- setup
create table averages (val integer);
insert into averages values (1);
insert into averages values (2);
insert into averages values (3);
insert into averages values (1);
insert into averages values (2);
insert into averages values (4);
insert into averages values (2);
insert into averages values (5);
insert into averages values (2);
insert into averages values (3);
insert into averages values (3);
insert into averages values (1);
insert into averages values (3);
insert into averages values (6);
-- find the mode
with
counts as
(
select
val,
count(*) as num
from
averages
group by
val
)
select
val as mode_val
from
counts
where
num in (select max(num) from counts);

View file

@ -0,0 +1,7 @@
Public Sub main()
s = [{1,2,3,3,3,4,4,4,5,5,6}]
t = WorksheetFunction.Mode_Mult(s)
For Each x In t
Debug.Print x;
Next x
End Sub