Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,61 @@
PROGRAM MODE_AVG
!$INTEGER
DIM A[10],B[10],Z[10]
PROCEDURE SORT(Z[],P->Z[])
LOCAL N,FLIPS
FLIPS=TRUE
WHILE FLIPS DO
FLIPS=FALSE
FOR N=0 TO P-1 DO
IF Z[N]>Z[N+1] THEN SWAP(Z[N],Z[N+1]) FLIPS=TRUE
END FOR
END WHILE
END PROCEDURE
PROCEDURE CALC_MODE(Z[],P->MODES$)
LOCAL I,OCCURRENCE,MAXOCCURRENCE,OLDVAL
SORT(Z[],P->Z[])
OCCURENCE=1
MAXOCCURENCE=0
OLDVAL=Z[0]
MODES$=""
FOR I=1 TO P DO
IF Z[I]=OLDVAL THEN
OCCURENCE=OCCURENCE+1
ELSE
IF OCCURENCE>MAXOCCURENCE THEN
MAXOCCURENCE=OCCURENCE
MODES$=STR$(OLDVAL)
ELSIF OCCURENCE=MAXOCCURENCE THEN
MODES$=MODES$+STR$(OLDVAL)
ELSE
!$NULL
END IF
OCCURENCE=1
END IF
OLDVAL=Z[I]
END FOR
!check after loop
IF OCCURENCE>MAXOCCURENCE THEN
MAXOCCURENCE=OCCURENCE
MODES$=STR$(OLDVAL)
ELSIF OCCURENCE=MAXOCCURENCE THEN
MODES$=MODES$+STR$(OLDVAL)
ELSE
!$NULL
END IF
END PROCEDURE
BEGIN
A[]=(1,3,6,6,6,6,7,7,12,12,17)
B[]=(1,2,4,4,1)
PRINT("Modes for array A (1,3,6,6,6,6,7,7,12,12,17)";)
CALC_MODE(A[],10->MODES$)
PRINT(MODES$)
PRINT("Modes for array B (1,2,4,4,1)";)
CALC_MODE(B[],4->MODES$)
PRINT(MODES$)
END PROGRAM

View file

