Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["shuffle_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
)
go_library(
name = "go_default_library",
srcs = ["shuffle.go"],
importpath = "go-common/app/service/main/bns/lib/shuffle",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,26 @@
package shuffle
import (
"math/rand"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// Shuffler A type, typically a collection, that satisfies Shuffler
// can be shuffle by Shuffle func
type Shuffler interface {
Len() int
Swap(i, j int)
}
// Shuffle s
func Shuffle(s Shuffler) {
l := s.Len()
for i := l; i > 0; i-- {
j := rand.Intn(i)
s.Swap(l-i, j)
}
}

View File

@@ -0,0 +1,27 @@
package shuffle
import (
"strings"
"testing"
)
type List []string
func (l List) Len() int {
return len(l)
}
func (l List) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
func TestShuffle(t *testing.T) {
l := List{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
old := strings.Join(l, "")
Shuffle(l)
new := strings.Join(l, "")
if old == new {
t.Errorf("shuffle error, %s == %s", old, new)
}
t.Log(new)
}