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

Need Help Figuring Out Why My String Comparison Isn't Passing All Test Cases

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)与 returns 一起用于 return 发生差异的索引或 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

我创建了六个测试用例(见下文)。按照我现在编写代码的方式,我的测试用例有一半是正确的。我很难尝试调试我的代码未能 return 预期值的地方。

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 循环永远找不到不相等的字符时(当其中一个字符串是另一个字符串的起始子字符串时就是这种情况),你的 for 循环就会结束,所以它不会遇到 return 语句,所以它会 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