September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,37 +1,37 @@
#define system.
#define system'routines.
#define system'collections.
#define extensions.
import system'routines.
import system'collections.
import extensions.
#class(extension) op
extension op
{
#method mode
mode
[
#var aCountMap := Dictionary new &default:0.
self run &each: anItem
var aCountMap := Dictionary new default:0.
self forEach(:anItem)
[
aCountMap@anItem := aCountMap@anItem + 1.
aCountMap[anItem] := aCountMap[anItem] + 1
].
aCountMap := aCountMap array_list sort:(:p:n) [ p > n ].
aCountMap := aCountMap array_list; sort(:p:n)(p > n).
#var aMax := aCountMap firstMember.
var aMax := aCountMap firstMember.
^ aCountMap
filter &each:kv [ aMax safeEqual:kv ]
select &each:kv [ kv key ]
filterBy(:kv)(aMax equal:kv);
selectBy(:kv)(kv key);
toArray.
]
}
#symbol program =
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").
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").
console
writeLine:"mode of (":anArray1:") is (":(anArray1 mode):")"
writeLine:"mode of (":anArray2:") is (":(anArray2 mode):")"
writeLine:"mode of (":anArray3:") is (":(anArray3 mode):")".
printLine("mode of (",anArray1,") is (",anArray1 mode,")");
printLine("mode of (",anArray2,") is (",anArray2 mode,")");
printLine("mode of (",anArray3,") is (",anArray3 mode,")");
readChar.
].

View file

@ -0,0 +1,21 @@
fun <T> modeOf(a: Array<T>) {
val sortedByFreq = a.groupBy { it }.entries.sortedByDescending { it.value.size }
val maxFreq = sortedByFreq.first().value.size
val modes = sortedByFreq.takeWhile { it.value.size == maxFreq }
if (modes.size == 1)
println("The mode of the collection is ${modes.first().key} which has a frequency of $maxFreq")
else {
print("There are ${modes.size} modes with a frequency of $maxFreq, namely : ")
println(modes.map { it.key }.joinToString(", "))
}
}
fun main(args: Array<String>) {
val a = arrayOf(7, 1, 1, 6, 2, 4, 2, 4, 2, 1, 5)
println("[" + a.joinToString(", ") + "]")
modeOf(a)
println()
val b = arrayOf(true, false, true, false, true, true)
println("[" + b.joinToString(", ") + "]")
modeOf(b)
}

View file

@ -0,0 +1,32 @@
-- will work with just about any collection...
call testMode .array~of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
call testMode .list~of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, .11)
call testMode .queue~of(30, 10, 20, 30, 40, 50, -100, 4.7, -11e2)
::routine testMode
use arg list
say "list =" list~makearray~toString("l", ", ")
say "mode =" mode(list)
say
::routine mode
use arg list
-- this is a good application for a bag
-- add all of the items to the bag
collector = .bag~new
collector~putAll(list)
-- now get a list of unique items
indexes = .set~new~~putall(collector)
count = 0 -- this is used to keep track of the maximums
-- now see how many of each element we ended up with
loop index over indexes
items = collector~allat(index)
newCount = items~items
if newCount > count then do
mode = items[1]
count = newCount
end
end
return mode

View file

@ -0,0 +1,5 @@
fcn mode(items){
d:=Dictionary(); foreach i in (items){ d.incV(i) }
m:=d.reduce(fcn(m,[(_,v)]){ v.max(m) },0);
d.filter('wrap([(_,v)]){ v==m }).apply("get",0);
}

View file

@ -0,0 +1,4 @@
data:=T(1, 2, 3, 1, 2, 4, 2, 5, 3, 3, 1, 3, 6);
println("Mode: ", mode(data));
println("Mode: ", mode(data.append(2)));
println("Mode: ", mode("this is a test".split("")));