【问题标题】:String comparison and sorting function字符串比较排序功能
【发布时间】:2019-02-09 05:43:13
【问题描述】:

我正在设计一个猜字游戏,我需要一些关于其中一个功能的帮助。 该函数接收 2 个输入并返回 true 或 false。
输入 my_word 包含猜测并与某个单词匹配的字母。 输入 other_word 是一些要与 my_input 进行比较的词。 例子:

>>> match_with_gaps("te_ t", "tact")
False
>>> match_with_gaps("a_ _ le", "apple")
True
>>> match_with_gaps("_ pple", "apple")
True
>>> match_with_gaps("a_ ple", "apple")
False

我的问题是应用它来返回 False,就像上一个示例一样,我不知道该怎么做。这是我到目前为止所做的。它有效,但不适用于 my_word 中的一个猜测字母在 other_word 中出现 2 次的情况。在这种情况下,我返回 true,但它应该是 False。 输入必须与示例中的格式完全相同(下划线后的空格)。

def match_with_gaps(my_word, other_word):
    myWord = []
    otherWord = []
    myWord_noUnderLine = []
    for x in my_word:
        if x != " ": # remove spaces
            myWord.append(x)
    for x in myWord:
        if x != "_": # remove underscore
            myWord_noUnderLine.append(x)
    for y in other_word:
        otherWord.append(y)

    match = ( [i for i, j in zip(myWord, otherWord) if i == j] ) # zip together letter by letter to a set
    if len(match) == len(myWord_noUnderLine): # compare length with word with no underscore
        return True
    else:
        return False


my_word = "a_ ple"
other_word = "apple"

print(match_with_gaps(my_word, other_word))

【问题讨论】:

  • Python 3. 谢谢
  • 仅供参考:您可以轻松地进行链式替换,而不是使用 for 循环。 sw = my_word.replace(' ').replace('_')

标签: python python-3.x string list filtering


【解决方案1】:

您可以创建字符串的“无空格”版本和“无空格,无下划线”版本,然后比较每个字符以查看非下划线字符是否匹配或是否已使用与下划线对应的字符。例如:

def match_with_gaps(match, guess):
    nospace = match.replace(' ', '')
    used = nospace.replace('_', '')
    for a, b in zip(nospace, guess):
        if (a != '_' and a != b) or (a == '_' and b in used):
            return False
    return True

print(match_with_gaps("te_ t", "tact"))
# False
print(match_with_gaps("a_ _ le", "apple"))
# True
print(match_with_gaps("_ pple", "apple"))
# True
print(match_with_gaps("a_ ple", "apple"))
# False

【讨论】:

  • 谢谢,但这不是我需要的。我需要它在 match_with_gaps("a_ple", "apple") 中返回 False
  • @SHR - 你的评论被截断了。在上述哪种情况下,您需要它在不返回 False 的地方返回?
  • >>> match_with_gaps("te_t", "tact") 错误 >>> match_with_gaps("a_ _ le", "apple") 正确 >>> match_with_gaps("_ pple", " apple") True >>> match_with_gaps("a_ple", "apple") False
  • @SHR - 根据您对另一个答案的评论,我想我现在正在关注您。这就像刽子手,任何已经使用过的字母都不在桌面上。请参阅编辑后的答案。
【解决方案2】:

这里的这一行:

 if len(match) == len(myWord_noUnderLine)

现在不会给你你想要的。在“a_ple”示例中,空格和“_”都被删除了,因此您的 myWord_noUnderLine 变量将为“aple”,因此检查“aple”和“apple”之间的长度匹配肯定会失败

【讨论】:

  • 没有。 my_word = "a_ple" 和 other_word = "apple" 的输出返回 True,因为字母 'p' 在两者中(您可以运行代码并查看)。在罐子里我需要它来返回 Falsa。
  • 对,我的错,我认为它正在检查其他我需要更好地理解这一点的东西。您能否详细说明为什么要进行第二次比较:match_with_gaps("a_ _ le", "apple") 返回 true,而第四次比较:match_with_gaps("a_ ple", "apple") 为 false。为什么 case 2 和 case 4 应该返回不同的输出?
  • 在 match_with_gaps("a_ple", "apple") 的情况下,我需要它返回 False,因为如果用户猜到了字母“p”,算法会在所有位置填充该字母这个单词。所以在“a_ple”的情况下,“apple”这个词不能是一个有效的选项,因为如果秘密词是“apple”,那么用户已经完成了游戏。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-03
  • 1970-01-01
  • 2022-12-05
  • 1970-01-01
  • 2013-06-06
  • 1970-01-01
  • 2021-01-01
相关资源
最近更新 更多