FAQ: Code Challenge: String Methods - Count Multi X

2 posts were split to a new topic: Printing number as string instead of int [solved]

30 posts were split to a new topic: How does python create an empty list?

3 posts were split to a new topic: Why doesn’t my code work? [solved]

3 posts were split to a new topic: Using slicing instead of a naive approach [solved]

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)

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?


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)

# 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)

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]:
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


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