David Gourley
David Gourley

Reputation: 1

Need steps for a basic python loop question

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!

Upvotes: 0

Views: 438

Answers (5)

Rob T
Rob T

Reputation: 74

This could also work

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

Upvotes: 0

Peter Ark
Peter Ark

Reputation: 244

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

Upvotes: 0

Zachary Sherlock
Zachary Sherlock

Reputation: 36

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.

Upvotes: 1

Andrej Kesely
Andrej Kesely

Reputation: 195593

# 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

Upvotes: 2

Blanks
Blanks

Reputation: 108

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)

Upvotes: 0

Related Questions