How do you find the difference between two letters in python?

I like the answer from Niklas R, but it has an issue (depending on your expectations). Using the answer with the following two test cases:

print compare('berry','peach')
print compare('berry','cherry')

We may reasonable expect cherry to be more similar to berry than to peach. Yet the we get a lower diff between berry and peach, then berry and cherry:

(' |   ', 4)  # berry, peach
('   |  ', 5) # berry, cherry

This occurs when strings are more similar backwards, than forwards. To extend the answer from answer from Niklas R, we can add a helper function which returns the minimum diff between the normal (forwards) diff and a diff of the reversed strings:

def fuzzy_compare(string1, string2):
    (fwd_result, fwd_diff) = compare(string1, string2)
    (rev_result, rev_diff) = compare(string1[::-1], string2[::-1])
    diff = min(fwd_diff, rev_diff)
    return diff

Use the following test cases again:

print fuzzy_compare('berry','peach')
print fuzzy_compare('berry','cherry')

...and we get

4 # berry, peach
2 # berry, cherry

As I said, this really just extends, rather than modifies the answer from Niklas R.

If you're just looking for a simple diff function (taking into consideration the aforementioned gotcha), the following will do:

def diff(a, b):
    delta = do_diff(a, b)
    delta_rev = do_diff(a[::-1], b[::-1])
    return min(delta, delta_rev)

def do_diff(a,b):
    delta = 0
    i = 0
    while i < len(a) and i < len(b):
        delta += a[i] != b[i]
        i += 1
    delta += len(a[i:]) + len(b[i:])
    return delta

Test cases:

print diff('berry','peach')
print diff('berry','cherry')

One final consideration is of the diff function itself when handling words of different lengths. There are two options:

  1. Consider the difference between lengths as difference characters.
  2. Ignore the difference in length and compare only shortest word.

For example:

  • apple and apples have a difference of 1 when considering all characters.
  • apple and apples have a difference of 0 when considering only the shortest word

When considering only the shortest word we can use:

def do_diff_shortest(a,b):
    delta, i = 0, 0
    if len(a) > len(b):
        a, b = b, a
    for i in range(len(a)):
        delta += a[i] != b[i]
    return delta

...the number of iterations is dictated by the shortest word, everything else is ignored. Or we can take into consideration different lengths:

def do_diff_both(a, b):
    delta, i = 0, 0
    while i < len(a) and i < len(b):
        delta += a[i] != b[i]
        i += 1
    delta += len(a[i:]) + len(b[i:])
    return delta

In this example, any remaining characters are counted and added to the diff value. To test both functions

print do_diff_shortest('apple','apples')
print do_diff_both('apple','apples')

Will output:

0 # Ignore extra characters belonging to longest word.
1 # Consider extra characters.

In this tutorial, we will write a Python program to find the difference between the two given strings. This problem can be asked in the interview. Let's understand the problem statement and then we will approach to the solution.

Problem Statement -

There are two strings given s and t. String t is generated by random shuffling string s and then added one more character at any random position. We need to write a Python program that returns the letter added to t.

Example -

Example -

Constraints:

The following constraints should be followed -

  • 0 <= s.length <= 1000
  • t.length == s.length + 1
  • s and t consist of lowercase English letters.

Python Program

Let's understand the following Python program.

Example -

Output:

Explanation -

In the above code, we defined the findThedifference() function that takes two strings as arguments. We used the list comprehension to convert the strings into list. Now, we iterate ls_s list, pick single element and remove that element to the second list ls_t. If all element removed from the second element, it means both given strings are same, otherwise return the first element of the second list.

Solution - 2

Let's see another solution of the problem.

Output:

Explanation -

In this tutorial, we used the sorted() method, which converts the string into a list of characters in a sorted manner. We created the two lists of strings and added an extra element as 0 to make the length equal; else, we will get the list index out of bounds. Now we iterated the t_list and checked if the s_list element is not equal to t_list; if the condition is matched, it returns that element.


How do I find the difference between two characters in Python?

How to get the difference between two strings in Python.
string1 = "abc".
string2 = "cdef".
first_set = set(string1).
second_set = set(string2).
difference = first_set. symmetric_difference(second_set).
print(difference).

How do you compare letters in Python?

You can use ( > , < , <= , <= , == , != ) to compare two strings. Python compares string lexicographically i.e using ASCII value of the characters.

How do you compare two letters in a string in Python?

Python String comparison can be performed using equality (==) and comparison (<, >, != , <=, >=) operators. There are no special methods to compare two strings.

How do you compare two letters in a string?

strcmp is used to compare two different C strings. When the strings passed to strcmp contains exactly same characters in every index and have exactly same length, it returns 0. For example, i will be 0 in the following code: char str1[] = "Look Here"; char str2[] = "Look Here"; int i = strcmp(str1, str2);