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 เต็ม จนกว่าผู้ใช้จะป้อนค่าที่ไม่ใช่ตัวเลข

Moving Average ค่าเฉลี่ยเคลื่อนที่
1 The problem โจทย์

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)

2 Main program — flowchart explanation โปรแกรมหลัก — คำอธิบาย flowchart

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 แล้วจบการทำงานของโปรแกรม

Start Set up lists, counters, and flag Numbers = [ ], window = [ ] i = 0, n = 4 p_status = True loop Loop while p_status is True p_status == True? No Yes Ask the user for a value user_input = input(...) Check if value is a number user_input.isdigit()? No Stop the loop next check p_status = False Yes Convert the text to a number Number = float(user_input) Remember every value entered Numbers.append(Number) Check if the window is full i > n - 1? No Add to window (not full) window.append(Number) Window not full yet "...: Not enough data" Yes Find slot to overwrite index = i % n Overwrite the oldest value window[index] = Number Compute the moving average mean = average(window) Report the moving average "Moving Averages (n={n}): {mean}" Move to the next round i += 1 Stop
The runnable code below reads values with input(), exactly like the flowchart. Edit the values in the [input] box (the last one must be non-numeric, e.g. done, to stop the loop), then press Run. โค้ดที่รันได้ด้านล่างรับค่าด้วย input() ตรงตาม flowchart แก้ค่าต่างๆ ในกล่อง [input] ได้ (ค่าสุดท้ายต้องไม่ใช่ตัวเลข เช่น done เพื่อให้ลูปหยุด) แล้วกด Run
[main]
[input]
[output]
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] ทันที

3 average(datas)

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

average(datas) Start total at zero Total = 0 Start index at zero i = 0 loop Check for another item i < len(datas)? No Yes Add item into the total Total += datas[i] Move to next item i++ Divide total by count Average_number = Total / len(datas) Send result back to caller return Average_number
Total is an accumulator: it starts at 0 and each value in datas is added in before dividing by the count. Total เป็น ตัวสะสมค่า: เริ่มที่ 0 แล้วบวกค่าใน datas ทีละค่าเข้าไป ก่อนหารด้วยจำนวนค่า
[demo]
[output]
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

End-of-lesson quizแบบฝึกหัดท้ายบท

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

What is the window size (n) used in this example?ในตัวอย่างนี้ ขนาดของ window (ค่า n) เท่ากับเท่าไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) 44

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 44
    Correct! The program sets n = 4 at the start.ถูกต้อง! หน้านี้กำหนด n = 4 ตั้งแต่ต้นโปรแกรม
  • (B) 33
    Wrong: the original diagram's i>3 condition means the window holds 4 values, not 3.ผิด: แผนภาพต้นฉบับใช้เงื่อนไข i>3 ซึ่งหมายถึงหน้าต่างเต็มที่ 4 ค่า ไม่ใช่ 3
  • (C) 1010
    Wrong: 10 doesn't appear anywhere in this program.ผิด: 10 ไม่ปรากฏในโปรแกรมนี้เลย
  • (D) Unlimited — every value entered is keptไม่จำกัด เก็บทุกค่าที่ป้อน
    Wrong: that describes the Numbers list, not window.ผิด: นั่นคือหน้าที่ของ list Numbers ไม่ใช่ window

ข้อ 2

2 / 10

When does the program switch from appending to the window to overwriting a slot in it?โปรแกรมจะเปลี่ยนจากการ append เข้า window ไปเป็นการเขียนทับ (window[index] = number) เมื่อใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) When i > n - 1 (i.e. once the window already holds n values)เมื่อ i > n - 1 (คือเมื่อ window มีครบ n ค่าแล้ว)

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Whenever i > 3, regardless of nเมื่อ i > 3 เสมอ ไม่ว่า n จะเป็นเท่าไร
    Wrong: this was the bug in the source diagram that this page fixes — the 3 was hardcoded instead of based on n.ผิด: นี่คือจุดผิดพลาดในแผนภาพต้นฉบับที่หน้านี้แก้ไขแล้ว — เงื่อนไข 3 ถูกฮาร์ดโค้ดไว้แทนที่จะอิง n
  • (B) When i > n - 1 (i.e. once the window already holds n values)เมื่อ i > n - 1 (คือเมื่อ window มีครบ n ค่าแล้ว)
    Correct! This page fixes the condition to depend on n instead of hardcoding 3.ถูกต้อง! หน้านี้แก้เงื่อนไขให้อิงกับ n แทนที่จะฮาร์ดโค้ดเป็น 3
  • (C) When window is emptyเมื่อ window ว่างเปล่า
    Wrong: an empty window is exactly when the program is still appending, not overwriting.ผิด: window ว่างคือช่วงที่ยังต้อง append อยู่ ไม่ใช่ช่วงเขียนทับ
  • (D) When p_status is Falseเมื่อ p_status เป็น False
    Wrong: p_status = False means the whole loop ends — it has nothing to do with the window.ผิด: p_status = False แปลว่าออกจากลูปทั้งหมด ไม่เกี่ยวกับ window

