-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscalesalgorithm.go
62 lines (57 loc) · 1.12 KB
/
scalesalgorithm.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
56
57
58
59
60
61
62
package scalesalgorithm
func GetScales(n, k, ref int) (scales [][]int) {
scales = [][]int{}
if k == 1 {
scale := []int{n}
return append(scales, scale)
}
for i := 1; i < n; i++ {
for _, element := range GetScales(n-i, k-1, ref) {
scale := append([]int{i}, element...)
if k != ref || !duplicate(scale, scales) {
scales = append(scales, scale)
}
}
}
return scales
}
func duplicate(scale []int, scales [][]int) bool {
for _, element := range scales {
if equivalent(scale, element) {
return true
}
}
return false
}
func equivalent(scale1, scale2 []int) bool {
if len(scale1) != len(scale2) {
return false
}
n := len(scale1)
for i := 0; i < n; i++ {
if identical(scale1, rotate(scale2, i)) {
return true
}
}
return false
}
func rotate(scale []int, r int) []int {
rotated := []int{}
n := len(scale)
for i := 0; i < n; i++ {
rotated = append(rotated, scale[(i+r)%n])
}
return rotated
}
func identical(scale1, scale2 []int) bool {
if len(scale1) != len(scale2) {
return false
}
n := len(scale1)
for i := 0; i < n; i++ {
if scale1[i] != scale2[i] {
return false
}
}
return true
}