Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
69
Task/Statistics-Basic/FreeBASIC/statistics-basic.freebasic
Normal file
69
Task/Statistics-Basic/FreeBASIC/statistics-basic.freebasic
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Randomize
|
||||
|
||||
Sub basicStats(sampleSize As Integer)
|
||||
If sampleSize < 1 Then Return
|
||||
Dim r(1 To sampleSize) As Double
|
||||
Dim h(0 To 9) As Integer '' all zero by default
|
||||
Dim sum As Double = 0.0
|
||||
Dim hSum As Integer = 0
|
||||
|
||||
' Generate 'sampleSize' random numbers in the interval [0, 1)
|
||||
' calculate their sum
|
||||
' and in which box they will fall when drawing the histogram
|
||||
For i As Integer = 1 To sampleSize
|
||||
r(i) = Rnd
|
||||
sum += r(i)
|
||||
h(Int(r(i) * 10)) += 1
|
||||
Next
|
||||
|
||||
For i As Integer = 0 To 9 : hSum += h(i) : Next
|
||||
' adjust one of the h() values if necessary to ensure hSum = sampleSize
|
||||
Dim adj As Integer = sampleSize - hSum
|
||||
If adj <> 0 Then
|
||||
For i As Integer = 0 To 9
|
||||
h(i) += adj
|
||||
If h(i) >= 0 Then Exit For
|
||||
h(i) -= adj
|
||||
Next
|
||||
End If
|
||||
|
||||
Dim mean As Double = sum / sampleSize
|
||||
|
||||
Dim sd As Double
|
||||
sum = 0.0
|
||||
' Now calculate their standard deviation
|
||||
For i As Integer = 1 To sampleSize
|
||||
sum += (r(i) - mean) ^ 2.0
|
||||
Next
|
||||
sd = Sqr(sum/sampleSize)
|
||||
|
||||
' Draw a histogram of the data with interval 0.1
|
||||
Dim numStars As Integer
|
||||
' If sample size > 500 then normalize histogram to 500
|
||||
Dim scale As Double = 1.0
|
||||
If sampleSize > 500 Then scale = 500.0 / sampleSize
|
||||
Print "Sample size "; sampleSize
|
||||
Print
|
||||
Print Using " Mean #.######"; mean;
|
||||
Print Using " SD #.######"; sd
|
||||
Print
|
||||
For i As Integer = 0 To 9
|
||||
Print Using " #.## : "; i/10.0;
|
||||
Print Using "##### " ; h(i);
|
||||
numStars = Int(h(i) * scale + 0.5)
|
||||
Print String(numStars, "*")
|
||||
Next
|
||||
End Sub
|
||||
|
||||
basicStats 100
|
||||
Print
|
||||
basicStats 1000
|
||||
Print
|
||||
basicStats 10000
|
||||
Print
|
||||
basicStats 100000
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
11
Task/Statistics-Basic/Hy/statistics-basic.hy
Normal file
11
Task/Statistics-Basic/Hy/statistics-basic.hy
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(import
|
||||
[numpy.random [random]]
|
||||
[numpy [mean std]]
|
||||
[matplotlib.pyplot :as plt])
|
||||
|
||||
(for [n [100 1000 10000]]
|
||||
(setv v (random n))
|
||||
(print "Mean:" (mean v) "SD:" (std v)))
|
||||
|
||||
(plt.hist (random 1000))
|
||||
(plt.show)
|
||||
51
Task/Statistics-Basic/Lasso/statistics-basic.lasso
Normal file
51
Task/Statistics-Basic/Lasso/statistics-basic.lasso
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
define stat1(a) => {
|
||||
if(#a->size) => {
|
||||
local(mean = (with n in #a sum #n) / #a->size)
|
||||
local(sdev = math_pow(((with n in #a sum Math_Pow((#n - #mean),2)) / #a->size),0.5))
|
||||
return (:#sdev, #mean)
|
||||
else
|
||||
return (:0,0)
|
||||
}
|
||||
}
|
||||
define stat2(a) => {
|
||||
if(#a->size) => {
|
||||
local(sx = 0, sxx = 0)
|
||||
with x in #a do => {
|
||||
#sx += #x
|
||||
#sxx += #x*#x
|
||||
}
|
||||
local(sdev = math_pow((#a->size * #sxx - #sx * #sx),0.5) / #a->size)
|
||||
return (:#sdev, #sx / #a->size)
|
||||
else
|
||||
return (:0,0)
|
||||
}
|
||||
}
|
||||
define histogram(a) => {
|
||||
local(
|
||||
out = '\r',
|
||||
h = array(0,0,0,0,0,0,0,0,0,0,0),
|
||||
maxwidth = 50,
|
||||
sc = 0
|
||||
)
|
||||
with n in #a do => {
|
||||
#h->get(integer(#n*10)+1) += 1
|
||||
}
|
||||
local(mx = decimal(with n in #h max #n))
|
||||
with i in #h do => {
|
||||
#out->append((#sc/10.0)->asString(-precision=1)+': '+('+' * integer(#i / #mx * #maxwidth))+'\r')
|
||||
#sc++
|
||||
}
|
||||
return #out
|
||||
}
|
||||
|
||||
with scale in array(100,1000,10000,100000) do => {^
|
||||
local(n = array)
|
||||
loop(#scale) => { #n->insert(decimal_random) }
|
||||
local(sdev1,mean1) = stat1(#n)
|
||||
local(sdev2,mean2) = stat2(#n)
|
||||
#scale' numbers:\r'
|
||||
'Naive method: sd: '+#sdev1+', mean: '+#mean1+'\r'
|
||||
'Second method: sd: '+#sdev2+', mean: '+#mean2+'\r'
|
||||
histogram(#n)
|
||||
'\r\r'
|
||||
^}
|
||||
32
Task/Statistics-Basic/Nim/statistics-basic.nim
Normal file
32
Task/Statistics-Basic/Nim/statistics-basic.nim
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import math, strutils
|
||||
randomize()
|
||||
|
||||
proc sd(ns): auto =
|
||||
var sx, sxx = 0.0
|
||||
for x in ns:
|
||||
sx += x
|
||||
sxx += x * x
|
||||
let sd = if ns.len > 0: sqrt(float(ns.len) * sxx - sx * sx) / float(ns.len)
|
||||
else: 0
|
||||
(sd, sx / float(ns.len))
|
||||
|
||||
proc histogram(ns) =
|
||||
var h = newSeq[int](10)
|
||||
for n in ns:
|
||||
let pos = int(n * 10)
|
||||
inc h[pos]
|
||||
|
||||
const maxWidth = 50
|
||||
let mx = max(h)
|
||||
echo ""
|
||||
for n, i in h:
|
||||
echo n/10,": ",repeatChar(int(i / mx * maxWidth), '+')
|
||||
echo ""
|
||||
|
||||
for i in [10, 100, 1_000, 10_000, 100_000]:
|
||||
var n = newSeq[float](i)
|
||||
for x in 0..n.high: n[x] = random(1.0)
|
||||
echo "\n##\n## ",i," numbers\n##"
|
||||
let (sd, mean) = sd(n)
|
||||
echo "sd: ",sd,", mean: ",mean
|
||||
histogram(n)
|
||||
15
Task/Statistics-Basic/Oforth/statistics-basic.oforth
Normal file
15
Task/Statistics-Basic/Oforth/statistics-basic.oforth
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
: main(n)
|
||||
| l m std i nb |
|
||||
|
||||
// Create list and calculate avg and stddev
|
||||
ListBuffer init(n, #[ Float rand ]) dup ->l avg ->m
|
||||
0 l apply(#[ sq +]) n / m sq - sqrt ->std
|
||||
System.Out "n = " << n << ", avg = " << m << ", std = " << std << cr
|
||||
|
||||
// Histo
|
||||
0.0 0.9 0.1 step: i [
|
||||
l count(#[ between(i, i 0.1 +) ]) 400 * n / asInteger ->nb
|
||||
System.Out i <<wjp(3, JUSTIFY_RIGHT, 2) " - " <<
|
||||
i 0.1 + <<wjp(3, JUSTIFY_RIGHT, 2) " - " <<
|
||||
StringBuffer new "*" <<n(nb) << cr
|
||||
] ;
|
||||
26
Task/Statistics-Basic/Sidef/statistics-basic.sidef
Normal file
26
Task/Statistics-Basic/Sidef/statistics-basic.sidef
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
func generate_statistics(n) {
|
||||
var(sum=0, sum2=0);
|
||||
var hist = 10.of(0);
|
||||
|
||||
n.times {
|
||||
var r = 1.rand;
|
||||
sum += r;
|
||||
sum2 += r**2;
|
||||
hist[10*r] += 1;
|
||||
}
|
||||
|
||||
var mean = sum/n;
|
||||
var stddev = Math.sqrt(sum2/n - mean**2);
|
||||
|
||||
say "size: #{n}";
|
||||
say "mean: #{mean}";
|
||||
say "stddev: #{stddev}";
|
||||
|
||||
var max = hist.max;
|
||||
hist.range.each {|i|
|
||||
printf("%.1f:%s\n", 0.1*i, "=" * 70*hist[i]/max);
|
||||
}
|
||||
print "\n";
|
||||
}
|
||||
|
||||
[100, 1000, 10000].each {|n| generate_statistics(n) }
|
||||
Loading…
Add table
Add a link
Reference in a new issue