FAQ: Code Challenge: String Methods - Count Multi X

Idk is this cheating?

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

2 Likes

Did you cheat yourself out of learning as many approaches as you can discover? Is that the solution that is suggested? Are we instructed to use a particular approach?

Idk are we?

1 Like

You could replace that with this:

count_multi_char_x = str.count

You’re not changing it in anyway, you can use it directly

I was wondering the same thing because (-1) would mean you would have to start at the end of the word. Why would that even matter? Also, the length of the word won’t matter unless your checking for a number instead of a clause.

I wanna share my solution, a bit odd I guess.


def count_multi_char_x(word,x):
  return int((len(word)-len("".join(word.split(x))))/len(x))

This is basically len(word) - len(word minus x) / len(x)

Hello everyone! I wanna share my solution. Initially I got the solution by splitting word with x parameter and returning (length of the split list - 1).

But I was wondering whether there is any method of getting the solution by comparing each letter of x in word sequentially. After trying for some time, came up with the following:

It gives the solution but I’m not too sure whether it is totally correct. Any suggestions on the function ?

for each location in the big string:
  for each location from here to where the small string would end:
    compare
  did all comparisons check out? increment count

what you describe is only two loops, not … however many that is

if the first letter isn’t in the word then you’ll get zero matches, that’s not a special case

the first character isn’t special either, you would compare that the same way as the others

for each location in the big string:
  for each location from here to where the small string would end:
    compare
  did all comparisons check out? increment count

Yes… this is better…

I was thinking of comparing the first character of the small string first…

wherever the first letter was found, then I was comparing the remaining letters of small string in subsequent locations in the big string…

I thought I’d share my solution because I haven’t seen similar ones in this thread.

def count_multi_char_x(word,x):
    counter = 0
    index = 0
    while word.find(x,index) != -1:
        finder = word.find(x,index)
        index = finder + len(x)
        counter += 1
    return counter

print(count_multi_char_x("mississippi", "iss"))
# should print 2
print(count_multi_char_x("apple", "pp"))
# should print 1

hi, i came up with that:

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

i’m not sur it is what i was asked to do but it did work very well.

1 Like

That is much simpler indeed! I’m not sure we learned about count() back in that lecture though. Now that I’ve finished the python course, all these past challenges seem so easy now.

1 Like

So the part where it states:

return(len(split)-1)

It is not actually stating to start at the end of the word, it is saying take the length the list split and subtract by 1

If you add a print statement in the function to show the list, you can start to understand

def count_multi_char_x(word, x):
splits = word.split(x)
print(splits)
return(len(splits)-1)

if we call the following:
print(count_multi_char_x(‘this is mississippi’, ‘is’))

the output is:
[‘th’, ’ ‘, ’ m’, ‘s’, ‘sippi’]
4

since you are splitting the string by the x parameter return(len(split) - 1) counts the spaces in between the split list, which is why you need to subtract 1 from the length of the split list.

Hopefully that is clear?

This is my solution without looking at hint:

def count_multi_char_x(word, x):
  cnt=0
  LenX=len(x)
  for i in range(len(word)):
    if word[i:i+LenX]==x:
        cnt+=1
  return cnt

But I can see more efficient solutions posted by others.

1 Like

I really wish an explanation as to why the below code works would be provided.
def count_multi_char_x(word, x):
splits = word.split(x)
return(len(splits)-1)

I understand that split() splits a string at the point of the argument passed.
I also know len() returns the length of a string

When split does what it does, it removes the arguments from the string it was called on.

the return value of split(“iss”) called on the string “mississippi” is [‘m’, ‘’, ‘ippi’]
so how does len([‘m’, ‘’, ‘ippi’]-1) translate to the number of times “iss” appeared in the string?

So how is that correct? I feel like something is going on under the hood that i do not understand, can someone please explain? Thanks

1 Like

Consider where the iss was removed from the string.

['m', '', 'ippi']
     ^   ^
    /   /

The empty string tells us where the split between the two removed strings is. The length of the list is 3. Two instances were removed. That’s 3 minus 1.

1 Like

Thank you, it makes sense now!

1 Like

I’ve also made this ques this way only. But what was your doubt…this line is very obvious i guess that len([‘m’, ‘’, ‘ippi’])-1 would give 2. Hope this way of doing solution is correct. @mtf @ionatan.

I didn’t get it can you please illustrate with example…thank you.

>>> x = 'xsxsxsxsxsxsxsxsxsxs'
>>> len(x)
20
>>>  y = x.split('xs')
>>> y
['', '', '', '', '', '', '', '', '', '', '']
>>> len(y)
11
>>> 

The empty quotes indicate the splits are consecutive. It makes sense since this list can only be restored to the original string if we have something to go between the join string (and before or after when the split is at beginning and/or end).

>>> z = 'xs'.join(y)
>>> z
'xsxsxsxsxsxsxsxsxsxs'
>>>