Algorithm: - Go through each character in word1, one by one. If this character occurs in word2, then add 1 to the count. Return total count after you're done going through the characters.
>>>count_common_occurrences('bob y', 'bobbette z')
3
>>>count_common_occurrences('bobbette z', 'bob y')
4
Here is my code
def count_common_occurrences(word1, word2):
count = 0
for i in word1.strip():
if i in word2.strip():
count = count + 1
return count
the result I get is always one greater than that of the example, I was intially suspecting the function counted space, so I used strip but after that the result is still the same. I dont know whats causing the function to count one more than its supposed to