0

I am a complete noob to programming so excuse the ignorance. I am taking a MOOC and can't for the life of me figure out what steps to take to solve this problem:

Assume s is a string of lower case characters.

Write a program that prints the number of times the string 'bob' occurs in s. For example, if s = 'azcbobobegghakl', then your program should print

Number of times bob occurs is: 2

OK. I know that a for loop is necessary. I think it should start with:

x=bob
count=0
for count in range(s):
>if 

Here I am not sure how to retrieve the specific word 'bob' from the index, and I know I need to enter count+=1 but am not sure where. Could someone please help me? I am sure I could solve this on myself but I am getting frustrated after a few hours!

6
  • Youll need range(len(s))` instead of range(s), since s is not an integer. Then, use x == s[count : count+len(x)]. Commented Aug 31, 2020 at 21:58
  • Aha! I was thinking I might have to convert the str into int. Thanks boss. Commented Aug 31, 2020 at 21:59
  • Would I need to nest my if statements after that? I am still uncertain how to begin these next if statements. Commented Aug 31, 2020 at 22:02
  • You don't need to nest any if statements. Think of it this way. When you are looking at string s, how do you know that the word in x has started? Now that you know it's started, how do you know it has ended? Commented Aug 31, 2020 at 22:05
  • I'm seriously at a standstill. Do I need to redefine x now? I am not sure how to isolate the value of an individual letter. Do I need to use an if statement at all? Here is what I have now: for x in range(len(s)): >x==s[count:count+lensx)] Commented Aug 31, 2020 at 22:29

6 Answers 6

2
# our input variables:
term = 'bob'
s = 'azcbobobegghakl'

# where I store count of found search term:
count = 0

# we are going from 0 to length of input string:
for i in range(len(s)):

    # compare the slice from <current index> to <current index + length of search term> to search term
    if s[i:i+len(term)] == term:
        # it there's match, increase the count
        count += 1

print(count)

Prints:

2
Sign up to request clarification or add additional context in comments.

Comments

1

So here is my solution:

s = 'azcbobobegghakl' 
x = 'bob'
count = 0
for i in range(len(s)):
    if x == s[i : i + len(x)]:
        count+= 1
print(count)

Few things to note. First of all your for statement had a couple errors, where you are using the variable count before the statement but also creating a new variable count in the for statement, and so I replaced it in the statement with the variable name i so that it would not override your first count variable. Secondly, it would need to be range(len(s)) as s is a string and you will want to iterate up to it's length.

Finally to explain the if x == s[i : i + len(x)]: part, this is to see if X is the same as the i'th position in S, along with the next 2 letters in S, because the length of X ('bob') is 3, so you'll need to be looking at every len(x) or three letters in the s string.

Comments

0

I did something like this, I am pretty sure someone can optimise the code, I just tried a quick one. It works so far.

keyword = "bob"
text = "azcbobobegghakl"
n = len(keyword)
temp = ""
counter = 0
for x in (text):
    temp += x
    if len(temp)%(n+1) == 0:
        temp = temp[1::]
        if keyword in temp:
            counter += 1
print(counter)

2 Comments

Thanks. It seems to work but it is somehow missing just one 'bob' in a few instances for example: 'bobobobobobobobobobob' 10 was expected. I will use your answer as I could not for the life of me wrap my head around how to go about this problem. Any tips or resources for practicing and learning more about these? Most guides did not provide more info than the lecture.
go for it, I didnt realise that, yes is true, it is missing the first bob. I think HackerRank.com is a great place to practice code. I use it from time to time.
0

Just a suggestion to complete the answers already given.

You probably could start by checking if the string you are looking for, 'bob', exists in the string to be searched, s, for example:

s = 'azcbobobegghakl'
if 'bob' in s:
    # your searching algotithm

Comments

0

This could also work

s = 'azcbobobegghakl' 
x = 'bob'
count = 0
for i in range(len(s)):
    if s[i:].startswith(x):
            count += 1

Comments

0

I hope this helps ,

Actually this code iterates through the text with the use of while loop

  1. text.find(keyword, index) searches for "bob" starting from the current index.

  2. If a match is found, count is increased by one.

  3. The crucial step is index += 1. This moves the search position forward by just one character, which is why it can find the second "bob" in "azc**bobob**egghakl".

  4. The loop stops when .find() can no longer locate the keyword and returns -1.

keyword = "bob"
text = "azcbobobegghakl"
count = 0
index = 0

while index < len(text):
    index = text.find(keyword, index)
    if index == -1:
        break
    count += 1
    index += 1

print(count)

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.