FAQ: Code Challenge: String Methods - Count Multi X

If you need to count only non-overlapping strings, you’ll need to advance your iteration variable by the length of the target string each time you reach it.

You can’t do that with for, but you can with while.

1 Like

while will reduce the number of iterations, but as a brute force approach,

makes perfect sense, even while it does have to completely iterate over the word string.

>>> def count_multi_char_x(word, x):
  count = 0; c = 0
  for w in range(len(word)):
    c += 1
    if word[w : w + len(x)] == x:
      count += 1
  return count, c

>>> count_multi_char_x("mississippi", "issi")
(2, 11)
>>> 

The target does overlap (they share last/first i) though your solution still finds both impressions, which makes sense given the criteria of the problem. If we advance the iteration variable as @patrickd314 suggests, only one impression of ‘issi’ will be found in your word.

Nobody is right or wrong here. Criteria dictates must always be carefully considered, and never assumed.

We can tweak your solution slightly to eliminate unneeded iterations…

>>> def count_multi_char_x(word, x):
  count = 0; c = 0
  for w in range(len(word) - len(x)):
    c += 1
    if word[w : w + len(x)] == x:
      count += 1
  return count, c

>>> count_multi_char_x("mississippi", "issi")
(2, 7)
>>> 
2 Likes

Yes, I guess I misunderstood @web5761085078’s question:

I thought that “but” implied that 2 was an incorrect answer, and that non-overlapping instances were required. However, as it happens, the assignment is mute on whether or not overlaps are accepted.

1. Write a function named count_multi_char_x that takes a string named word and a string named x . This function should do the same thing as the count_char_x function you just wrote - it should return the number of times x appears in word . However, this time, make sure your function works when x is multiple characters long.

For example, count_multi_char_x("Mississippi", "iss") should return 2

… shoulda looked first.

2 Likes

I Have used the below solution :

def count_multi_char_x(word,x):
p = len(’’.join(word.split(x)))
l = len(word)
y = len(x)
return (l - p)/y

I thought I might approach it by iterating over the entire word by x length and look for match cases.

def count_multi_char_x(word, x):
    counter = 0
    stepper = len(x)
    #print(stepper)
    for index in range(len(word)):
        #print(index, word[index: stepper], stepper)
        if (word[index: stepper] == x):
            counter += 1
        stepper +=1
    return counter

The downside to stepping is we miss all the first letters between position one and position two. It makes sense to step past a found match, and continue from there, but until such match is found, the step should be one character over.

However, if we look back to the previous posts, we see that there are times when we would not want to jump, found match or not. The objectives need to be clear in order to choose a route to take.

Hi mtf, thank for the reply!

I am not sure I understand, because the step is one character over in word.

The function iterates over word by:
Starting at char 0 in the word and checking for matches from
Index 0 in word to len(x) in word then
Index 1 in word to len(x) in word then
Index 2 in word to len(x) in word

1 Like

For this exercise in order to get the expected result we must step one character at a time. It’s the only way we get 2 when counting issi in mississippi.

I thought that was what my function does?

1 Like

And so it does. I was letting the variable name (stepper) throw me off. My bad.