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,9 @@
: avg \ a -- avg(a)
dup ' n:+ 0 a:reduce
swap a:len nip n:/ ;
\ test:
[ 1.0, 2.3, 1.1, 5.0, 3, 2.8, 2.01, 3.14159 ] avg . cr
[ ] avg . cr
[ 10 ] avg . cr
bye

View file

@ -0,0 +1 @@
avg[list]

View file

@ -0,0 +1,6 @@
AveVal(SET OF INTEGER s) := AVE(s);
//example usage
SetVals := [14,9,16,20,91];
AveVal(SetVals) //returns 30.0 ;

View file

@ -0,0 +1,21 @@
(lib 'math)
(mean '(1 2 3 4)) ;; mean of a list
→ 2.5
(mean #(1 2 3 4)) ;; mean of a vector
→ 2.5
(lib 'sequences)
(mean [1 3 .. 10]) ;; mean of a sequence
→ 5
;; error handling
(mean 'elvis)
⛔ error: mean : expected sequence : elvis
(mean ())
💣 error: mean : null is not an object
(mean #())
😐 warning: mean : zero-divide : empty-vector
→ 0
(mean [2 2 .. 2])
😁 warning: mean : zero-divide : empty-sequence
→ 0

View file

@ -0,0 +1,42 @@
' FB 1.05.0 Win64
Function Mean(array() As Double) As Double
Dim length As Integer = Ubound(array) - Lbound(array) + 1
If length = 0 Then
Return 0.0/0.0 'NaN
End If
Dim As Double sum = 0.0
For i As Integer = LBound(array) To UBound(array)
sum += array(i)
Next
Return sum/length
End Function
Function IsNaN(number As Double) As Boolean
Return Str(number) = "-1.#IND" ' NaN as a string in FB
End Function
Dim As Integer n, i
Dim As Double num
Print "Sample input and output"
Print
Do
Input "How many numbers are to be input ? : ", n
Loop Until n > 0
Dim vector(1 To N) As Double
Print
For i = 1 to n
Print " Number #"; i; " : ";
Input "", vector(i)
Next
Print
Print "Mean is"; Mean(vector())
Print
Erase vector
num = Mean(vector())
If IsNaN(num) Then
Print "After clearing the vector, the mean is 'NaN'"
End If
Print
Print "Press any key to quit the program"
Sleep

View file

@ -0,0 +1,7 @@
R (n) P ;
0
1, n rep (i)
R P +
]
n div
P

View file

@ -0,0 +1,23 @@
DIM a%(10)
FOR i%=0 TO 10
a%(i%)=i%*2
PRINT "element ";i%;" is ";a%(i%)
NEXT i%
PRINT "mean is ";@mean(a%)
'
FUNCTION mean(a%)
LOCAL i%,size%,sum
' find size of array,
size%=DIM?(a%())
' return 0 for empty arrays
IF size%<=0
RETURN 0
ENDIF
' find sum of all elements
sum=0
FOR i%=0 TO size%-1
sum=sum+a%(i%)
NEXT i%
' mean is sum over size
RETURN sum/size%
ENDFUNC

View file

@ -0,0 +1,3 @@
(defn arithmetic-mean [xs]
(if xs
(/ (sum xs) (len xs))))

View file

@ -0,0 +1,3 @@
(defun mean (data)
(/ (lists:sum data)
(length data)))

View file

@ -0,0 +1,8 @@
> (mean '(1 1))
1.0
> (mean '(1 2))
1.5
> (mean '(2 10))
6.0
> (mean '(6 12 18 24 30 36 42 48 54 60 66 72 78))
42.0

View file

@ -0,0 +1,3 @@
(defmacro mean args
`(/ (lists:sum ,args)
,(length args)))

View file

@ -0,0 +1,6 @@
> (mean 42)
42.0
> (mean 18 66)
42.0
> (mean 6 12 18 24 30 36 42 48 54 60 66 72 78)
42.0

View file

@ -0,0 +1,8 @@
define average(a::array) => {
not #a->size ? return 0
local(x = 0.0)
with i in #a do => { #x += #i }
return #x / #a->size
}
average(array(1,2,5,17,7.4)) //6.48

View file

@ -0,0 +1,2 @@
average(1,2,3,4,5) -- 3
average(empty) -- 0

View file

@ -0,0 +1,11 @@
import strutils
proc mean(xs): float =
for x in xs:
result += x
result = result / float(xs.len)
var v = @[1.0, 2.0, 2.718, 3.0, 3.142]
for i in 0..5:
echo "mean of first ", v.len, " = ", formatFloat(mean(v), precision = 0)
v.setLen(v.high)

View file

@ -0,0 +1,2 @@
[1, 2, 2.718, 3, 3.142] avg println
[ ] avg println

View file

@ -0,0 +1,6 @@
function mean(sequence s)
if length(s)=0 then return 0 end if
return sum(s)/length(s)
end function
? mean({1, 2, 5, -5, -9.5, 3.14159})

View file

@ -0,0 +1,9 @@
nums = [1,2,3,4,5,6,7,8,9,10]
sum = 0
see "Average = " + average(nums) + nl
func average numbers
for i = 1 to len(numbers)
sum = sum + nums[i]
next
return sum/len(numbers)

View file

@ -0,0 +1,10 @@
func avg(Array list) {
list.len > 0 || return 0;
list.sum / list.len;
}
say avg([Math.inf, Math.inf]);
say avg([3,1,4,1,5,9]);
say avg([1e+20, 3, 1, 4, 1, 5, 9, -1e+20]);
say avg([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0.11]);
say avg([10, 20, 30, 40, 50, -100, 4.7, -1100]);

View file

@ -0,0 +1,6 @@
func meanDoubles(s: [Double]) -> Double {
return s.reduce(0, +) / Double(s.count)
}
func meanInts(s: [Int]) -> Double {
return meanDoubles(s.map{Double($0)})
}

View file

@ -0,0 +1,11 @@
#
# arithmetic mean
#
decl int<> input
decl int i
for (set i 1) (< i (size args)) (inc i)
append (int args<i>) input
end for
out (/ (+ input) (size input)) endl console

View file

@ -0,0 +1,2 @@
def (mean l)
sum.l / len.l

View file

@ -0,0 +1,12 @@
@let {
; using a fork (sum divided-by length)
mean1 @(@sum / #)
; using a function with a named argument
mean2 &a / @sum a #a
[[
!mean1 [3 1 4 1 5 9 2]
!mean2 [3 1 4 1 5 9 2]
]]
}

View file

@ -0,0 +1,7 @@
class Arithmetic {
static mean(arr) {
if (arr.count == 0) Fiber.abort("Length must be greater than zero")
return arr.reduce(Fn.new{ |x,y| x+y }) / arr.count
}
}
Arithmetic.mean([1,2,3,4,5]) // 3

View file

@ -0,0 +1,5 @@
(defun mean (v)
(if (= (vector-length v) 0)
nil
(let ((l (vector->list v)))
(/ (apply + l) (length l)))))

View file

@ -0,0 +1 @@
add/length

View file

@ -0,0 +1,3 @@
def mean: if length == 0 then null
else add/length
end;