文字列の良さは、次の2つのルールに従います。
- 1つ以上の'u'を含む文字列の良さは0です
- それ以外の場合、文字列の良さは文字列内の'g'の数に等しくなります
「gbbgb」は2
「gubgb」は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
誰かが私にこれを説明してもらえますか、私はそれを理解していません。