Smallest and largest · เล็กสุดและใหญ่สุด
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.
น้อยสุดและมากที่สุด
หาขอบเขตทั้งสองของรายการ在一次 pass (in a single pass).
เก็บสองค่าพร้อมกัน, ทั้งคู่เริ่มต้นด้วยไอเท็มแรก. ตรวจสอบแต่ละตัวเลขเทียบกับทั้งสอง: มันอาจแทนที่ค่าน้อยสุด, ค่ามากที่สุด, หรือ neither (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) ที่คืนค่ารายการ [smallest, largest] โดยใช้ลูป หนึ่ง ครั้งและไม่ใช้ min หรือ max รายการจะไม่ว่างเปล่า
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่