Add all the A tasks
This commit is contained in:
parent
2dd7375f96
commit
051504d65b
1608 changed files with 18584 additions and 0 deletions
|
|
@ -0,0 +1,34 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func sma(n int) func(float64) float64 {
|
||||
s := make([]float64, 0, n)
|
||||
i, sum, rn := 0, 0., 1/float64(n)
|
||||
return func(x float64) float64 {
|
||||
if len(s) < n {
|
||||
sum += x
|
||||
s = append(s, x)
|
||||
return sum / float64(len(s))
|
||||
}
|
||||
s[i] = x
|
||||
i++
|
||||
if i == n {
|
||||
i = 0
|
||||
}
|
||||
sum = 0
|
||||
for _, x = range s {
|
||||
sum += x
|
||||
}
|
||||
return sum * rn
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
sma3 := sma(3)
|
||||
sma5 := sma(5)
|
||||
fmt.Println("x sma3 sma5")
|
||||
for _, x := range []float64{1, 2, 3, 4, 5, 5, 4, 3, 2, 1} {
|
||||
fmt.Printf("%5.3f %5.3f %5.3f\n", x, sma3(x), sma5(x))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue