-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.go
55 lines (51 loc) · 941 Bytes
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package next
import "math"
type base interface {
of(int) <-chan []interface{}
}
func count(b base, r int) int {
switch c := b.(type) {
case combination:
n := len(c)
if r > n {
return 0
}
var t = 1
for i := 1; i <= r; i++ {
t = t * (n - r + i) / i
}
return t
case permutation:
n := len(c)
if r > n {
return 0
}
var t = 1
for i := n - r + 1; i <= n; i++ {
t = t * i
}
return t
case repeatCombination:
n := len(c)
var t = 1
for i := r + 1; i < n+r; i++ {
t = t * i
}
for i := 1; i < n; i++ {
t = t / i
}
return t
case repeatPermutation:
return int(math.Pow(float64(len(c)), float64(r)))
}
return 0
}
// Creates a new result using the selected indexes and sends them to the channel
func sendIndex(base []interface{}, index []int, ch chan<- []interface{}) {
r := len(index)
res := make([]interface{}, r)
for i, idx := range index {
res[i] = base[idx]
}
ch <- res
}