mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.1 KiB
65 lines
1.1 KiB
5 years ago
|
package main
|
||
|
|
||
|
import (
|
||
5 years ago
|
"fmt"
|
||
|
|
||
5 years ago
|
"./student"
|
||
5 years ago
|
)
|
||
|
|
||
5 years ago
|
func foldInt(f func(int, int) int, a []int, n int) {
|
||
|
result := n
|
||
|
for _, v := range a {
|
||
|
result = f(result, v)
|
||
|
}
|
||
|
fmt.Println(result)
|
||
|
}
|
||
|
|
||
5 years ago
|
func main() {
|
||
5 years ago
|
f := []func(int, int) int{
|
||
|
func(accumulator, currentValue int) int {
|
||
|
return accumulator + currentValue
|
||
|
},
|
||
|
func(accumulator, currentValue int) int {
|
||
|
return accumulator - currentValue
|
||
|
},
|
||
|
func(accumulator, currentValue int) int {
|
||
|
return currentValue * accumulator
|
||
|
},
|
||
|
}
|
||
5 years ago
|
|
||
|
type node struct {
|
||
5 years ago
|
a []int
|
||
5 years ago
|
functions []func(int, int) int
|
||
|
n int
|
||
|
}
|
||
|
argInt := []int{}
|
||
|
table := []node{}
|
||
|
|
||
|
for i := 0; i < 8; i++ {
|
||
5 years ago
|
argInt = append(argInt, lib.MultRandIntBetween(0, 50)...)
|
||
5 years ago
|
table = append(table, node{
|
||
|
a: argInt,
|
||
5 years ago
|
functions: f,
|
||
5 years ago
|
n: lib.RandIntBetween(0, 60),
|
||
5 years ago
|
})
|
||
5 years ago
|
}
|
||
|
|
||
|
table = append(table, node{
|
||
5 years ago
|
a: []int{1, 2, 3},
|
||
5 years ago
|
functions: f,
|
||
|
n: 93,
|
||
|
})
|
||
|
|
||
|
table = append(table, node{
|
||
5 years ago
|
a: []int{0},
|
||
5 years ago
|
functions: f,
|
||
|
n: 93,
|
||
|
})
|
||
|
|
||
|
for _, v := range table {
|
||
|
for _, f := range v.functions {
|
||
5 years ago
|
lib.Challenge("FoldInt", student.FoldInt, foldInt, f, v.a, v.n)
|
||
5 years ago
|
}
|
||
|
}
|
||
|
}
|