Commit 9482dd4e by zhengqiuyun86

数组差集算法

parent 1dfe2ff0
......@@ -17,4 +17,5 @@ require (
github.com/onsi/gomega v1.20.0 // indirect
github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a
github.com/swaggo/gin-swagger v1.5.2
gopkg.in/fatih/set.v0 v0.2.1
)
......@@ -317,6 +317,8 @@ gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fatih/set.v0 v0.2.1 h1:Xvyyp7LXu34P0ROhCyfXkmQCAoOUKb1E2JS9I7SE5CY=
gopkg.in/fatih/set.v0 v0.2.1/go.mod h1:5eLWEndGL4zGGemXWrKuts+wTJR0y+w+auqUJZbmyBg=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
......
//package main
//
//import (
// "git.168cad.top/zhengqiuyun/rym-util/conf"
// "git.168cad.top/zhengqiuyun/rym-util/es"
// "git.168cad.top/zhengqiuyun/rym-util/log"
// "git.168cad.top/zhengqiuyun/rym-util/mysqlrym"
// "git.168cad.top/zhengqiuyun/rym-util/redis"
// "git.168cad.top/zhengqiuyun/rym-util/router"
//)
//
//func main() {
// log.Debug("测试")
// log.Info("测试")
// log.Warn("测试")
// log.Error("测试")
// conf.BaseConfig(conf.Config{
// ServerName: "rym-framework",
// Env: "dev",
// HttpPort: 1024,
// LogColorful: true,
// })
// mysqlrym.TestLog()
// es.TestLog()
// redis.TestLog()
// c := router.InitGinEngine(true)
// router.Run(c)
//}
package main
import (
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/es"
"git.168cad.top/zhengqiuyun/rym-util/log"
"git.168cad.top/zhengqiuyun/rym-util/mysqlrym"
"git.168cad.top/zhengqiuyun/rym-util/redis"
"git.168cad.top/zhengqiuyun/rym-util/router"
"fmt"
"gopkg.in/fatih/set.v0"
)
/*set并集 交集 差集计算示例*/
func main() {
log.Debug("测试")
log.Info("测试")
log.Warn("测试")
log.Error("测试")
conf.BaseConfig(conf.Config{
ServerName: "rym-framework",
Env: "dev",
HttpPort: 1024,
LogColorful: true,
})
mysqlrym.TestLog()
es.TestLog()
redis.TestLog()
c := router.InitGinEngine(true)
router.Run(c)
a := set.New(set.ThreadSafe)
a.Add(1)
a.Add(2)
a.Add(3)
b := set.New(set.ThreadSafe)
b.Add(2)
b.Add(3)
b.Add(4)
//并集
unionSet := set.Union(a, b)
fmt.Printf("并集:%v\n", unionSet)
//交集
intersectionSet := set.Intersection(a, b)
fmt.Printf("交集:%v\n", intersectionSet)
//差集
diffS1S2 := set.Difference(a, b)
fmt.Printf("差集(属a不属b):%v\n", diffS1S2)
diffS2S1 := set.Difference(b, a)
fmt.Printf("差集(属b不属a):%v\n", diffS2S1)
}
......@@ -29,6 +29,9 @@ func Int16s(p int16) *int16 {
func Int8s(p int8) *int8 {
return &p
}
func Ints(p int) *int {
return &p
}
func Float64s(p float64) *float64 {
return &p
}
......
package util
import "time"
import (
"time"
)
func ArrayContains(array []string, e string) bool {
for _, v := range array {
......@@ -74,6 +76,14 @@ func ArrayContainsInt8(array []int8, e int8) bool {
}
return false
}
func ArrayContainsInt(array []int, e int) bool {
for _, v := range array {
if v == e {
return true
}
}
return false
}
func ArrayContainsFloat64(array []float64, e float64) bool {
for _, v := range array {
if v == e {
......@@ -106,3 +116,91 @@ func ArrayContainsTime(array []time.Time, e time.Time) bool {
}
return false
}
func DifferenceSetInt64(a1, a2 []int64) (d1, d2 []int64) {
var vMap = map[int64]*int{}
for _, v1 := range a1 {
vMap[v1] = Ints(1)
}
for _, v2 := range a2 {
if vMap[v2] == nil {
vMap[v2] = Ints(2)
} else {
vMap[v2] = Ints(0)
}
}
for k, v := range vMap {
if *v == 1 {
d1 = append(d1, k)
} else if *v == 2 {
d2 = append(d2, k)
}
}
return d1, d2
}
func DifferenceSetInt32(a1, a2 []int32) (d1, d2 []int32) {
var vMap = map[int32]*int{}
for _, v1 := range a1 {
vMap[v1] = Ints(1)
}
for _, v2 := range a2 {
if vMap[v2] == nil {
vMap[v2] = Ints(2)
} else {
vMap[v2] = Ints(0)
}
}
for k, v := range vMap {
if *v == 1 {
d1 = append(d1, k)
} else if *v == 2 {
d2 = append(d2, k)
}
}
return d1, d2
}
func DifferenceSetInt(a1, a2 []int) (d1, d2 []int) {
var vMap = map[int]*int{}
for _, v1 := range a1 {
vMap[v1] = Ints(1)
}
for _, v2 := range a2 {
if vMap[v2] == nil {
vMap[v2] = Ints(2)
} else {
vMap[v2] = Ints(0)
}
}
for k, v := range vMap {
if *v == 1 {
d1 = append(d1, k)
} else if *v == 2 {
d2 = append(d2, k)
}
}
return d1, d2
}
func DifferenceSet(a1, a2 []string) (d1, d2 []string) {
var vMap = map[string]*int{}
for _, v1 := range a1 {
vMap[v1] = Ints(1)
}
for _, v2 := range a2 {
if vMap[v2] == nil {
vMap[v2] = Ints(2)
} else {
vMap[v2] = Ints(0)
}
}
for k, v := range vMap {
if *v == 1 {
d1 = append(d1, k)
} else if *v == 2 {
d2 = append(d2, k)
}
}
return d1, d2
}
......@@ -2,14 +2,32 @@ package util
import (
"fmt"
"net/http"
"testing"
"time"
)
func TestArrayContainsInterface(t *testing.T) {
var areaIds []time.Time
//areaIds = append(areaIds, PareDateTime("2022-02-09 22:09:12"))
//areaIds = append(areaIds, PareDateTime("2022-03-09 22:09:12"))
//areaIds = append(areaIds, PareDateTime("2022-12-09 22:09:12"))
areaIds = append(areaIds, PareDateTime("2022-02-09 22:09:12"))
areaIds = append(areaIds, PareDateTime("2022-03-09 22:09:12"))
areaIds = append(areaIds, PareDateTime("2022-12-09 22:09:12"))
fmt.Println(ArrayContainsTime(areaIds, PareDateTime("2022-02-10 22:09:12")))
}
func TestDifference(t *testing.T) {
a1 := []int64{1, 5, 2, 3}
a2 := []int64{2, 1, 34, 3, 3, 4}
d1, d2 := DifferenceSetInt64(a1, a2)
fmt.Println("a1与a2的差集分别是:", d1, d2)
}
func TestPostJSONStr(t *testing.T) {
responseData, err := PostFormStr("http://ex-test-dcxy-base-manage.168cad.top/v3/operate/balance/recharge/success", "{\"orderNo\":\"BRO165119944230021\"}", nil, http.Client{
Timeout: 10 * time.Second,
})
if err != nil {
fmt.Printf("错误:%s\n", err.Error())
}
fmt.Printf("请求结果:%s\n", string(responseData))
}
package util
import (
"bytes"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/exception"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
func ParamInt64(c *gin.Context, key string) int64 {
......@@ -95,3 +99,61 @@ func ParamB(c *gin.Context, key string) string {
v := c.Query(key)
return v
}
const (
ApplicationJSON = "application/json"
XmlForm = "application/x-www-form-urlencoded;charset=utf-8"
)
func PostFormStr(url string, params string, header map[string]string, client http.Client) ([]byte, error) {
p := StrToUrlValue(params)
requestStr := p.Encode()
buf := strings.NewReader(requestStr)
request, err := http.NewRequest(http.MethodPost, url, buf)
if err != nil {
return nil, err
}
request.Header.Add("Content-Type", XmlForm)
if header != nil {
for k, v := range header {
request.Header.Add(k, v)
}
}
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return data, err
}
func PostJSONStr(url string, params string, header map[string]string, client http.Client) ([]byte, error) {
request, err := http.NewRequest(http.MethodPost, url, bytes.NewBufferString(params))
if err != nil {
return nil, err
}
request.Header.Add("Content-Type", ApplicationJSON)
if header != nil {
for k, v := range header {
request.Header.Add(k, v)
}
}
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return data, err
}
......@@ -43,3 +43,7 @@ func containsForSplit(list, item, character string) bool {
}
return false
}
func SubStr(str string, begin, end int) {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment