Question

Given two strings of equal length, how do I return the number of times that the strings have the same character at a given index?

So: count_matches("bob","boa") would give 2 as index 0 holds the same character and so does index 1.

But, count_matches('bob', 'bbo') would only return 1 as the only index where the character is the same in both is index 0. Although there are two 'b's in both, only one of those is at a corresponding index.

Was it helpful?

Solution

I assume you mean the number of indices where the character is the same in both strings.

Therefore, I would do:

>>> sum(a==b for a, b in zip('bob', 'boa'))
2

Wrapping that in a function should be trivial.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top