python - 需要帮助找出为什么我的字符串比较没有通过所有测试用例
问题描述
编写一个名为 singleline_diff 的函数,它接受两个单行字符串。您可以假设两个字符串总是单行并且不包含任何换行符。该函数应返回两行之间不同的第一个字符的索引。如果行相同,则函数应返回常量 IDENTICAL,该常量已定义为 -1。
如果行的长度不同,但整个较短的行与较长行的开头匹配,则第一个差异位于较短行中最后一个字符之后的索引处。换句话说,较短行末尾之后的任何字符都没有被定义为与该位置的较长行中存在的任何字符不同。
提示:1)您不需要检查两个输入是否为单行。你可以假设他们是。
2)您应该首先检查两个输入的长度并确定较短线的长度。
3) 在较短的一行中查找直到最后一个字符的行之间的差异。
4)如果你没有发现任何差异,考虑在两种可能的情况下你应该做什么:(1)线的长度相同,(2)线的长度不同。
我已经按照说明编写了函数,并使用了一系列条件来比较字符串的长度。一旦确定了字符串的长度,我就将一个索引变量 i 初始化为 0,我将其与 for 循环一起使用来遍历字符串的字符以查找第一个差异。我使用条件(if、elif 和 else)以及返回来返回发生差异的索引或返回 IDENTICAL(等于 -1)。
IDENTICAL = -1
def singleline_diff(line1, line2):
"""
Inputs:
line1 - first single line string
line2 - second single line string
Output:
Returns the index where the first difference between
line1 and line2 occurs.
Returns IDENTICAL if the two lines are the same.
"""
if len(line1) > len(line2):
i = 0
for i in range(len(line2)):
if line1[i] == line2[i]:
i += 1
elif line1[i] != line2[i]:
return i
else:
return i+1
elif len(line1) < len(line2):
i = 0
for i in range(len(line1)):
if line1[i] == line2[i]:
i += 1
elif line1[i] != line2[i]:
return i
else:
return i+1
else: #Condition where the lengths of the strings are equal
i = 0
for i in range(len(line1)):
if line1[i] == line2[i]:
i += 1
elif line1[i] != line2[i]:
return i
else:
return IDENTICAL
我创建了六个测试用例(见下文)。我现在编写代码的方式,我的测试用例有一半是正确的。我很难调试我的代码无法返回预期值的地方。
print(singleline_diff("abcd", "abcd")) #Should return -1, instead get None
print(singleline_diff("abcd", "abdf")) #Should return 2 (Works)
print(singleline_diff("1234566", "1234567")) #Should return 6 (works)
print(singleline_diff("123", "1234")) #Should return 3, instead get None
print(singleline_diff("4321", "321")) #Should return 0 (works)
print(singleline_diff("lollejrlke", "lollejrlkefa")) #Should return 10, instead get None
解决方案
当您的for
循环从未找到不相等的字符时(当其中一个字符串是另一个字符串的起始子字符串时就是这种情况),您的循环将运行结束,因此它不会命中return
语句,因此它将返回None
.
您的else: return IDENTICAL
子句没有被击中,因为if line1[i] == line2[i]:
它elif line1[i] != line2[i]:
涵盖了该索引的所有可能情况。
此外,手动递增i
是多余的,因为您正在迭代一个范围,该范围已经规定了在每次迭代时将给出哪个数字。
考虑这样的事情:
def singleline_diff(line1, line2):
"""
Inputs:
line1 - first single line string
line2 - second single line string
Output:
Returns the index where the first difference between
line1 and line2 occurs.
Returns IDENTICAL if the two lines are the same.
"""
if len(line1) > len(line2):
for i in range(len(line2)):
if line1[i] != line2[i]:
return i
# We've checked all the characters in the range and found no differences
# but we know line1 is longer, so this is the position at which they differ
return len(line2)
elif len(line1) < len(line2):
for i in range(len(line1)):
if line1[i] != line2[i]:
return i
return len(line1)
else:
for i in range(len(line1)):
if line1[i] != line2[i]:
return i
# They're the same length, and we've found no differences,
# therefore the strings are identical
return IDENTICAL
您可以进一步简化这一点,并且只编写for
一次循环。
def singleline_diff(line1, line2):
end = min(len(line1), len(line2))
for i in range(end):
if line1[i] != line2[i]:
return i
if len(line1) != len(line2):
return end
return IDENTICAL
推荐阅读
- c# - 使用 lock、Monitor Pulse 和 Wait 同步线程
- vue.js - 如何在 Vue.js 中动态切换没有值的 HTML 属性?
- python - 使用 EfficientNetB0 - 错误 - AttributeError:模块 'keras.utils' 没有属性 'get_file'
- java - 将比较器与 Treemap 一起使用会导致键重复
- node.js - 如何从 POSTMAN 发送 CSV 文件并在 nodejs 的后端读取该文件
- c# - c# Blazored.Typehead 方法的类型参数不能从用法中推断出来
- amazon-web-services - 用于跟踪 EMR 步骤执行成本的 AWS 标记
- python - OpenCV的Python tkinter GIL崩溃问题
- pyspark - 对透视列进行排名
- amazon-web-services - 如何防止机器人在我的 AWS Lambda 函数中产生成本