Example 3 — Moving Average ตัวอย่างที่ 3 — ค่าเฉลี่ยเคลื่อนที่ (Moving Average)
A worked problem: read numbers one at a time forever, keep only the last few in a circular window, and report the moving average once the window fills — until the user enters something that isn't a number. โจทย์ตัวอย่าง: รับตัวเลขทีละค่าไม่จำกัดจำนวน เก็บค่าล่าสุดไว้ใน window แบบวงกลม แล้วรายงานค่าเฉลี่ยเคลื่อนที่เมื่อ window เต็ม จนกว่าผู้ใช้จะป้อนค่าที่ไม่ใช่ตัวเลข
Read numbers one at a time, with no fixed limit on how many. Keep only the most recent n = 4 values in a list called window: once it holds 4 values, each new number overwrites the oldest one instead of growing the list further (a circular buffer). Every round that window is full, print the moving average of its current 4 values using a user-defined function average(datas). Stop reading as soon as the user enters something that isn't a valid number.
รับตัวเลขทีละค่า ไม่จำกัดจำนวนรอบ โดยเก็บเฉพาะ 4 ค่าล่าสุด (n = 4) ไว้ในลิสต์ชื่อ window เมื่อมีครบ 4 ค่าแล้ว ค่าใหม่ที่เข้ามาจะเขียนทับค่าเก่าที่สุดแทนที่จะเพิ่มเข้าไปเรื่อยๆ (circular buffer) ทุกรอบที่ window เต็ม ให้พิมพ์ค่าเฉลี่ยเคลื่อนที่ของ 4 ค่านั้น ด้วยฟังก์ชันที่นิยามเอง average(datas) และให้หยุดรับค่าทันทีที่ผู้ใช้ป้อนสิ่งที่ไม่ใช่ตัวเลขที่ถูกต้อง
- average(datas) — the arithmetic mean of a list of numbers
- average(datas) — ค่าเฉลี่ยเลขคณิตของลิสต์ตัวเลข
Ideas used: an unbounded while loop with a stop condition, input validation (isdigit), a circular buffer built with the modulo operator (i % n), index assignment (window[index] = ...), and a user-defined function (def / return).
แนวคิดที่ใช้: loop while ที่ไม่จำกัดรอบพร้อมเงื่อนไขหยุด, การตรวจสอบค่าที่รับเข้ามา (isdigit), circular buffer ด้วยตัวดำเนินการ mod (i % n), การเขียนทับค่าด้วยดัชนี (window[index] = ...) และฟังก์ชันที่นิยามเอง (def / return)
The program starts by setting Numbers = [] and window = [] as empty lists, i = 0 as the round counter, n = 4 as the window size, and p_status = True as the flag that keeps the loop running. Next it checks whether p_status == True; if it is, an input asks the user for a value. It checks whether that value isdigit; if it is not, it sets p_status = False so the loop will stop. If it is, it converts the value with Number = float(user_input), stores it with Numbers.append(Number), then checks whether i > n - 1. If it is not yet (the window isn't full), it adds the number with window.append(Number) and prints "Not enough data". If it is, it computes index = i % n, overwrites that slot with window[index] = Number, finds mean = average(window) using the sub-function average, and prints the moving average. Either way, i is then increased by 1 and control goes back to test p_status == True again. This repeats until p_status is false, so it leaves the loop and the program ends.
โปรแกรมนี้เริ่มต้นด้วยกำหนดค่า Numbers = [] และ window = [] เป็นลิสต์ว่าง i = 0 เป็นตัวนับจำนวนรอบ n = 4 เป็นขนาด window และ p_status = True เป็นตัวธงที่คุมให้ลูปทำงานต่อ ถัดไปตรวจสอบเงื่อนไขว่า p_status == True หรือไม่ ถ้าใช่ให้สร้าง input ถามค่าจากผู้ใช้ แล้วตรวจสอบว่าค่านั้น isdigit หรือไม่ ถ้าไม่ใช่ให้ตั้งค่า p_status = False เพื่อให้ลูปหยุดทำงาน ถ้าใช่ให้แปลงค่าด้วย Number = float(user_input) แล้วเก็บค่าด้วย Numbers.append(Number) จากนั้นตรวจสอบเงื่อนไขว่า i > n - 1 หรือไม่ ถ้ายังไม่ถึง (window ยังไม่เต็ม) ให้เพิ่มค่าด้วย window.append(Number) แล้วพิมพ์ "Not enough data" ถ้าถึงแล้วให้คำนวณ index = i % n แล้วเขียนทับตำแหน่งนั้นด้วย window[index] = Number จากนั้นหาค่า mean = average(window) ด้วย function ย่อยชื่อ average แล้วพิมพ์ค่าเฉลี่ยเคลื่อนที่ ไม่ว่าทางใดทางหนึ่ง หลังจากนั้นเพิ่มค่า i ขึ้น 1 แล้วกลับไปตรวจสอบ p_status == True อีกครั้ง ทำแบบนี้จน p_status เป็นเท็จ จึงออกจาก loop แล้วจบการทำงานของโปรแกรม
Enter a number (or a word to stop): 10 Moving Averages (n=4): Not enough data Enter a number (or a word to stop): 12 Moving Averages (n=4): Not enough data Enter a number (or a word to stop): 8 Moving Averages (n=4): Not enough data Enter a number (or a word to stop): 14 Moving Averages (n=4): Not enough data Enter a number (or a word to stop): 20 Moving Averages (n=4): 13.5 Enter a number (or a word to stop): 6 Moving Averages (n=4): 12.0 Enter a number (or a word to stop): done
Try it yourself — the first value you submit below clears the example trace and starts a fresh run with your own numbers. After that, each value you submit shows its result in [output] right away.
ลองป้อนค่าเอง —ค่าแรกที่กดส่งด้านล่างจะล้างตัวอย่างเดิม แล้วเริ่มรันใหม่ด้วยตัวเลขของคุณ หลังจากนั้น ทุกค่าที่กดส่งจะขึ้นผลลัพธ์ที่ [output] ทันที
This function starts by setting Total = 0 as an accumulator and i = 0 as the index. Next it checks whether i < len(datas); if it is, it adds the current value into Total with Total += datas[i], then increases i by 1 and goes back to test i again. This repeats until i < len(datas) is false, so it leaves the loop, computes Average_number = Total / len(datas), and sends Average_number back with return.
ฟังก์ชันนี้เริ่มต้นด้วยกำหนดค่า Total = 0 เป็นตัวสะสมค่า และ i = 0 เป็นดัชนี ถัดไปตรวจสอบเงื่อนไขว่า i < len(datas) หรือไม่ ถ้าน้อยกว่าให้บวกค่าปัจจุบันเข้าไปที่ Total ด้วย Total += datas[i] แล้วเพิ่มค่า i ขึ้น 1 แล้วกลับไปตรวจสอบค่า i อีกครั้ง ทำแบบนี้จน i < len(datas) เป็นเท็จ จึงออกจาก loop แล้วคำนวณ Average_number = Total / len(datas) แล้วส่งค่า Average_number กลับด้วยคำสั่ง return
12.0
The Run buttons above simulate this Python with JavaScript, the same as the other lessons. To run the real Python, paste the code into Google Colab.
ปุ่ม Run ด้านบนจำลองโค้ด Python ด้วย JavaScript เหมือนบทเรียนอื่นๆ หากต้องการรัน Python จริง ให้คัดลอกโค้ดไปวางที่ Google Colab
References อ้างอิง
- Python docs — Lists (list, append, index assignment used for Numbers and window) (list, append, การเขียนทับด้วยดัชนี ที่ใช้กับ Numbers และ window)
- Python docs — Binary arithmetic operations (the % modulo operator used to build the circular window) (ตัวดำเนินการ % ที่ใช้ทำ circular buffer ของ window)
- Flowchart display and symbols follow 01 Basic → Flowchart Basic. รูปแบบและสัญลักษณ์ของ flowchart อ้างอิงตาม 01 Basic → Flowchart Basic