HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Count substring in string

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
countstringsubstring

Problem

With the comments on the same line as the code, it refers to. e.g

NumBob = 0
s = 'sexbobombob'
i = range(len(s)+1) #Generates a list from 0 to the length of s
u = 0 # This would be the variable storing each value of the list
for ans in i: # this allows me to extract each value of i
    if ans > 0: # Completely arbitrary. i just used any condition that allows me to pick up all values in the list.
        u = ans
    x = s[u:u+3] #By using indexing to pick u, u+1, and u +2 i can pick up 3 letters at a time
    if "bob" in x: # Checking if the term i picked up is a Bob
        numBob += 1 # increasing the Bob count
print "Number of times bob occurs is:" + str(numBob) # Printing the number of Bobs


With the comments beneath the line of code, it refers to. e.g

numBob = 0
s = 'sexbobombob'
i = range(len(s)+1)
#Generates a list from 0 to the length of s
u = 0
# This would be the variable storing each value of the list
for ans in i:
    # this allows me to extract each value of i
    if ans > 0:
        # Completely arbitrary. i just used any condition that allows me to pick up all values in the list.
        u = ans
    x = s[u:u+3]
    #By using indexing to pick u, u+1, and u +2 i can pick up 3 letters at a time
    if "bob" in x:
        # Checking if the term i picked up is a Bob
        numBob += 1
        # increasing the Bob count
print "Number of times bob occurs is:" + str(numBob) 
#Printing the number of Bobs


Or with the comments preceding the line of code. e.g

```
numBob = 0
s = 'sexbobombob'
#Generates a list from 0 to the length of s
i = range(len(s)+1)
# This would be the variable storing each value of the list
u = 0
# this allows me to extract each value of i
for ans in i:
# Completely arbitrary. i just used any condition that allows me to pick up all values in the list.
if ans > 0:
u = ans
# By using indexing to pick u, u+1, and u +2 i can pick up 3 letters at a time
x = s[u:u+3]
# Checking if the term i picke

Solution

Using this kind of all-top-level short-named fully imperative style forces you to rely heavily on comments to make the code readable.

Comments everywhere repeating the code violate the DRY principle:

# increasing the Bob count
    numBob += 1


You just doubled reading / maintaining time here.

Descriptive names, examples of usage and sometimes functional style make "how"-kind comments unnecessary:

def count(substring, string):
    """
    >>> count("bob","bobhejrhaabobfqbqsanbobb")
    3
    """
    return sum(substring == group for group in groups(string, len(substring)))

def groups(xs, n):
    """
    >>> groups([1, 2, 3, 4, 5], 3)
    [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
    """
    for i in range(len(xs)-n):
        yield xs[i:i+n]


The code looks self explaining to me, but feel free to ask about any doubts about it.

Code Snippets

# increasing the Bob count
    numBob += 1
def count(substring, string):
    """
    >>> count("bob","bobhejrhaabobfqbqsanbobb")
    3
    """
    return sum(substring == group for group in groups(string, len(substring)))

def groups(xs, n):
    """
    >>> groups([1, 2, 3, 4, 5], 3)
    [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
    """
    for i in range(len(xs)-n):
        yield xs[i:i+n]

Context

StackExchange Code Review Q#141730, answer score: 5

Revisions (0)

No revisions yet.