简体   繁体   English

快速:比较2个字符数组,从每个字符数组中删除一个值

[英]swift: compare 2 arrays of characters, remove one value from each

Here's what I'm trying to do. 这就是我想要做的。 I'm making a game where opponents guess each other's word, and each guess has direct and indirect hits. 我正在做一个游戏,对手在猜对方的话,每次猜都有直接和间接的命中。 See asterisks for where I'm having an issue. 请参阅星号以了解我遇到的问题。

var directSum = 0
var indirectSum = 0
var words = ["canon", "cnams"]
var secretWord = Array(words[0].characters)
var guessWord = Array(words[1].characters)
var secretWordInd = [Character]()
var guessWordInd = [Character]()

let dir1 = secretWord[0] == guessWord[0]
let dir2 = secretWord[1] == guessWord[1]
let dir3 = secretWord[2] == guessWord[2]
let dir4 = secretWord[3] == guessWord[3]
let dir5 = secretWord[4] == guessWord[4]


if dir1 && dir2 && dir3 && dir4 && dir5 {
    print ("you won!")
}
    if dir1 {
        directSum += 1
    } else {
        secretWordInd.append(secretWord[0])
        guessWordInd.append(guessWord[0])
}
    if dir2 {
        directSum += 1
    } else {
        secretWordInd.append(secretWord[1])
        guessWordInd.append(guessWord[1])
}
    if dir3 {
        directSum += 1
    } else {
        secretWordInd.append(secretWord[2])
        guessWordInd.append(guessWord[2])
}
    if dir4 {
        directSum += 1
    } else {
        secretWordInd.append(secretWord[3])
        guessWordInd.append(guessWord[3])
}
    if dir5 {
        directSum += 1
    } else {
        secretWordInd.append(secretWord[4])
        guessWordInd.append(guessWord[4])
**}

for var secretLetter in secretWordInd {
    for var guessLetter in guessWordInd{
        if secretLetter == guessLetter {
            secretWordInd.remove(at:secretWordInd.indexOf(secretLetter))
            guessWordInd.remove(at:guessWordInd.indexOf(guessLetter))
            indirectSum += 1
            }
        }
    }**
var score = [directSum, indirectSum]

what I need to do is count every time there's a character in Array:SecretWordInd that matches a character in Array:guessWordInd, remove only those two characters(one from each string), and indirectSum += 1 for every time this occurs. 我需要做的是计数Array:SecretWordInd中的每个字符是否与Array:guessWordInd中的一个字符匹配,仅删除这两个字符(每个字符串中的一个),并且每次发生时都将indirectSum + = 1除去。 If there are 2 "a"s in one and 1 in the other, for instance, the function needs to remove 1 a from each. 例如,如果一个中有2个“ a”,另一个中有1个,则该功能需要从每个中删除1个a。 That means the output of the function for directSum, indirectSum in this case should be [1,2] since there is only one indirect hit from one "n" from the guess word. 这意味着在这种情况下,directSum,indirectSum的函数输出应为[1,2],因为猜测词中的一个“ n”仅存在一个间接命中。 This is what is making the function complicated for me. 就是使功能变得复杂的原因。 The number of values in both arrays will not be constant. 两个数组中值的数量将不是恒定的。 I can't figure out how to use a method to do this(.contains is for only strings I think). 我不知道如何使用一种方法来做到这一点(.contains仅适用于我认为的字符串)。 Your help is greatly appreciated! 非常感谢您的帮助! Thanks! 谢谢!

As others have said this isn't the place for homework ;) 正如其他人所说的,这不是家庭作业的地方;)

But here are some pointers as to what might be useful: 但是这里有一些关于可能有用的指针:

indexOf : returns index of element you are looking for, nil if not in set indexOf:返回要查找的元素的索引,如果未设置则返回nil

remove : remove from an array remove:从数组中删除

You should look into Array, Set and String data types in the documentation, they provide lots of functions to perform that kind of calculations and transformations. 您应该查看文档中的Array,Set和String数据类型,它们提供了许多功能来执行这种计算和转换。

For example: 例如:

var secretWord = "canon"
var guessWord  = "cnams"

// identify shared letters using sets
let sharedLetters  = Set(secretWord.characters).intersection(Set(guessWord.characters))

// count number of shared letters present in both words
let sharedCounts   = sharedLetters.map{  c      in (c, secretWord.characters, guessWord.characters) }
                                  .map{ (c,s,g) in (c, s.filter{$0==c}, g.filter{$0==c}) } 
                                  .map{ (c,s,g) in (c, min(s.count,g.count)) }

// count same letters at same position and shared
// (if you're making a mastermind game, you should subtract directCount from indirectCount)                                 
let directCount   = zip(secretWord.characters,guessWord.characters).filter{$0==$1}.count
let indirectCount = sharedCounts.map{$0.1}.reduce(0,+)                                 


// remove shared letters (for count) from both strings
sharedCounts.flatMap{ Array(repeating:$0, count:$1) }
            .forEach{ secretWord.remove(at:secretWord.characters.index(of:$0)!) }
sharedCounts.flatMap{ Array(repeating:$0, count:$1) }
            .forEach{ guessWord.remove(at:guessWord.characters.index(of:$0)!) }


sharedLetters  // {"n", "a", "c"}
sharedCounts   // ("n", 1), ("a",1), ("c", 1)]
directCount    // 1
indirectCount  // 3    
secretWord     // "on"
guessWord      // "ms"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Swift iOS 中比较两个数组并从一个数组中删除匹配的元素 - How to compare two arrays and remove matching elements from one array in Swift iOS 比较两个 Arrays 并删除重复项 Swift - Compare Two Arrays and Remove Duplicates Swift 如何比较两个数组,并从一个数组中删除两个数组中不匹配的多个项目? - How do I compare two arrays and remove multiple items from one array that do not match in both arrays? 比较两个数组的值 - Compare value from two arrays 比较两个对象数组并删除第二个对象数组中具有相同属性值的项目 - Compare two arrays of objects and remove items in the second one that have the same property value 使用每个数组中的一个值转换和连接多个数组 - Transform and concatenate multiple arrays using one value from each js/array :比较 3 个数组并“传输”一个值 - js/array : compare 3 arrays and "transfert" one value 如何比较两个数组并从一个数组中删除匹配元素以进行下一个循环? - How to compare two arrays and remove matching elements from one for the next loop? 如何将包含用户详细信息的两个数组作为数组进行比较,并从一个数组中删除匹配的元素? - How to compare two arrays that contain user details as array and remove the matching elements from one array? 比较网址数组并从一个数组中删除网址取决于第二个数组 - Compare arrays of urls and remove from one array urls depend on second array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM