2 回答

TA贡献1797条经验 获得超4个赞
您可以创建字符串的“无空格”版本和“无空格,无下划线”版本,然后比较每个字符以查看是否匹配非下划线字符或是否已使用与下划线对应的字符。例如:
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

TA贡献1934条经验 获得超2个赞
这条线在这里:
if len(match) == len(myWord_noUnderLine)
不会给你你现在想要的。在“a_ple”示例中,空格和“_”都被删除,因此您的 myWord_noUnderLine 变量将为“aple”,因此检查“aple”和“apple”之间的长度匹配肯定会失败
添加回答
举报