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

时间:2019-05-14 14:27:00

标签: python python-3.x string function conditional

  

编写一个名为singleline_diff的函数,该函数需要两个单行字符串。您可以假设两个字符串始终都是一行,并且不包含任何换行符。该函数应返回两行之间不同的第一个字符的索引。如果两行相同,则函数应返回常数IDENTICAL,该常数已经定义为-1。

     

如果各行的长度不同,但是整个较短的行与较长的行的开头匹配,则第一个差异位于索引处,该索引比较短的行中的最后一个字符晚一个。换句话说,没有将短行末尾的字符定义为与该位置的长行中存在的任何字符都不同。

     

提示:   1)您无需检查两个输入是否为单行。您可能会认为它们是。

     

2)您应该首先检查两个输入的长度,然后确定较短线的长度。

     

3)寻找到短行中最后一个字符为止的行中的差异。

     

4)如果没有发现任何差异,请考虑在两种可能的情况下应该采取的措施:(1)线长相同,(2)线长不同。

我已经按照指令中的说明编写了函数,并且我使用了一系列条件比较字符串的长度。一旦确定了字符串的长度,我便将索引变量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

1 个答案:

答案 0 :(得分:2)

当您的for循环从没发现不相等的字符时(例如,其中一个字符串是另一个字符串的起始子字符串),循环就结束了,所以它不会命中{{1 }}语句,因此它将返回return

因为Noneelse: 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
相关问题