ข้อ 3

3 / 10

What does index = i % n compute?คำสั่ง index = i % n คำนวณอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) The slot in the window (0 to n-1) to overwrite this roundตำแหน่งใน window (0 ถึง n-1) ที่จะถูกเขียนทับรอบนี้

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) The total number of rounds so farจำนวนรอบทั้งหมดที่ผ่านมา
    Wrong: that's just i itself, not the result of i % n.ผิด: นั่นคือค่า i เอง ไม่ใช่ผลลัพธ์ของ i % n
  • (B) The average of the windowค่าเฉลี่ยของ window
    Wrong: the average comes from the average(window) function, not the % operator.ผิด: ค่าเฉลี่ยมาจากฟังก์ชัน average(window) ไม่ใช่ตัวดำเนินการ %
  • (C) The slot in the window (0 to n-1) to overwrite this roundตำแหน่งใน window (0 ถึง n-1) ที่จะถูกเขียนทับรอบนี้
    Correct! i % n cycles through positions 0..n-1, making the window act as a circular buffer.ถูกต้อง! i % n วนตำแหน่ง 0..n-1 ซ้ำ ทำให้ window ทำงานแบบ circular buffer
  • (D) The number of values not yet enteredจำนวนค่าที่ยังไม่ได้ป้อน
    Wrong: this program reads an unlimited number of values — there's no such count.ผิด: โปรแกรมนี้รับค่าไม่จำกัด ไม่มี "จำนวนที่ยังไม่ได้ป้อน"

ข้อ 4

4 / 10

What happens when the user enters something that isn't a number (e.g. types letters)?เมื่อผู้ใช้ป้อนค่าที่ไม่ใช่ตัวเลข (เช่น พิมพ์ตัวอักษร) จะเกิดอะไรขึ้น?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) p_status is set to False and the main loop endsp_status ถูกตั้งเป็น False แล้วลูปหลักจบการทำงาน

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) The program errors out immediately with no messageโปรแกรม error และหยุดทันทีโดยไม่แสดงอะไร
    Wrong: the program checks with isdigit() first, so it doesn't error.ผิด: โปรแกรมตรวจสอบด้วย isdigit() ก่อน จึงไม่ error
  • (B) The value is converted to 0 and the program continuesค่านั้นถูกแปลงเป็น 0 แล้วทำงานต่อ
    Wrong: a non-numeric value is never converted at all, because isdigit() is checked first.ผิด: ค่าที่ไม่ใช่ตัวเลขจะไม่ถูกแปลงเลย เพราะเช็ค isdigit() ก่อน
  • (C) p_status is set to False and the main loop endsp_status ถูกตั้งเป็น False แล้วลูปหลักจบการทำงาน
    Correct! This is the only way to exit the while loop in this program.ถูกต้อง! นี่คือทางเดียวที่จะออกจากลูป while ในโปรแกรมนี้
  • (D) The program keeps asking until a valid value is enteredโปรแกรมขอให้ป้อนค่าใหม่ซ้ำจนกว่าจะถูกต้อง
    Wrong: the flowchart has no retry loop — an invalid value ends the loop immediately.ผิด: flowchart ไม่มีการวนถามซ้ำ — ค่าไม่ถูกต้องจะจบลูปทันที

ข้อ 5

5 / 10

