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)
-
1\$\begingroup\$ It's recommended to follow the style guide: python.org/dev/peps/pep-0008 \$\endgroup\$janos– janos2016年09月18日 20:03:10 +00:00Commented Sep 18, 2016 at 20:03
1 Answer 1
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.
-
1\$\begingroup\$ Even simpler:
def count(substring, string): return sum(string.startswith(substring, i) for i in range(len(string) - len(substring)))
\$\endgroup\$200_success– 200_success2016年09月20日 07:33:51 +00:00Commented Sep 20, 2016 at 7:33