gpt4 book ai didi

python - 澄清简单的功能

转载 作者:太空宇宙 更新时间:2023-11-04 10:51:02 26 4
gpt4 key购买 nike

<分区>

字符串的优点遵循以下两个规则:

  • 包含一个或多个'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

谁能帮我解释一下,我不明白。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com