[英]Comparing similarity of two strings in Python
我想要以下内容:
Input: ("a#c","abc")
Output: True
Input:("a#c","abd")
Desired Output: False
Real Output: True
因此,如果两个字符串具有相同的长度,并且它们的区别仅在于代表随机字符的字符#不同,则该函数返回True。 如果没有,我希望它返回False。
我应该在此功能中进行哪些更改?
def checkSolution(problem, solution):
if len(problem) != len(solution):
return False
if len(problem) == len(solution):
for i, c in zip(problem, solution):
if i != c:
return False
if i == c or "#" == c:
return True
print (checkSolution("a#c","abc"))
print (checkSolution("a#c","abd"))
您只检查第一个字符。 你不应该回到True
的情况下,第一个字符是相同的或者是#
,但你应该去找到第一个不匹配,返回True
只能在外面的for循环。
第二个问题是,在您的测试用例中,变量c
永远不会是'#'
,因为i
是problem
的特征,而c
是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
现在,您只测试长度和第一个字符是否匹配。
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
结果为您完成。
if len(problem) == len(solution):
for p_ch, s_ch in zip(problem, solution):
if p_ch == "#": # wildcard
continue # so we skip testing this character
if p_ch != s_ch:
return False # This logic works now that we're skipping testing the "#"
else: # if we fall off the bottom here
return True # then it must be equal
else:
return False
或一行:
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):
return re.match("^" + ".".join(map(re.escape, problem.split("#"))) + "$",
solution)
正如评论中指出的那样,您的缩进已弄糟,应予以修复。
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"))
功能的一行版本:
def check_solution(problem, solution):
return (len(problem) == len(solution) and
all(ch==solution[i] for i, ch in enumerate(problem) if ch != '#'))
测试:
>>> check_solution("a#c", "abc")
True
>>> check_solution("a#c", "abd")
False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.