Golang 比较两个字符串包含相同的词

我正在尝试检查一个数组是否包含另一个数组的特定单词,如下所示:

  • 示例:“3 of hearts”和“5 of hearts”匹配,因为它们都是红心并且应该返回 true。

  • 示例:“7 of hearts”和“7 of clubs”匹配,因为它们的值都是 7,应该返回 true。

  • 示例:“Jack of spades”仅匹配另一个“Jack of spades”并且应返回 true。

我将如何在 golang 中执行此操作。我尝试了一系列步骤,但我回到了原点,到目前为止我所拥有的是:

func compare(firstString, secondString string) bool {

    return false

}


compare("3 of hearts", "5 of hearts")

## This should return true


largeQ
浏览 165回答 3
3回答

慕尼黑的夜晚无繁华

我是 golang 的新手,但无需循环两次即可获得此信息的最佳方法是:func compare(firstString, secondString string) bool {    f, s := strings.Split(f, " "), strings.Split(secondString, " ")    if f[0] == s[0] || f[2] == f[2] {        return true    }    return false}compare("3 of hearts", "5 of hearts")## This should return truecompare("7 of hearts", "7 of clubs")## This should return truecompare("Jack of spades", "Jack of spades")## This should return truecompare("5 of hearts", "7 of clubs")## This should return false

波斯汪

func compare(f, s string) bool {    arr1, arr2 := strings.Split(f, " "), strings.Split(s, " ")    for _, v1 := range arr1 {        for _, v2 := range arr2 {            if v1 == v2 {                return true            }        }    }    return false}go playground

慕勒3428872

