-3

Suppose I have two strings of equal length:

s1 = 'tommy'

s2 = 'tammi'

How would I write a function that would return the index of the mismatch, like so:

s1 = 'tommy'

s2 = 'tammi'

mismatch = Get_Misalignment_Index(s1, s2)

print(mismatch)

[1, 4]

But could also handle missing or inserted charactors:

s3 = 'drain'

s4 = 'rains'

gaps = Get_Misalignment_Index(s3, s4)

print(gaps)

[0, 5]

Is this task doable in python?

jhurst5
  • 67
  • 1
  • 10

1 Answers1

0

You can do it easily using list comprehensions:

def find_dupes(s1, s2):
    return [x for x in range(len(s1)) if s1[i] != s2[i]] or None
Alec
  • 8,529
  • 8
  • 37
  • 63
  • This does not work for the second example, as it does not align the strings. It gives [0, 1, 2, 3, 4] – jhurst5 May 22 '19 at 21:39
  • I’m not sure what you’re going for with the second example. My function returns all the replaced indices. – Alec May 22 '19 at 21:42
  • The alignment of s3 to s4 is “drain-“, so index 0 and 5 are the location of inserted and deleted characters – jhurst5 May 23 '19 at 01:01
  • One function can’t detect both without choosing one arbitrarily because any shift could cause two strings not to match. It’s ambiguous whether characters where shifted or indices replaced – Alec May 23 '19 at 01:04
  • I believe the solution would involve making new strings, and then applying the function in your answer. If one inserted the aligned strings “drain-“ and “-rains” to your answer, it would return [0, 5]. – jhurst5 May 23 '19 at 01:08