首页 > 解决方案 > 需要帮助找出为什么我的字符串比较没有通过所有测试用例

问题描述

编写一个名为 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

标签: pythonpython-3.xstringfunctionconditional

解决方案


当您的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

推荐阅读