# Assignment: Count-x

While I did manage to complete the assignment, my code only supports up to 2 repeats per word and it doesn’t truly feel like I’ve actually tackled the issue at hand. I know that .find only shows the location of first index of the located element. So I figured I’ll create a new word and remove the initial part that matched and yeah it works but it is a manual thing as far as I can tell. What’d have been a better method to tackle this issue?

My code:

``````# Write your count_multi_char_x function here:
def count_multi_char_x(word, phrase):
wc = 0
y=word.find(phrase)
if y > -1:
x=y+3
wc += 1
yeniword =word[x:]
z = yeniword.find(phrase)
if z > -1:
wc += 1
return wc

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

So if `.find()` doesn’t do what we want, why use it? Maybe a better function exists? Or we can implement an algorithm ourselves

Have loop been introduced yet? It would allow us if there where many more matches in the word

1 Like

Do you mean for loops? Yes we learned them.

Have you considered using a loop here?

How could I have used a loop here?

Many different ways. think about, if there are 3, 4 or even more matches in a word, using a loop would eliminate a lot of the otherwise duplicate code

You could decide not to use `.find()`, instead implement your own algorithm, with a loop we also have indices, so we could check if the values at current index + length of `phrase` equals `phrase`.

essentially we are then using string slicing to check every part of the string

Or if you want to use built-in function, i believe there is also `.findAll` (not the recommend approach at this stage in the learning process)

``````def count_multi_char_x(word, phrase):
wc = 0
y=word.find(phrase)
if y > -1:
x=y+3
wc += 1
yeniword =word[x:]
z = yeniword.find(phrase)
if z > -1:
wc += 1
return wc
``````

yeniword simply means new word in Turkish.
I did x = y + 3 because 3 was the length of the argument Rookie mistake, I’ll adjust that.
x= y + len[phrase]

So the new code is:

``````def count_multi_char_x(word, phrase):
wc = 0
y=word.find(phrase)
if y > -1:
x= y + len[phrase]
wc += 1
newword =word[x:]
z = newword.find(phrase)
if z > -1:
wc += 1
return wc
``````

Now the only reason I said it is limited was that I ran out of options when creating new variables. Lets go over my code and let me tell you what I’m doing:
1- We have a function that takes word and phrase attributes.
2- I created the wc variable and set it to 0, I need its value to show how many times the phrase is located in the given word.
3- Next line I used the find to point out the element of the phrase located in my word.
4- Now we begin the if check, I’m checking to see if the phrase is located at all in my word. If it is located then I’m telling the code to define the x variable, which is index of the phrase start + phrase length. I incrase the wc(how many times phrase was spotted in the word) count by one.
5- Now this is the juicy part that limits me, I create a new word. Strings are immutable, meaning I cannot change them at all. So I felt like my only option to search further was to create a new variable and input the string after locating the first phrase. (Mississippi --> issippi)
6- Then I assign the z variable, I suppose this is not needed. Could’ve simply done

``````if newword.find(phrase) > -1:
wc +=1
``````

7- But yeah as you may understand my limitation was having to create a new word and use a variable. How else could I have solved this?

Actually… I just thought of something. What if I used the same variable and redifned it? Not like I need anything from the first definition, it is just a stepping tool in my code.

``````def count_multi_char_x(word, phrase):
wc = 0
y=word.find(phrase)
if y > -1:
x= y + len[phrase]
wc += 1
newword =word[x:]
######################
if newword.find(phrase) > -1:
newword =word[x:]
wc +=1
######################
``````

I just need to loop inbetween # area.

you are thinking in the right direction. What do you still need from me?

Oh nothing! Thanks a lot, problem solved.

You mostly did it yourself (which is even better), good job

I’ve been meaning to ask, is it normal for me to spend like 30-60 minutes on an assignment like this? I have this feeling that I am constantly overcomplicating stuff. Due to my inability to remember the shortcut the process takes quite long. Is this a problem?

Like when I tell you to do this:
15 * 14 * 2 * 20=
You’ll most likely just do it in your head ( 20 * 2 = 40 * 15 = 600 * 14 = 8400

Where as I feel like I’m doing this ( 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15 + 15)

Which takes longer. Does what I’m saying make any sense?

depends how long you are already programming. If you program 20 hours a week for 5 years and such a challenge still would take you 30-60 minutes, depending on the solution you come up with, that might be worrying

in the beginning, these things take time. That is totally fine.

1 Like