我不知道你真正想做什么。(我的意思是hello world很无聊)但是从问题中,至少我知道它可能与扑克有关。所以你应该首先设计与扑克牌相关的结构。然后你需要一个方法来从字符串中获取对象最后设计一个Equal函数的卡片,就大功告成了。type Card struct{}func GetCardFromStr(str string) (*Card, error)func (card *Card) Equal(card2 *Card, criteria criteria) bool示例代码package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "strings")type Suit uint8const (&nbsp; &nbsp; Spades Suit = iota + 1&nbsp; &nbsp; Hearts&nbsp; &nbsp; Diamonds&nbsp; &nbsp; Clubs)var suitMap map[string]Suitfunc init() {&nbsp; &nbsp; suitMap = map[string]Suit{"spades": Spades, "hearts": Hearts, "diamonds": Diamonds, "clubs": Clubs}}func StrToSuit(str string) Suit {&nbsp; &nbsp; if suit, exists := suitMap[strings.ToLower(str)]; exists {&nbsp; &nbsp; &nbsp; &nbsp; return suit&nbsp; &nbsp; }&nbsp; &nbsp; return 0}type Card struct {&nbsp; &nbsp; point int&nbsp; // 1-13 // ace, 2 to 10, Jack J, Queen Q, King K&nbsp; &nbsp; suit&nbsp; Suit // spades ♠️, hearts ♥️, diamonds ♦️, clubs ♣️ // emoji suit: https://emojipedia.org/search/?q=Suit}func NewCard(point int, suit Suit) (*Card, error) {&nbsp; &nbsp; if point < 0 || point > 13 {&nbsp; &nbsp; &nbsp; &nbsp; return nil, fmt.Errorf("illegal point: '%d', it should in the range: 1~13", point)&nbsp; &nbsp; }&nbsp; &nbsp; return &Card{point, suit}, nil // you can consider checking the suit.}func (card *Card) String() string {&nbsp; &nbsp; return fmt.Sprintf("%s%d", map[Suit]string{&nbsp; &nbsp; &nbsp; &nbsp; Spades:&nbsp; &nbsp;"♠️",&nbsp; &nbsp; &nbsp; &nbsp; Hearts:&nbsp; &nbsp;"♥️",&nbsp; &nbsp; &nbsp; &nbsp; Diamonds: "♦️",&nbsp; &nbsp; &nbsp; &nbsp; Clubs:&nbsp; &nbsp; "♣️",&nbsp; &nbsp; }[card.suit], card.point)}type criteria uint8const (&nbsp; &nbsp; Loose criteria = 1 << iota // one of them match&nbsp; &nbsp; // ... // others&nbsp; &nbsp; Strict // all match)func (card *Card) Equal(card2 *Card, criteria criteria) bool {&nbsp; &nbsp; if criteria == Strict {&nbsp; &nbsp; &nbsp; &nbsp; if card.point == card2.point && (card.suit == card2.suit && card.suit != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; }&nbsp; &nbsp; if card.point == card2.point || (card.suit == card2.suit && card.suit != 0) {&nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; }&nbsp; &nbsp; return false}func GetCardFromStr(str string) (*Card, error) {&nbsp; &nbsp; slice := strings.Split(str, " ")&nbsp; &nbsp; if slice == nil {&nbsp; &nbsp; &nbsp; &nbsp; return nil, fmt.Errorf("can't convert string to the card")&nbsp; &nbsp; }&nbsp; &nbsp; alphaMap := map[string]int{&nbsp; &nbsp; &nbsp; &nbsp; "ace":&nbsp; 1,&nbsp; &nbsp; &nbsp; &nbsp; "jack": 11, "queen": 12, "king": 13,&nbsp; &nbsp; }&nbsp; &nbsp; cardPoint := 0&nbsp; &nbsp; var cardSuit Suit&nbsp; &nbsp; for _, elem := range slice {&nbsp; &nbsp; &nbsp; &nbsp; elem = strings.ToLower(elem)&nbsp; &nbsp; &nbsp; &nbsp; if cardPoint == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkPoint := true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if point, exists := alphaMap[elem]; exists {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cardPoint = point&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkPoint = false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if checkPoint {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if point, err := strconv.Atoi(elem); err == nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cardPoint = point&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if cardSuit == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if suit := StrToSuit(elem); suit != 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cardSuit = suit&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if cardPoint == 0 {&nbsp; &nbsp; &nbsp; &nbsp; return nil, fmt.Errorf("can't convert string to the card (unknown point)")&nbsp; &nbsp; }&nbsp; &nbsp; if cardSuit == 0 {&nbsp; &nbsp; &nbsp; &nbsp; return nil, fmt.Errorf("can't convert string to the card (unknown suit)")&nbsp; &nbsp; }&nbsp; &nbsp; return NewCard(cardPoint, cardSuit)}func main() {&nbsp; &nbsp; for caseNumber, data := range []struct {&nbsp; &nbsp; &nbsp; &nbsp; s1 string&nbsp; &nbsp; &nbsp; &nbsp; s2 string&nbsp; &nbsp; &nbsp; &nbsp; criteria&nbsp; &nbsp; }{&nbsp; &nbsp; &nbsp; &nbsp; {"-5 hearts", "5 hearts", Loose},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // error illegal point: '-5', it should in the range: 1~13&nbsp; &nbsp; &nbsp; &nbsp; {"0", "", Loose},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // error can't convert string to the card (unknown point)&nbsp; &nbsp; &nbsp; &nbsp; {"3 of hearts", "3 of hearts", Loose},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// true&nbsp; &nbsp; &nbsp; &nbsp; {"3 of hearts", "5 of hearts", Loose},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// true&nbsp; &nbsp; &nbsp; &nbsp; {"7 of hearts", "7 of clubs", Loose},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // true&nbsp; &nbsp; &nbsp; &nbsp; {"Jack of spades", "Jack of spades", Strict},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // true&nbsp; &nbsp; &nbsp; &nbsp; {"Jack of spades", "Jack spades", Strict},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// true&nbsp; &nbsp; &nbsp; &nbsp; {"Jack of spades", "Jack hearts", Strict},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// false&nbsp; &nbsp; &nbsp; &nbsp; {"Jack of spades", "Jack", Strict},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // error can't convert string to the card (unknown suit)&nbsp; &nbsp; &nbsp; &nbsp; {"Jack of spades", "spades", Strict},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // error can't convert string to the card (unknown point)&nbsp; &nbsp; &nbsp; &nbsp; {"player Foo: 1 of clubs ", "player bar: I get an Ace of spades !!", Loose}, // true&nbsp; &nbsp; } {&nbsp; &nbsp; &nbsp; &nbsp; card1, err := GetCardFromStr(data.s1)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("case:%d errMsg:%s\n", caseNumber, err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; card2, err := GetCardFromStr(data.s2)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("case:%d errMsg:%s\n", caseNumber, err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("criteria %d, %s equal %s: %v\n",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.criteria, card1, card2, card1.Equal(card2, data.criteria),&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; }}go playground通过使用上面的代码,我相信没有人会对比较感到困惑,并且您将能够避免被否决。然后你可以把问题转向如何优化函数只GetCardFromStr&nbsp;使用循环一层、正则表达式、检测更多的边缘......严格来说,我不是在回答问题,只是为你的问题方向提供参考。我希望你不介意,祝你好运。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go