![](/img/trans.png)
[英]Creating a function that takes a DataFrame as an argument and returns a dictionary of scores
[英]Python: Creating a function that returns a list of scores in a competition
我無法使用以下標准創建 function。 我認為這是一個巧合,我的 function 有時會提出正確的答案。
例子:
next_round(2, 3, [1, 3, 2, 4])
next_round(10, 5, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
回報:
2
10
我的代碼:
def next_round(k, limit, scores):
"""Returns number of people progressing to the next round."""
for n in scores:
if n > limit:
del scores[k:]
if n < limit:
scores.remove(n) # I think my problem is here but not sure what to do.
return len(scores)
謝謝!
讓我知道最后一個項目符號,我會相應地更新我的答案。
def next_round(k, limit, scores):
"""Returns number of people progressing to the next round."""
progressing = 0
# Look at the top k scores.
topk = sorted(scores, reverse=True)[:k]
for score in topk:
if score >= limit:
progressing += 1
# And also include people with the same score as the k-th
# person with score greater than limit. (last bullet)
rest = sorted(scores, reverse=True)[k:]
for score in rest:
if score == topk[-1] and score > limit:
progressing += 1
return progressing
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.