1
if s1 == s2:
    return 0


elif len(s1) == len(s2):
    mismatch = 0
    for i,j in zip(s1,s2):
        if i != j:
            mismatch +=1

    if mismatch == 1:
        return 1

elif len(s1) != len(s2):
    mismatch = 0
    for i,j in zip(s1,s2):
        if i != j:
            mismatch +=1

    if mismatch > 1:
        return 2

I am asked to write a code comparing two strings that will be received as parameters for my function. In iterating over them I have to return 0 if they both are the same, 1 if there is a mismatch in one character and 2 if the length of the two characters isn't the same or there is mismatch in more than 1 character.

When I try 'sin' and 'sink' as input strings, my code does not return anything? I have tried to vary my code many different ways but it doesn't help. It also returns nothing if there is a difference of one character like a space in the string and another character.

It seems like a simple enough piece of code and yet I cant seem to write it right. If I try to fix the issue with the space and another character by using .lower() it doesn't take in the 'k' at the end of the string. If I fix that with the .upper() it would not return anything. I fail to see what I'm doing wrong.

  • 2
    Possible duplicates.. There are a lot. Not sure which one to pick. http://stackoverflow.com/search?q=single_insert_or_delete+is%3Aquestion – Lafexlos Feb 25 '16 at 09:52
  • For the 'sin' / 'sink' issue check the answers to this question: [Python: zip-like function that pads to longest length?](http://stackoverflow.com/questions/1277278/python-zip-like-function-that-pads-to-longest-length) – alexs Feb 25 '16 at 10:08
  • all I can say is .... LOL – Tammy Davis Feb 27 '16 at 23:50

3 Answers3

0

I think it's normal that your code didn't return anything because in some cases there isn't any return value: if length are equal but mismatch != 1 and also when length are different but mismatch <= 1

Because in your example "sin" and "sink" you calculate the mismatch only on the "sin" part, so at the end mismatch is equal 0.

morony
  • 15
  • 4
0

This function might be what you are looking for

def match_string(s1,s2):
s1=s1.lower()
s2=s2.lower()
if s1 == s2:
    return 0
elif abs(len(s1)-len(s2))<2:
    mismatch=0
    for i in range(0,min(len(s1),len(s2))):
        if s1[i]!=s2[i]:
            mismatch+=1
    if mismatch<2:
        return 1
    else:
        return 2
else:
    return 2
0

This is what finally got it to work:

def find_mismatch(s1,s2): a=list(s1.lower()) b=list(s2.lower()) c=len(a) d=len(b)

match = 0

for char in b:
    if char in a:
        match += 1
        a.remove(char)

g=list(s1.lower())
h=list(s2.lower())
i=len(g)
j=len(h)           

match1 = 0
for char in g:
    if char in h:
        match1 += 1
        h.remove(char)

if match == c and match1 == j:
    return 0
elif (match == 0 or match1 == 0) and a != h:
    return 2
elif a != h and len(a) == len(h):
    return 1
elif a != h and len(a) != len(h):
    return 2