FAQ: Code Challenge: String Methods - Count Multi X


#1

This community-built FAQ covers the “Count Multi X” exercise from the lesson “Code Challenge: String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise Count Multi X

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

Why output is like this…
[‘m’, ‘’, ‘ppi’]
[‘a’, ‘le’]

and not like this…
[‘m’, ‘’, ‘’, ‘ppi’]
[‘a’, ‘’, ‘le’]

???


#9

Im curious about the ’ return(len(splits)-1) ’ here. What is its purpose?

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

why not:
def count_multi_char_x(word, x)::
split_count = 0
splits = word.split(x)
if splits == x:
split_count += 1
return split_count

Why cant we just provide a variable to match to the argument x?


#10

I guess it’s because 3 lines of the code is more readable than 6.
and because you won’t have different number every time: just like analogy in number of cuts needed compared to parts of paper… there’s no need to assign a variable that will always give you -1 result.


#11

Consider,

>>> len('mississippi'.split('iss'))
3
>>> 'mississippi'.split('iss')
['m', '', 'ippi']
>>> len('mississippi'.split('i'))
5

The only thing that we can conclude is that it fudges the data to match the expectation given. Forcing data this way is counterproductive. As we can see, there is no logic in splitting the word on the characters we are attempting to identify then return a uniques count.

What that code would be more useful for is removing characters from the string…

>>> def remove_x(word, x):
	return ''.join(word.split(x))

>>> remove_x('mississippi', 'i')
'msssspp'
>>> 

Bottom line, neither example is correct. Strings are iterable and don’t need to be split to solve this problem. We will need to iterate one of the input strings; the only question is which one?


#12

Hello,

# first example
word = "mississippi"
word = word.split("iss")

print(word) <<< prints ["m", " ", "ippi"], len(word) = 3

occurences = len(word) - 1      #which is 2

# second example
word = "mapapapdrink"
word = word.split("ap")

print(word) <<< prints ["m", "", "", "ippi"], len(word) = 4

occurences = len(word) - 1     #which is 3

This page: Splitting Strings II explains a bit on empty string ("") as a by-product when I split a string on a character that it also ends with, I’ll end up with an empty string at the end of the list.

But for the word “mapapapdrink”, the program returns 2 occurence of “” in the split string instead of 1.

Have I understood the chapter wrong? Can anyone help me understand this?


#13

Ending in the splitting substring isn’t a special case. Wherever it splits, you’ll get a string for each side whether there are characters there or not.


#14

I see, thank you so much for explaining!


#15

Hey, for this prompt, I used the code
def count_multi_char_x(word, x):
return str(word.count(x))

,and the message at the bottom of the code input center says " count_multi_char_x('mississippi', 'iss') returned 2, expected 2." When I used this on the second example, "print(count_multi_char_x(“apple”, “pp”))

should print 1" My code also returns a value of 1.

Is this an error with my code, or is this just a bug?

Thanks!


#16

Something representing a count should probably be an integer, not text


#17

This is my best guess.

I think that it might help to look at it like you’re making cuts in a piece of rope. Each split is a cut. So in the case of “mississippi”, “iss” makes two cuts. if you cut a piece of rope twice you will have three pieces. In the case of “apple” and “pp”, the rope will be cut once, so two pieces in this case.

How does Python generate an empty element in the case of “mississippi” and “iss”? You got me. but in makes sense in the rope analog.

Maybe someone with a better understanding of Python’s inner workings can shed some light on the subject.


#18

There is nothing between 'iss' and 'iss', which gets translated to ''. Everything between the separators is still a string.


#19

Do you know how python does this?


#20

If I was a C++ programmer I would, so no. I understand what is happening, is all.


#21

No such thing as an empty element, what’s empty is the string, a lack of elements, not an empty one

there’s nothing special about it, if anything empty is the simplest case

like you pointed out, you will end up with one more piece than the amount of splits you made. splitting zero times results in one piece (also using zero, but also not special, there isn’t a at-least-once requirement, nor is 1 the smallest natural number)

what would be special is -1, there you’d run into some real problems.


#22

Interesting. Do you know what is used as a place holder for an empty string in this case?


#23

But isn’t it? 0 is not a natural number, but undefined. On the number line it is both Integer and Real.


#24

That’s my point.
Nothing special needs to happen.


#25

If you put blueberries in a basket, what is the first state of the basket?
Of course it’s a natural number.

What amount does a ruler start counting at?

What speed do you have when you begin moving?

Starting at 1 is skipping the first case because it’s so trivial and simple, and yet incredibly important and existing everywhere


#26

An empty basked may have zero items in it, but that does not make zero a natural counting number. Natural numbers begin with 1.

Mind, it depends who you ask. One standard includes 0 and others do not. I’m stuck in the trenches of old-school thinking as taught in the sixties.

One supposes it depends on the basis of the number. For counting? or ordering? If cardinal, there is no zero; if ordinal there is a zero, as in zero-indexing.