gpt4 book ai didi

围棋排列泵

转载 作者:IT王子 更新时间:2023-10-29 01:11:57 26 4
gpt4 key购买 nike

我只是想知道是否有一种好的方法可以根据问题将“//如果长度不相等 - false”更改为“//如果其中一个数字丢失 = false”编码。

package main

import (
"fmt"
"sort"
)

type RuneSlice []rune

func (p RuneSlice) Len() int { return len(p) }
func (p RuneSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p RuneSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }

func isPumpung(str1, str2 string) bool {
// if lengths are not equal - false
if len(str1) == len(str2) {
return false
}

// sort both strings/runes
var rune1 RuneSlice = []rune(str1)
var rune2 RuneSlice = []rune(str2)

sort.Sort(rune1)
sort.Sort(rune2)

//fmt.Println(string(rune1[:]))
//fmt.Println(string(rune2[:]))

// compare rune1 and rune 2 by indexes
for i := 0; i < len(rune1); i++ {
if rune1[i] != rune2[i] {
return false
}
}

return true
}

func main() {
fmt.Println("18,19,20,21 and 21,20,18,20,19,18,20 is permutation of each other : ", isPumpung("18,19,20,21", "21,20,18,20,19,18,20"))
fmt.Println("18,19,20,21 and 21,20,18,20,18,20 is permutation of each other : ", isPumpung("18,19,20,21", "21,20,18,20,18,20"))

}

Playground :https://play.golang.org/p/n_bPNSFCr59

Go语言问题:

pumpung 是连续整数的排列,可能包含重复项。为了例如,[21, 20, 18, 20, 19, 18, 20] 是 pumpung,因为它是 [18, …, 21] 的排列重复 18 和 20。然而, [21, 20, 18, 20, 18, 20] 不是。编写一个函数 IsPumpung(list),检查给定的列表是否是 pumpung,返回如果是,则为真。

最佳答案

if one of the number missing = false


例如,

// A pumpung is a permutation of consecutive integers, possibly with repeated items.
// For example, [21, 20, 18, 20, 19, 18, 20] is pumpung
// since it is a permutation of [18, …, 21] with 18 and 20 repeated.
// However, [21, 20, 18, 20, 18, 20] is not.

package main

import "fmt"

const (
maxInt = int(^uint(0) >> 1)
minInt = -maxInt - 1
)

func isPumpung(a []int) (min, max int, pumpung bool) {
min, max = maxInt, minInt
p := make(map[int]bool)
for _, e := range a {
p[e] = true
if min > e {
min = e
}
if max < e {
max = e
}
}
pumpung = max-min+1 == len(p)
if !pumpung {
min, max = 0, 0
}
return min, max, pumpung
}

func isPumpungEqual(a1, a2 []int) bool {
min1, max1, pumpung1 := isPumpung(a1)
if !pumpung1 {
return false
}
min2, max2, pumpung2 := isPumpung(a2)
if !pumpung2 {
return false
}
return min1 == min2 && max1 == max2
}

func main() {
a1 := []int{18, 19, 20, 21}
fmt.Print(a1, " ")
fmt.Println(isPumpung(a1))
a2 := []int{21, 20, 18, 20, 18, 20}
fmt.Print(a2, " ")
fmt.Println(isPumpung(a2))
a3 := []int{21, 20, 18, 20, 19, 18, 20}
fmt.Print(a3, " ")
fmt.Println(isPumpung(a3))

fmt.Println()
fmt.Println(a1, a2, isPumpungEqual(a1, a2))
fmt.Println(a1, a3, isPumpungEqual(a1, a3))
}

Playground :https://play.golang.org/p/ExtmRhX_utC

输出:

[18 19 20 21] 18 21 true
[21 20 18 20 18 20] 0 0 false
[21 20 18 20 19 18 20] 18 21 true

[18 19 20 21] [21 20 18 20 18 20] false
[18 19 20 21] [21 20 18 20 19 18 20] true

关于围棋排列泵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54719550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com