Smallest and 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
Smallest and largest
Find both ends of a list in a single pass.
Keep two values at once, both starting as the first item. Check each number against both: it may replace the smallest, the largest, or neither.
中文
最小值和最大值
只遍历一遍,就找出列表的两端。
同时记住两个值,开始时都等于第一个元素。把每个数和这两个值都比较一下:它可能替换最小值、最大值,或者都不替换。
Write min_max(nums) that returns a list [smallest, largest] using one · 一个 loop and no min or · 或 max. The list is never empty. · 编写 min_max(nums),只用一个循环、不用 min 和 max,返回列表 [最小值, 最大值]。列表不会是空的。
Click Run to see the output here. · 点击“运行”查看此处输出。