Before the window has n values yet, what message does the program print?ก่อนที่ window จะมีครบ n ค่า โปรแกรมจะพิมพ์ข้อความอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) "Moving Averages (n=4): Not enough data""Moving Averages (n=4): Not enough data"

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) "Moving Averages (n=4): Not enough data""Moving Averages (n=4): Not enough data"
    Correct! While i <= n-1 the window isn't full yet, so no average can be computed.ถูกต้อง! เมื่อ i <= n-1 window ยังไม่เต็ม จึงยังคำนวณค่าเฉลี่ยไม่ได้
  • (B) The average of whatever values exist so far (even if fewer than n)ค่าเฉลี่ยของค่าที่มีอยู่ตอนนั้น (แม้ยังไม่ครบ n ค่า)
    Wrong: this program never computes an average until the window holds n values.ผิด: โปรแกรมนี้จะไม่คำนวณค่าเฉลี่ยจนกว่า window จะเต็ม n ค่า
  • (C) It prints nothing at allไม่พิมพ์อะไรเลย
    Wrong: every valid round of the loop always produces an Output.ผิด: ทุกรอบของลูป (ที่ป้อนค่าถูกต้อง) จะมีการ Output เสมอ
  • (D) "program_status = False""program_status = False"
    Wrong: this isn't a printed message — it was a mismatched variable name in the source diagram (fixed to p_status on this page).ผิด: นี่ไม่ใช่ข้อความที่พิมพ์ แต่เป็นชื่อตัวแปรที่ไม่ตรงกันในแผนภาพต้นฉบับ (แก้เป็น p_status แล้วในหน้านี้)

ข้อ 6

6 / 10

Inside average(datas), what should be added into Total on each round?ในฟังก์ชัน average(datas) ค่าที่ควรบวกเข้า Total ในแต่ละรอบคืออะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) datas[i]datas[i]

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) sideside
    Wrong: this was the bug in the source diagram — side isn't a parameter of this function.ผิด: นี่คือจุดผิดพลาดในแผนภาพต้นฉบับ — side ไม่ใช่พารามิเตอร์ของฟังก์ชันนี้
  • (B) datas[i]datas[i]
    Correct! The function's parameter is called datas, so it must add datas[i].ถูกต้อง! ฟังก์ชันรับพารามิเตอร์ชื่อ datas จึงต้องบวกด้วย datas[i]
  • (C) ii
    Wrong: i is the index counter, not a data value to sum.ผิด: i คือดัชนี ไม่ใช่ค่าข้อมูลที่จะรวม
  • (D) nn
    Wrong: n belongs to the main program and is never passed into this function.ผิด: n เป็นตัวแปรของโปรแกรมหลัก ไม่ได้ถูกส่งเข้าฟังก์ชันนี้

ข้อ 7

7 / 10

The final line of average(datas) should divide by which variable?บรรทัดสุดท้ายของ average(datas) ควรหารด้วยตัวแปรใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) Total (capital T, the same accumulator that was built up)Total (ตัวพิมพ์ใหญ่ ตัวเดียวกับตัวสะสมค่า)

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) total (lowercase)total (ตัวพิมพ์เล็ก)
    Wrong: this was the bug in the source diagram — lowercase total was never assigned anywhere.ผิด: นี่คือจุดผิดพลาดในแผนภาพต้นฉบับ — total ตัวพิมพ์เล็กไม่เคยถูกกำหนดค่าไว้เลย
  • (B) Total (capital T, the same accumulator that was built up)Total (ตัวพิมพ์ใหญ่ ตัวเดียวกับตัวสะสมค่า)
    Correct! It must divide by the very same variable that was set to Total = 0 and accumulated.ถูกต้อง! ต้องหารด้วยตัวแปรเดียวกับที่ตั้งค่า Total = 0 และสะสมค่าไว้
  • (C) nn
    Wrong: n is the main program's window size, not a variable inside average(datas).ผิด: n เป็นขนาด window ของโปรแกรมหลัก ไม่ใช่ตัวแปรใน average(datas)
  • (D) ii
    Wrong: i is the loop index; once the loop ends, i equals len(datas), not a sum.ผิด: i คือดัชนีของ loop เมื่อ loop จบ i จะเท่ากับ len(datas) ไม่ใช่ผลรวม

ข้อ 8

8 / 10

