Count letter differences of two strings

后端 未结 11 700
既然无缘
既然无缘 2020-12-09 17:11

This is the behaviour I want:

a: IGADKYFHARGNYDAA
c: KGADKYFHARGNYEAA
2 difference(s).
11条回答
  •  佛祖请我去吃肉
    2020-12-09 17:30

    The Theory

    1. Iterate over both strings simultaneously and compare the characters.
    2. Store the result with a new string by adding either a spacebar or a | character to it, respectively. Also, increase a integer-value starting from zero for each different character.
    3. Output the result.

    Implementation

    You can use the built-in zip function or itertools.izip to simultaneously iterate over both strings, while the latter is a little more performant in case of huge input. If the strings are not of the same size, iteration will only happen for the shorter-part. If this is the case, you can fill up the rest with the no-match indicating character.

    import itertools
    
    def compare(string1, string2, no_match_c=' ', match_c='|'):
        if len(string2) < len(string1):
            string1, string2 = string2, string1
        result = ''
        n_diff = 0
        for c1, c2 in itertools.izip(string1, string2):
            if c1 == c2:
                result += match_c
            else:
                result += no_match_c
                n_diff += 1
        delta = len(string2) - len(string1)
        result += delta * no_match_c
        n_diff += delta
        return (result, n_diff)
    

    Example

    Here's a simple test, with slightly different options than from your example above. Note that I have used an underscore for indicating non-matching characters to better demonstrate how the resulting string is expanded to the size of the longer string.

    def main():
        string1 = 'IGADKYFHARGNYDAA AWOOH'
        string2 = 'KGADKYFHARGNYEAA  W'
        result, n_diff = compare(string1, string2, no_match_c='_')
    
        print "%d difference(s)." % n_diff  
        print string1
        print result
        print string2
    
    main()
    

    Output:

    niklas@saphire:~/Desktop$ python foo.py 
    6 difference(s).
    IGADKYFHARGNYDAA AWOOH
    _||||||||||||_|||_|___
    KGADKYFHARGNYEAA  W
    

提交回复
热议问题