Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -3,39 +3,35 @@
#define system'collections.
#define extensions.
// Averages/Mode
#symbol mode = (:anArray)
[
#var aCountMap := Dictionary new &default:0.
control foreach:anArray &do: anItem
#class(extension) op
{
#method mode
[
aCountMap set &key:anItem &value:(aCountMap getAt &key:anItem + 1).
].
#var aCountMap := Dictionary new &default:0.
self run &each: anItem
[
aCountMap@anItem := aCountMap@anItem + 1.
].
listControl sort:aCountMap &with: (:p:n)
[ p value > n value ].
aCountMap := aCountMap array_list sort:(:p:n) [ p > n ].
#var aResult := List new.
#var aMax := aCountMap firstMember.
#var aMax := aCountMap First value.
control foreach:aCountMap &do: anItem
[
aMax == anItem value
? [ aResult += anItem key. ].
].
^ listControl toArray:aResult.
].
^ aCountMap
filter &each:kv [ aMax safeEqual:kv ]
select &each:kv [ kv key ]
toArray.
]
}
#symbol 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 aMode1 := mode:anArray1.
#var aMode2 := mode:anArray2.
consoleEx writeLine:"mode of (":anArray1:") is (":aMode1:")".
consoleEx writeLine:"mode of (":anArray2:") is (":aMode2:")".
console
writeLine:"mode of (":anArray1:") is (":(anArray1 mode):")"
writeLine:"mode of (":anArray2:") is (":(anArray2 mode):")"
writeLine:"mode of (":anArray3:") is (":(anArray3 mode):")".
].

View file

@ -0,0 +1,14 @@
defmodule Average do
def mode(list) do
gb = Enum.group_by(list, &(&1))
max = Enum.map(gb, fn {_,val} -> length(val) end) |> Enum.max
for {key,val} <- gb, length(val)==max, do: key
end
end
lists = [[3,1,4,1,5,9,2,6,5,3,5,8,9],
[1, 2, "qwe", "asd", 1, 2, "qwe", "asd", 2, "qwe"]]
Enum.each(lists, fn list ->
IO.puts "mode: #{inspect list}"
IO.puts " => #{inspect Average.mode(list)}"
end)