擅长:python、mysql、java
<p>现在你只需要测试长度和第一个字符是否匹配。在</p>
<pre><code>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
</code></pre>
<p>相反,您需要遍历整个字符串并返回结果,或者使用<code>all</code>来完成。在</p>
^{pr2}$
<p>或者在一行中:</p>
<pre><code>return len(problem) == len(solution) and \
all(p_ch==s_ch or p_ch=="#" for p_ch, s_ch in zip(problem, solution)
</code></pre>
<p>或者,如果你真的很疯狂(读:你太喜欢正则表达式了),你可以做一些类似的事情:</p>
<pre><code>def checkSolution(problem, solution):
return re.match("^" + ".".join(map(re.escape, problem.split("#"))) + "$",
solution)
</code></pre>