字符串的优劣遵循以下两条规则:
- 包含一个或多个 'u' 的字符串的优度为 0
- 否则,字符串的优度等于字符串中 'g' 的数量
"gbbgb" 是 2
"gbbgb" 是 0
#I understand this function
def goodness(s):
if s.count('u') > 0:
return 0
else:
return s.count('g')
#But not this one.
def best_slice(s, k):
''' s is str, k is an integer such that 0 <= k <= len(s). Return the starting index of the length-k slice of s with highest goodness. If k is zero, return -1.'''
stop = len(s) - k # ?
best_start = -1 # ?
best_goodness = 0
for i in range(stop + 1):
cur_slice = s[i:i+k]
slice_goodness = goodness(cur_slice)
if slice_goodness > best_goodness:
best_start = i
best_goodness = slice_goodness
return best_start
有人可以为我解释一下吗,我不明白。