0

I have a problem for class where I need to compare strings ie. 'truck' vs 'trunk' and return the FIRST position in which the strings differ. I have managed to solve the initial part but I run into trouble when I encounter strings of different lengths ie. 'truck' vs 'trucks'. I've tried a few different methods before deleting and going back to square 1 so I'm hoping to get some advice here. Any help would be greatly appreciated. My current code is below.

IDENTICAL = -1

def singleline_diff(line1, line2):
    
    short = min(len(line1),len(line2))
    
    for i in range(0,short):
        
        if line1[i] == line2[i]:
            continue

        else:
            return i
  
    else:
        return IDENTICAL


word1 = 'truck'
word2 = 'trunk'
prob1 = singleline_diff(word1, word2)

print(prob1)
4
  • 1) You have calculated the lengths, so move them to their own variables and compare. 2) What are you supposed to return when they are different lengths? Commented Oct 13, 2020 at 19:29
  • Please specify what you mean by "position in which the strings differ" Does that mean the strings can only differ in 1 position? Or can they be completely different? What would be the expected out of singleline_diff("StackOverflow", "overflowstack")
    – Andreas
    Commented Oct 13, 2020 at 19:29
  • I added to my post specifying that the code is expected to find the FIRST difference in the strings. In your example the result would be 0 due to "S" being different from "o". Sorry for not being more specific.
    – Nick
    Commented Oct 13, 2020 at 19:34
  • @Nick ok perfect, then check my answer should work for you. Happy Coding!
    – Andreas
    Commented Oct 13, 2020 at 19:43

2 Answers 2

1

you can use zip + enumerate.

zip combines 2 iterables like lists or strings. If you loop through it you get pairs of one character of word1 in a tupel with the character on the same position in word2.

enumerate returns the index of the current loop. since return stops all activities you can use that to return the index (from enumerate) once the tupel w (which comes from zip() has a different character on position 0 and 1

def singleline_diff(word1, word2):
    
    word1 = word1.ljust(len(word2))
    word2 = word2.ljust(len(word1))
    
    for i,w in enumerate(zip(word1, word2)):
        if w[0] != w[1]:
            return i
        
    return "Identical"
    
word1 = 'truck'
word2 = 'trucks' 
print(singleline_diff(word1, word2))
6
  • Thanks for the response but this still doesn't solve the problem I have with 'truck' vs 'trucks' in which the returned value should be the index loc of 's'.
    – Nick
    Commented Oct 13, 2020 at 19:43
  • why? what is the problem? The function returns 3 which is the the 4th character of the strings.
    – Andreas
    Commented Oct 13, 2020 at 19:44
  • Try word1 = 'truck' and word2 = 'trucks'
    – Nick
    Commented Oct 13, 2020 at 19:46
  • @Nick try this then, just artificially lengthen the smaller word until it has the same length as the longer one.
    – Andreas
    Commented Oct 13, 2020 at 19:58
  • That seems to work, thanks! I was unfamiliar with ljust.
    – Nick
    Commented Oct 13, 2020 at 19:59
0

You can simply return IDENTICAL if line1==line2 evaluates to true. If there lengths are uneven and no differences are found, just return short.

def singleline_diff(line1, line2):
    if line1 == line2:
         return IDENTICAL
    short = min(len(line1),len(line2))
    for i in range(0,short):
        if line1[i] == line2[i]:
            continue
        else:
            return i
    return short
        
5
  • While this might answer the question, providing details as to how to reached the conclusion and your thought process will surely help the fellow asker. See How to answer?
    – Shivam Jha
    Commented Oct 13, 2020 at 19:30
  • How does returning the length of the strings help? Commented Oct 13, 2020 at 19:31
  • It is the first index unequal in both strings, according to the requirements of the question Commented Oct 13, 2020 at 19:32
  • you should return short + 1 because if both are equal upto the part of same length, the last index pos. +1 will be different
    – Shivam Jha
    Commented Oct 13, 2020 at 19:33
  • For a string of length 5, the first index out of range is actually 5 itself Commented Oct 13, 2020 at 19:35

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.