Skip to content Skip to sidebar Skip to footer

Bob Counter In Python

Using Python 2.7, I was attempting to count the number of occurances of 'bob' in the phrase 'bobbbobobboobobookobobbobbboj.' To do this, I wrote the code below: b=0 string='bob

Solution 1:

let's see if we can help you out.

Your code is

s='bobbbobobboobobookobobbobbboj'

 for i in s:

     if(i=='BOB' or i=='bob'):
        b=b+1

It is important to think about this- a string like "s" is a list of characters. When you do for i in s, you are looping through each individual character.

on the first iteration, i == 'b', on the second one it equals 'o' and so on.

What you want is something that checks sections of code. a way of doing that would be

for i in range(len(s)):
    if s[i:i+4] == "bob":

The reason this works is that range returns a list of numbers in order, so it will go 0, 1, 2, 3... the [i:i+4] part cuts out a section of the string based on the how far into the string it is. For your string s[0:2] would be "bob" (it starts with 0).

I left several problems... for example, if you let it run to the end you'll have a problem (if s is 10 characters long and you try to do s[9:12] you will get an error) ... but that should help you get going


Solution 2:

>>> s = 'bobbbobobboobobookobobbobbboj'
>>> term = 'bob'
sum(1 for i, j in enumerate(s) if s[i:i+len(term)] == term)
6

Solution 3:

The Bob Counter Program Answer In Python

s = ('bobobobobobob') #Declare the variable 's' and assign it to a string with 6 bobs in it.
count = 0 #Declare the variable 'count' and set it equal to 0.

for i in range(len(s)): # for the number i in the length of 's' (12 in our example)
    if s[i:i+3] == "bob": # Example: if s[0:3] == 'bob' is true
        count += 1 # Increase count by 1 to keep track of how many times 'bob' occurs.
  # else :
      # 'count' is not incremented because the string does not equal 'bob'.
      # It most likely says 'obs' in our example instead of 'bob'.

    # Next number. On the second loop, i increments from 0 to 1 and it loops through again.

print ('Number of times bob occurs is: ' + str(count)) # Output: 'Number of times bob occurs is: 6'

If you need further explanation on this, I would recommend that you put this into a python IDE such as http://pythontutor.com/visualize.html#mode=edit and step through the program.


Solution 4:

If you want to do it the Pythonic way, then you'll want to read up on the string find function.

If you want to learn how make a string find function, then you'll want to read up on string searching algorithms.


Solution 5:

Here is an implementation based on Brandon's answer to a linked question.

def MIT(full_string, substring):
    results = []
    sub_len = len(substring)
    for i in range(len(full_string)):
    # range returns a list of values from 0 to (len(full_string) - 1)
        if full_string[i:i+sub_len] == substring:
        # this is slice notation;
        # it means take characters i up to (but not including) i
        # + the length of th substring
            results.append(i)
    return results

full_string = "bobBOBBobBoj"
lower_substring = "bob"
upper_substring = "BOB"
lower_occurrence_array = MIT(full_string, lower_substring)
upper_occurrence_array = MIT(full_string, upper_substring)
number_of_lower_occurrences = len(lower_occurrence_array)
number_of_upper_occurrences = len(upper_occurrence_array)
number_of_occurrences = number_of_lower_occurrences + number_of_upper_occurrences

print ("Number of times bob occurs is: %s" %number_of_occurrences)

The result is

Number of times bob occurs is: 2

The main gist is already there with Paul's answer but I hope this helps.


Post a Comment for "Bob Counter In Python"