@ -0,0 +1,16 @@
(define (modes L)
(define G (group* L)) ;; sorts and group equal items
(define cardmax (for/max [(g G)] (length g)))
(map first (filter (lambda(g) (= cardmax (length g))) G)))
(modes '( a b c a d e f))
→ (a)
(modes (iota 6))
→ (0 1 2 3 4 5)
(modes '(x))
→ (x)
(modes '(🎾 🏉 ☕️ 🎾 🎲 🎯 🎺 ☕️ 🎲 🎸 🎻 🏆 ☕️ 🏁 🎾 🎲 🎻 🏉 ))
→ (🎾 ☕️ 🎲)
(modes '())
😖️ error: group : expected list : null 🔎 'modes'

View file

@ -0,0 +1,66 @@
' FB 1.05.0 Win64
Sub quicksort(a() As Integer, first As Integer, last As Integer)
Dim As Integer length = last - first + 1
If length < 2 Then Return
Dim pivot As Integer = a(first + length\ 2)
Dim lft As Integer = first
Dim rgt As Integer = last
While lft <= rgt
While a(lft) < pivot
lft +=1
Wend
While a(rgt) > pivot
rgt -= 1
Wend
If lft <= rgt Then
Swap a(lft), a(rgt)
lft += 1
rgt -= 1
End If
Wend
quicksort(a(), first, rgt)
quicksort(a(), lft, last)
End Sub
' The modal value(s) is/are stored in 'm'.
' The function returns the modal count.
Function mode(a() As Integer, m() As Integer, sorted As Boolean = false) As Integer
Dim lb As Integer = LBound(a)
Dim ub As Integer = UBound(a)
If ub = -1 Then Return 0 '' empty array
If Not sorted Then quicksort(a(), lb, ub)
Dim cValue As Integer = a(lb)
Dim cCount As Integer = 1
Dim cMax As Integer = 0
'' We iterate to the end of the array plus 1 to ensure the
'' final value is dealt with properly
For i As Integer = lb + 1 To ub + 1
If i <= ub AndAlso a(i) = cValue Then
cCount += 1
Else
If cCount > cMax Then
Erase m
Redim m(1 To 1)
m(1) = cValue
cMax = cCount
ElseIf cCount = cMax Then
Redim Preserve m(1 To UBound(m) + 1)
m(UBound(m)) = cValue
End If
If i = ub + 1 Then Exit For
cValue = a(i)
cCount = 1
End If
Next
Return cMax
End Function
Dim a(1 To 14) As Integer = {1, 2, 3, 1, 2, 4, 2, 5, 2, 3, 3, 1, 3, 6}
Dim m() As Integer '' to store the mode(s)
Dim mCount As Integer = mode(a(), m())
Print "The following are the modes which occur"; mCount; " times : "
For i As Integer = LBound(m) To UBound(m) : Print m(i); " "; : Next
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,12 @@
define getmode(a::array)::array => {
local(mmap = map, maxv = 0, modes = array)
// store counts
with e in #a do => { #mmap->keys >> #e ? #mmap->find(#e) += 1 | #mmap->insert(#e = 1) }
// get max value
with e in #mmap->keys do => { #mmap->find(#e) > #maxv ? #maxv = #mmap->find(#e) }
// get modes with max value
with e in #mmap->keys where #mmap->find(#e) == #maxv do => { #modes->insert(#e) }
return #modes
}
getmode(array(1,3,6,6,6,6,7,7,12,12,17))
getmode(array(1,3,6,3,4,8,9,1,2,3,2,2))

View file

@ -0,0 +1,10 @@
import tables
proc modes[T](xs: openArray[T]): T =
var count = initCountTable[T]()
for x in xs:
count.inc(x)
largest(count).key
echo modes(@[1,3,6,6,6,6,7,7,12,12,17])
echo modes(@[1,1,2,4,4])

View file

@ -0,0 +1,40 @@
function mode(sequence s)
-- returns a list of the most common values, each of which occurs the same number of times
object this
integer nxt = 1, count = 1, maxc = 1
sequence res = {}
if length(s)!=0 then
s = sort(s)
this = s[1]
for i=2 to length(s) do
if s[i]!=this then
s[nxt] = {count,this}
nxt += 1
this = s[i]
count = 1
else
count += 1
if count>maxc then
maxc = count
end if
end if
end for
s[nxt] = {count,this}
res = ""
for i=1 to nxt do
if s[i][1]=maxc then
res = append(res,s[i][2])
end if
end for
end if
return res
end function
?mode({1, 2, 5, -5, -9.5, 3.14159})
?mode({ 1, "blue", 2, 7.5, 5, "green", "red", 5, 2, "blue", "white" })
?mode({1, 2, 3, 1, 2, 4, 2, 5, 2, 3, 3, 1, 3, 6})
?mode({.2, .7, .1, .8, .2})
?mode({"two", 7, 1, 8, "two", 8})
?mode("Hello there world")
?mode({})
{} = wait_key()

View file

@ -0,0 +1,6 @@
func mode(array) {
var c = Hash.new;
array.each{|i| c{i} := 0 ++};
var max = c.values.max;
c.keys.grep{|i| c{i} == max};
}

View file

@ -0,0 +1,2 @@
say mode([1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17]).join(' ');
say mode([1, 1, 2, 4, 4]).join(' ');

View file

@ -0,0 +1,3 @@
func one_mode(arr) {
arr.max_by{|i| arr.count(i)};
}

View file

@ -0,0 +1,13 @@
class Arithmetic {
static mode(arr) {
var map = {}
for (e in arr) {
if (map[e] == null) map[e] = 0
map[e] = map[e] + 1
}
var max = map.values.reduce {|x, y| x > y ? x : y}
return map.keys.where {|x| map[x] == max}.toList
}
}
System.print(Arithmetic.mode([1,2,3,4,5,5,51,2,3]))

View file

@ -0,0 +1,19 @@
# modes/0 produces an array of [value, count]
# in increasing order of count:
def modes:
sort | reduce .[] as $i ([];
# state variable is an array of [value, count]
if length == 0 then [ [$i, 1] ]
elif .[-1][0] == $i then setpath([-1,1]; .[-1][1] + 1)
else . + [[$i,1]]
end )
| sort_by( .[1] );
# mode/0 outputs a stream of the modal values;
# if the input array is empty, the output stream is also empty.
def mode:
if length == 0 then empty
else modes as $modes
| $modes[-1][1] as $count
| $modes[] | select( .[1] == $count) | .[0]
end;

View file

@ -0,0 +1,3 @@
[1,2,3,1,2,1] | mode # => 1
[1,2,3,1,2,1,2] | mode # => 1 2
[1.1, 1.2, 1.3, 1.1, 1.2, 1.1] | mode) # => 1.1