Second largest · 第二大的值
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
This one builds on a lesson you haven't finished yet. Do that lesson first: · 此任务基于您尚未完成的课程。请先完成以下课程:
Python Basics · Python 基础 · Functions · 函数 →
English
Second largest
Return the second-biggest distinct value. Duplicates of the maximum don't count, so [5, 5, 4] gives 4, not 5.
One clean route: drop duplicates with set(...), sort the result, and take the value one in from the end with [-2].
中文
第二大的值
返回第二大的不同值。与最大值相同的重复项不算,所以 [5, 5, 4] 得到 4,而不是 5。
一个简洁的思路:用 set(...) 去掉重复项,对结果排序,然后用 [-2] 取倒数第二个值。
Write second_largest(xs) that returns the second-largest distinct value in xs. You may assume there are at least two distinct values. · 编写 second_largest(xs),返回 xs 中第二大的不同值。可以假设至少存在两个不同的值。
Click Run to see the output here. · 点击“运行”查看此处输出。