3
\$\begingroup\$

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 picked up is a Bob
 if "bob" in x:
 # increasing the Bob count
 numBob += 1
 # Printing the number of Bobs
print "Number of times bob occurs is:" + str(numBob) 
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Sep 18, 2016 at 19:58
\$\endgroup\$
1
  • 1
    \$\begingroup\$ It's recommended to follow the style guide: python.org/dev/peps/pep-0008 \$\endgroup\$ Commented Sep 18, 2016 at 20:03

1 Answer 1

5
\$\begingroup\$

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.

answered Sep 18, 2016 at 20:26
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Even simpler: def count(substring, string): return sum(string.startswith(substring, i) for i in range(len(string) - len(substring))) \$\endgroup\$ Commented Sep 20, 2016 at 7:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.