FAQ: Code Challenge: String Methods - Count Multi X


#47

can anyone tell me why this doesn’t work?

def count_multi_char_x(word,x):
count= 0
chunk = word.split(x)
if chunk == x:
count += 1
return count


#48

Why would it work? What exact actions do you mean that should take? If you print out each thing it does, is that what ends up happening? If not, then you’ve answered your own question by showing yourself where it differed from what you expected. Or, if you haven’t thought it through in full, then it definitely shouldn’t work because nobody decided what should happen.


#49

…very helpful, thanks.


#50
def count_multi_char_x(word, x):
  count = 0
  i = word.find(x)
  while (i>=0) and (word != ""):
    count+=1
    word = word[i+1:]
    i = word.find(x)
  return count

Hi ! What do you think about this answer fellow community members ?


#51

We can refactor your solution to simplify…

def count_multi_char_x(word, x):
    count = 0
    while word:
        i = word.find(x)
        if i < 0: break
        count += 1
        word = word[i+1:]
    return count

There is something not very naive about the solution since it uses a built-in with slicing, rather than plain iteration and testing of each position for a match. As learners we should have the basic approach under our belt before moving on to more sophisticated solution approaches.


#52

str.find stops when it finds a match meaning that it will in total visit each location only once (except for the first time you use it, that’s a bit inconsistent: you forgot to make the string smaller there)

There’s another problem though, the copies you make of the string does end up being more work than there should be. Rather than copying the string, you could tell str.find where it should start searching.


#53

splits = word.split(x)
return splits
print(count_multi_char_x(“mississipi”, “iss”))

[‘m’, ‘’, ‘ipi’]
can someone explain why theres “” i dont understand why it would do that?

Also why does:
“mississipi”.split(“i”)
give us [‘m’, ‘ss’, ‘ss’, ‘p’, ‘’]
" in the end. I guess my logic is not clear on this.


#54

str.split() will default to space characters as the separator string, and will preserve only the words in a list. When we supply a separator string (can be one or more characters), the method only preserves what is on either side of that separator. In the above we see that there are no i's preserved in the list, only what was on either side of them.

Occasionally, as in your first example, there may be two such separator strings next to each other, ississ and when we remove the second one it preserves an empty string on its left side, hence, the '', in the list. When the separator is at the end of the string, again it preseves an empty string on its left. This is not a flaw, but the nature of the method.


#55

how about this solution?

def count_multi_char_x(word, x):
splited = word.split(x)
joined = “”.join(splited)
return (len(word) - len(joined))/len(x)


#56

I see many people use split etc. but I thought of this solution:

def count_multi_char_x(word, x):
  return word.count(x)

would that be ok as well?


#57

It depends… Does the exercise suggest an algorithm of our own creation, or does it ask us to use the builtt-in method? Not much to be learned using the the built-in since it does all the heavy lifting and we do nothing.

Can you devise an algo that performs the same task but which exposes its logic to the reader? That would be better practice, one expects.