Ravi
Ravi

Reputation: 69

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 have written up the function as specified in the instructions, and I used a series of conditionals to compare the lengths of the strings. Once the lengths of the strings have been determined, I then initialize an indexing variable, i to 0, which I use with a for loop to go through the characters of the strings looking for the first difference. I use conditional (if, elif, and else) along with returns to return the index where the difference occurred or return IDENTICAL (which equals -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

I created six test cases (see below). The way my code is written right now, I get half of my test cases correct. I am stumped trying to debug where my code is failing to return the expected values.

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

Upvotes: 1

Views: 419

Answers (1)

khelwood
khelwood

Reputation: 59166

Your for loop runs off the end when it never finds an unequal character (which is the case when one of the strings is the starting substring of the other), so it doesn't hit a return statement, so it will return None.

Your else: return IDENTICAL clauses are not hit because if line1[i] == line2[i]: and elif line1[i] != line2[i]: cover all the possible cases for that index.

Also, manually incrementing i is redundant, because you are iterating through a range, which already prescribes which number will be given at each iteration.

Consider something like this:

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

You can further simplify this and only write the for loop once.

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

Upvotes: 2

Related Questions