mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 22:54:43 +01:00
20201216
This commit is contained in:
31
20201216/main.go
Normal file
31
20201216/main.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
|
||||
func process(n int) []string {
|
||||
m := make(map[string]struct{})
|
||||
for i := 2; i <= n; i++ {
|
||||
for j := 1; j < i; j++ {
|
||||
g := findGCD(i, j)
|
||||
m[fmt.Sprintf("%d/%d", j/g, i/g)] = struct{}{}
|
||||
}
|
||||
}
|
||||
var ret []string
|
||||
for k := range m {
|
||||
ret = append(ret, k)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func findGCD(a, b int) int {
|
||||
for a > 0 {
|
||||
t := a
|
||||
a = b % a
|
||||
b = t
|
||||
}
|
||||
return b
|
||||
}
|
||||
63
20201216/main_test.go
Normal file
63
20201216/main_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_process(t *testing.T) {
|
||||
type args struct {
|
||||
n int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
"test1",
|
||||
args{
|
||||
n: 2,
|
||||
},
|
||||
[]string{"1/2"},
|
||||
},
|
||||
{
|
||||
"test2",
|
||||
args{
|
||||
n: 3,
|
||||
},
|
||||
[]string{"1/2", "1/3", "2/3"},
|
||||
},
|
||||
{
|
||||
"test3",
|
||||
args{
|
||||
n: 4,
|
||||
},
|
||||
[]string{"1/2", "1/3", "1/4", "2/3", "3/4"},
|
||||
},
|
||||
{
|
||||
"test4",
|
||||
args{
|
||||
n: 1,
|
||||
},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"test5",
|
||||
args{
|
||||
n: 9,
|
||||
},
|
||||
[]string{"1/2", "1/3", "1/4", "1/5", "1/6", "1/7", "1/8", "1/9", "2/3", "2/5", "2/7", "2/9", "3/4", "3/5", "3/7", "3/8", "4/5", "4/7", "4/9", "5/6", "5/7", "5/8", "5/9", "6/7", "7/8", "7/9", "8/9"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := process(tt.args.n)
|
||||
sort.Strings(got)
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("process() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user