gpt4 book ai didi

python - 需要帮助弄清楚为什么我的字符串比较没有通过所有测试用例

转载 作者:行者123 更新时间:2023-12-01 00:57:28 24 4
gpt4 key购买 nike

Write a function called singleline_diff that takes two single line strings. You may assume that both strings are always a single line and do not contain any newline characters. The function should return the index of the first character that differs between the two lines. If the lines are the same, the function should return the constant IDENTICAL, which is already defined to be -1.

If the lines are different lengths, but the entire shorter line matches the beginning of the longer line, then the first difference is located at the index that is one past the last character in the shorter line. In other words, no character after the end of the shorter line is defined to be different than whatever character exists in the longer line at that location.

Hints: 1) You do not need to check whether or not the two inputs are a single line or not. You may assume that they are.

2) You should first check the lengths of the two inputs and determine the length of the shorter line.

3) Look for differences in the lines up to the last character in the shorter line.

4) If you do not find any differences, think about what you should do in the two possible cases: (1) the lines are the same length and (2) the lines are different lengths.

我已经按照说明编写了函数,并使用了一系列条件来比较字符串的长度。一旦确定了字符串的长度,我就会将索引变量 i 初始化为 0,将其与 for 循环一起使用来遍历字符串的字符以查找第一个差异。我使用条件(if、elif 和 else)以及 return 来返回发生差异的索引或返回 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

关于python - 需要帮助弄清楚为什么我的字符串比较没有通过所有测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56132778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com