-3

I need to compare two strings that are almost the same. Then find the point at which they differ using python. Any help?

for example two strings a and b

A = 'oooooSooooooooooooooooooRoMooooooAooooooooooooooo'
B = 'oooooSooooooooooooooooooooMooooooAooooooooooooooo'

Thanks

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Rific Air
  • 19
  • 1
  • 1
  • 1
  • 6
    "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results." – zero323 Sep 19 '13 at 07:19
  • 4
    Welcome to Stack Overflow! It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive. – Martijn Pieters Sep 19 '13 at 07:19
  • By `point they differ`, I'm guessing you are referring to index? – smac89 Sep 19 '13 at 07:22
  • possible duplicate of [Python: Determine prefix from a set of (similar) strings](http://stackoverflow.com/questions/6718196/python-determine-prefix-from-a-set-of-similar-strings) – lc2817 Sep 19 '13 at 07:35

4 Answers4

6

I'd suggest using the difflib which is shipped with every standard python installation. There you'll find the handy function ndiff.

>>> import difflib

>>> print "\n".join(difflib.ndiff([A], [B])),
- oooooSooooooooooooooooooRoMooooooAooooooooooooooo
?                         ^

+ oooooSooooooooooooooooooooMooooooAooooooooooooooo
?                         ^
>>> 
Constantinius
  • 34,183
  • 8
  • 77
  • 85
2

For same sized strings or if only shortest length matters:

def diffindex(string1, string2):
    for i, (char1, char2) in enumerate(zip(string1, string2)):
        if char1 != char2:
            return i
    return -1

For different sized strings:

from itertools import zip_longest

Now replace the corresponding line with this one:

for i, (char1, char2) in enumerate(zip_longest(string1, string2)):
SzieberthAdam
  • 3,999
  • 2
  • 23
  • 31
1

Some hints.

String have length:

print(len(A))

You can access individual letters by index:

print(A[0])

There is range function which allows you to generate sequence of integers:

for i in range(10):
    print(i)

You can check if two characters are equal:

 'a' == 'a'
 'a' == 'b'
zero323
  • 322,348
  • 103
  • 959
  • 935
0

1.split the string to letters to arrays A[] and B[]

2.compare each letter with the same array index inside a loop.

3.repeat the loop 0- (length of the string) with a count inside the loop.

4.Take the count for the compare condition became false (when A[] == B[] became False)

Sobin Augustine
  • 3,639
  • 2
  • 25
  • 43