FAQ: Code Challenge: String Methods - Count Multi X

5 posts were split to a new topic: Could I just use .count()?

That’s what I came up with too!

def count_multi_char_x(word, x):
  count = word.split(x)
  wordlen= ''.join(count)
   
  return (len(word)-len(wordlen ))//len(x)

can anyone validate this direction of logic? Is it on the right path or have I made a simple task more complex that it needs to be? Is there a more elegant way of doing it?

4 posts were split to a new topic: Can I iterate through the word? [solved]

7 posts were split to a new topic: Iterating through word search with steps? [solved]

2 posts were split to a new topic: Can I solve this with a built in method?

3 posts were split to a new topic: Can you review my code?

Creative!

What you do is actually removing the search string in the text (split by x then join with empty string). You could acheive same thing just by simple replace:

def count_multi_char_x(word, x):
  replaced = word.replace(x, '')
  return int((len(word) - len(replaced)) / len(x))

( i added int([...]) at return to fix float result format type

According to the solution code, when executing the first print comment(mississipi), how come the splits list get the elements of “m”, " ", “ipi”?
I thought it will get “m” and “ipi” only.

[spoiler]This text will be blurred[/spoiler]
# Write your count_multi_char_x function here:
def count_multi_char_x(word, x):
  splits = word.split(x)
  return(len(splits)-1)

# Uncomment these function calls to test your  function:
print(count_multi_char_x("mississippi", "iss"))
# should print 2
print(count_multi_char_x("apple", "pp"))
# should print 1[spoiler]This text will be blurred[/spoiler]

Can someone explain exactly what this is saying?
split = word.split(x)
return(len(split)-1)

if you know what each thing there does then you can carry it out with pen and paper and observe what you do

or if not, then find out what that individual thing does before considering all of it at once. it’s not some impenetrable blob, there are individual parts that you need to learn before you can understand the overall thing

and, if there are individual parts in it that you can learn about, then maybe doing so lets you understand the overall thing and therefore don’t need to ask for help with it quite yet

I understand what each thing there does, but I’m confused on why we’re splitting at x. I also don’t know why we’re returning the length of the split and then putting the -1. I just started learning python and some of these things aren’t making sense, sorry for sounding a little stupid.

1 Like

how does this solution compare? (I’m impressed by the .join() one)

def count_multi_char_x(word, x):
n = range(len(word) - len(x) +1)
k =[ ]
for i in n:
if word[i] == x[0]:
k.append(word[i:i+len(x)])
return k.count(x)

I’ve basically isolated all stringlets of length = len(x) starting with x[0] and then counted the number of stringlets identical to 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