If window = [20, 6, 8, 14], what does average(window) return?ถ้า window = [20, 6, 8, 14] ผลลัพธ์ของ average(window) คือเท่าไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) 12.012.0

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 4848
    Wrong: 48 is the sum (20+6+8+14), but it hasn't been divided by the count yet.ผิด: 48 คือผลรวม (20+6+8+14) แต่ยังไม่ได้หารด้วยจำนวนค่า
  • (B) 13.513.5
    Wrong: 13.5 was the average of the previous round's window ([20,12,8,14]), not this one.ผิด: 13.5 คือค่าเฉลี่ยของรอบก่อนหน้า (window = [20,12,8,14]) ไม่ใช่รอบนี้
  • (C) 12.012.0
    Correct! (20+6+8+14)/4 = 48/4 = 12.0ถูกต้อง! (20+6+8+14)/4 = 48/4 = 12.0
  • (D) 44
    Wrong: 4 is the number of values in the window (len(datas)), not the average.ผิด: 4 คือจำนวนค่าใน window (len(datas)) ไม่ใช่ค่าเฉลี่ย

ข้อ 9

9 / 10

What causes the main loop in this example's demo to stop?อะไรทำให้ลูปหลักในตัวอย่าง demo นี้หยุดทำงาน?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) Entering something that isn't a number, e.g. typing "done"การป้อนค่าที่ไม่ใช่ตัวเลข เช่น พิมพ์ "done"

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Entering something that isn't a number, e.g. typing "done"การป้อนค่าที่ไม่ใช่ตัวเลข เช่น พิมพ์ "done"
    Correct! A value where isdigit() is False sets p_status = False and ends the loop.ถูกต้อง! ค่าที่ isdigit() เป็น False จะทำให้ p_status = False และจบลูป
  • (B) Once the window fills to n valuesเมื่อ window เต็มครบ n ค่า
    Wrong: once the window fills, the program keeps computing averages — it doesn't stop.ผิด: เมื่อ window เต็ม โปรแกรมจะเริ่มคำนวณค่าเฉลี่ยต่อไปเรื่อยๆ ไม่ได้หยุด
  • (C) After 10 values have been enteredเมื่อป้อนค่าครบ 10 ค่า
    Wrong: there's no such fixed count in this program — it reads without limit until a non-numeric value.ผิด: ไม่มีขอบเขตจำนวนค่าแบบนั้นในโปรแกรมนี้ — รับได้ไม่จำกัดจนกว่าจะป้อนค่าไม่ใช่ตัวเลข
  • (D) When i % n == 0เมื่อ i % n == 0
    Wrong: i % n == 0 happens normally every n rounds — it doesn't stop the loop.ผิด: i % n == 0 เกิดขึ้นเป็นปกติทุก n รอบ ไม่ได้ทำให้ลูปหยุด

ข้อ 10

10 / 10

What is the difference between Numbers and window?Numbers กับ window ต่างกันอย่างไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) Numbers keeps every value ever entered, while window keeps only the last n values in a circular bufferNumbers เก็บทุกค่าที่เคยป้อนทั้งหมด ส่วน window เก็บเฉพาะ n ค่าล่าสุดแบบวงกลม

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Numbers keeps only the last n values, while window keeps every valueNumbers เก็บเฉพาะ n ค่าล่าสุด ส่วน window เก็บทุกค่า
    Wrong: this swaps the roles — window is the one that keeps only the last n values.ผิด: สลับหน้าที่กัน — ที่ถูกคือ window เก็บ n ค่าล่าสุด
  • (B) They always hold exactly the same values throughout the programทั้งสองตัวเก็บค่าชุดเดียวกันทุกประการตลอดการทำงาน
    Wrong: once window is full it overwrites old values, while Numbers still keeps every old value.ผิด: หลังจาก window เต็ม มันจะเขียนทับค่าเก่าทิ้ง ในขณะที่ Numbers ยังเก็บค่าเก่าไว้ครบ
  • (C) window is only for display and doesn't actually store valueswindow ใช้แสดงผลเท่านั้น ไม่ได้เก็บค่าจริง
    Wrong: window is a real list holding the last n values, and it's actually passed into average(window).ผิด: window เป็น list จริงที่เก็บค่าล่าสุด n ค่า และถูกส่งเข้า average(window) จริงๆ
  • (D) Numbers keeps every value ever entered, while window keeps only the last n values in a circular bufferNumbers เก็บทุกค่าที่เคยป้อนทั้งหมด ส่วน window เก็บเฉพาะ n ค่าล่าสุดแบบวงกลม
    Correct! Numbers grows without bound, while window stays size n by overwriting old slots.ถูกต้อง! Numbers โตขึ้นเรื่อยๆ ไม่มีขีดจำกัด ส่วน window คงขนาด n ด้วยการเขียนทับตำแหน่งเดิม