for i, c in zip(problem, solution):
if i != c:
# that's the first set of chars, but we're already returning??
return False
if i == c or "#" == c:
# wildcard works here, but already would have failed earlier,
# and still an early return if it IS true!
return True
相反,您需要遍历整个字符串并返回结果,或者使用all来完成。在
^{pr2}$
或者在一行中:
return len(problem) == len(solution) and \
all(p_ch==s_ch or p_ch=="#" for p_ch, s_ch in zip(problem, solution)
def checkSolution(problem, solution):
if len(problem) != len(solution):
return False
for i, c in zip(problem, solution):
if i != '#' and c != '#' and i != c :
return False
return True
if len(problem) == len(solution):
# in the line below,
# 'i' will contain the next char from problem
# 'c' will contain the next char from solution
for i, c in zip(problem, solution):
# in this line, if they're not equal, you return False
# before you have a chance to look for the wildcard character
if i != c:
return False
# ...and here you'd fail anyway, because you're testing
# the character from the solution string against the wildcard...
if i == c or "#" == c:
return True
# ...while in your test, you pass the wildcard in as part of the problem string.
print (checkSolution("a#c","abc"))
现在你只需要测试长度和第一个字符是否匹配。在
相反,您需要遍历整个字符串并返回结果,或者使用
^{pr2}$all
来完成。在或者在一行中:
或者,如果你真的很疯狂(读:你太喜欢正则表达式了),你可以做一些类似的事情:
您只检查第一个字符。如果第一个字符相同或是
True
,则不应返回True
,但应继续查找第一个不匹配项并仅在for循环外返回True
。在第二个问题是,在您的测试用例中,},因为}的字符,而{}是{}的字符。在
c
永远不是{i
是{正如评论中指出的,你的缩进是错误的,应该修正。在
相关问题 更多 >
编程相关推荐