【问题标题】:Algorithm on finding all 4 letter words in a string - where's the last item?在字符串中查找所有 4 个字母单词的算法 - 最后一项在哪里?
【发布时间】:2020-01-30 12:31:40
【问题描述】:

在这里发布一个问题 ~find all ~4 个字母的单词在一个字符串 ~algorithm site:stackoverflow.com google 没有返回任何积极的结果。

问题: 编写一个函数,接收一串单词并返回 4 个字母的个数。输入参数 'sentence' 是一个字符串,单词由空格分隔,没有标点符号。

到目前为止,我有这样的代码:

func fourLetters(sentence: String) -> Int {
    var targetNames = 0
    var letterCount = 0

    for letter in sentence {

    if letter != " " {
      letterCount += 1
      } else {
        print ("space found") // tech line
        print (letterCount) // tech line

        if letterCount == 4 {
          targetNames += 1
          letterCount = 0
          }
        letterCount = 0
      }
    }

    print(targetNames) // tech line
    return targetNames
}

问题: 该算法现在不考虑字符串的最后一部分给出无效的 4 个字母单词数。考虑我们有句子:“Good Night Lil Peep”将返回 1,尽管显然有两个 4 出纳词。我错过了什么?似乎循环完全忽略了最后一个词。

repl.it 链接方便运行:https://repl.it/@DmitryAksyonov/4-lettered-names

感谢您的帮助! 问候

【问题讨论】:

  • @KeshuR.: 不,代码审查是为了审查(和改进)工作正常据作者所知的代码。”
  • 单行:return sentence.split(separator: " ").filter { $0.count == 4 }.count
  • @MartinR 我正要评论sentence.split{$0.isWhitespace}.filter{$0.count == 4}.count"
  • @LeoDabus:我们还在等待count(where:) ... :)
  • @LeoDabus:在 Swift 5.2 中你可以做到 split(whereSeparator: \.isWhitespace) :)

标签: swift algorithm loops


【解决方案1】:
func findWords(ofLenght lenght: Int, in string: String) -> [Substring] {
    let words = string.split{ $0.isWhitespace }
    return words.filter { $0.count == lenght }
}

let input = "abcd word some string"
let result = findWords(ofLenght: 4, in: input)
print(result.count)

输出:3

【讨论】:

  • 最好使用Character属性isWhitespace
  • 应该打印 3
  • 无需为每个单词初始化一个新字符串。您可以返回一个子字符串数组
  • 在此基础上,我会将方法设为泛型,将泛型类型限制为 StringProtocol 并返回一个数组 os SubSequence 但在这种情况下,OP 只需要结果数组的 count 属性跨度>
【解决方案2】:

问题是targetNames 的增量代码只有在输入字符串中找到space 时才会遇到,而字符串末尾的情况并非如此。如果字符串的最后一个字符是space,您可能会很幸运。

因此,即使您的句子仅包含一个 4 字母单词,您的代码也会失败。

一个可能的解决方案是您修改您的第一个 if 语句的条件,方法是向它添加另一个条件,如果循环已到达字符串末尾,则返回 false,以便您的代码的 else 部分将在那个时候运行并检查最后一个单词是否是 4 个字母的单词。

【讨论】:

    【解决方案3】:

    您的算法不起作用,因为您没有检查文本末尾的 4 个字母单词。

    您必须添加另一个else 子句和一个索引来检查文本结尾

    当然有更有效的方法可以做到这一点,这只是为什么不考虑最后一项的问题的答案。

    func fourLetters(sentence: String) -> Int {
        var targetNames = 0
        var letterCount = 0
        var index = sentence.startIndex
    
        for character in sentence {
            sentence.formIndex(after: &index)
            if character == " " { // check space
                print ("space found") // tech line
                print (letterCount) // tech line
    
                if letterCount == 4 {
                    targetNames += 1
                }
                letterCount = 0
            } else if index == sentence.endIndex { // check end of text
                print ("end of text found") // tech line
                letterCount += 1 // increment letterCount
                if letterCount == 4 {
                    targetNames += 1
                }
            } else {
                letterCount += 1
            }
        }
        print(targetNames) // tech line
        return targetNames
    }
    
    fourLetters(sentence: "Good Night Lil Peep") // 2
    

    【讨论】:

    • 另一种选择是在循环之后对letterCount == 4 进行最终检查(使循环更短更简单)。
    猜你喜欢
    • 1970-01-01
    • 2019-05-12
    • 2018-10-03
    • 2019-05-12
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多