Example 10 — Find the Best Sales Dayตัวอย่างที่ 10 — หาวันที่ขายได้มากที่สุด
Last updated แก้ไขล่าสุด
Add up the sales of each day over several weeks, then find the day that sold the most.รวมยอดขายของแต่ละวันจากหลายสัปดาห์ แล้วหาว่าวันไหนขายได้มากที่สุด
Read the problem, follow the flowchart, then press Run. You can change the numbers in
week_data to try your own data.
อ่านโจทย์ ไล่ตาม flowchart แล้วกด Run โดยแก้ตัวเลขใน week_data เพื่อลองข้อมูลของตัวเองได้
1The problemโจทย์
The variable week_data holds the number of items a shop sold each day. One row is one
week, with 7 values from Monday to Sunday. Show the name of the day with the highest total over all the weeks.
There is at least one week of data, and no two days share the highest total.
A list can hold lists: week_data[1] is the second row and week_data[1][5] is the sixth value of that row.
[0] * 7 makes a list of seven zeros.
A list can be written over several lines. Python keeps reading until the ] that closes it.
คำสั่งที่ใช้ในโปรแกรมนี้:
list เก็บ list ได้ week_data[1] คือแถวที่ 2 และ week_data[1][5] คือค่าที่ 6 ของแถวนั้น
[0] * 7 สร้าง list ที่มีเลข 0 จำนวน 7 ตัว
list เขียนหลายบรรทัดได้ Python จะอ่านต่อจนเจอ ] ที่ปิดวงเล็บ
2Main program — flowchart and full codeโปรแกรมหลัก — flowchart และโค้ดทั้งหมด
The program starts by setting the sales data in week_data and making
day_sum, 7 slots that hold the total of each day. Next it takes one week at a time as
week and sets pos = 0 to count the position of the day in that
week. Then it takes the sales of one day at a time as sold, adds them to
day_sum[pos] and adds 1 to pos. It repeats this until every day
of every week is done, then leaves the loop. After that it makes day_names to hold the day
names and sets best_day = 0, which treats Monday as the best day for now. It then compares
the other days, from d = 1 to d = 6, checking whether
day_sum[d] > day_sum[best_day]: if so, it keeps d in
best_day; if not, it moves on to the next day. When every day is compared it shows the
name of the day with print, and the program ends.
Change the rows of week_data in card 2 and press Run to check each row of this table. The
first three rows add one week at a time, so you can see the best day change. The last two rows test a best day at
the first and the last position of the list.
แก้แถวใน week_data ของการ์ดที่ 2 แล้วกด Run เพื่อตรวจแต่ละแถวของตารางนี้ 3 แถวแรกเพิ่มข้อมูลทีละสัปดาห์
จึงเห็นวันที่ขายได้มากที่สุดเปลี่ยนไป ส่วน 2 แถวสุดท้ายทดสอบกรณีที่คำตอบอยู่ตำแหน่งแรกและตำแหน่งสุดท้ายของ list
week_data
Totals, Monday to Sundayยอดรวม จันทร์ถึงอาทิตย์
Outputผลลัพธ์
[15, 42, 8, 3, 61, 27, 30]
15, 42, 8, 3, 61, 27, 30
Friday
addเพิ่ม[22, 18, 5, 12, 20, 90, 19]
37, 60, 13, 15, 81, 117, 49
Saturday
addเพิ่ม[0, 0, 0, 0, 50, 0, 0]
37, 60, 13, 15, 131, 117, 49
Friday
[9, 1, 1, 1, 1, 1, 1]
9, 1, 1, 1, 1, 1, 1
Monday
[1, 1, 1, 1, 1, 1, 9]
1, 1, 1, 1, 1, 1, 9
Sunday
4Think furtherชวนคิดต่อ
Suppose the program should also show the total of the best day. Which line would you add?
Add print(day_sum[best_day]) at the end of the program. The loop keeps the position of
the day in best_day, so the same position gives its name in
day_names and its total in day_sum.
คำอธิบายแต่ละตัวเลือก / Explanation for each choice
(A)[0, 0, 0, 0, 0, 0, 0][0, 0, 0, 0, 0, 0, 0]
Correct! It makes a list of seven zeros.ถูกต้อง! ได้ list ที่มีเลข 0 จำนวน 7 ตัว
(B)[7][7]
Wrong: * 7 repeats the items of the list, it does not multiply the value.ผิด: * 7 ทำซ้ำสมาชิกใน list ไม่ได้คูณค่าข้างใน
(C)[0, 7][0, 7]
Wrong: the 7 is not added to the list.ผิด: เลข 7 ไม่ได้ถูกเพิ่มเข้าไปใน list
(D)00
Wrong: the result is still a list, not a number.ผิด: ผลลัพธ์ยังเป็น list ไม่ใช่ตัวเลข
ข้อ 2
2 / 10
In the code on this page, what is week_data[1][5] when week_data = [ [15, 42, 8, 3, 61, 27, 30], [22, 18, 5, 12, 20, 90, 19], ]จากโค้ดในหน้านี้ week_data[1][5] มีค่าเท่าไร? เมื่อ week_data = [ [15, 42, 8, 3, 61, 27, 30], [22, 18, 5, 12, 20, 90, 19], ]
ดูเฉลยแล้ว — ไม่นับคะแนน / Answer viewed — not scored
เฉลย / Answerซ่อนเฉลย / Hide
เฉลย / Answer: (B)9090
คำอธิบายแต่ละตัวเลือก / Explanation for each choice
(A)2727
Wrong: 27 is week_data[0][5], which is in the first row.ผิด: 27 คือ week_data[0][5] ซึ่งอยู่ในแถวแรก
(B)9090
Correct! The second row is index 1, and the sixth value of that row is index 5.ถูกต้อง! แถวที่ 2 คือ index 1 และค่าที่ 6 ของแถวนั้นคือ index 5
(C)1919
Wrong: 19 is the last value of the row, which is index 6.ผิด: 19 คือค่าสุดท้ายของแถว ซึ่งเป็น index 6
(D)2020
Wrong: 20 is the fifth value of the row, which is index 4.ผิด: 20 คือค่าที่ 5 ของแถว ซึ่งเป็น index 4
ข้อ 3
3 / 10
Why is pos = 0 set again for every week? (Each row runs from Monday to Sunday.) day_sum = [0] * 7 for week in week_data: pos = 0 for sold in week: day_sum[pos] += sold pos += 1ทำไมต้องกำหนด pos = 0 ใหม่ทุกสัปดาห์? (แต่ละแถวเรียงจากวันจันทร์ถึงวันอาทิตย์) day_sum = [0] * 7 for week in week_data: pos = 0 for sold in week: day_sum[pos] += sold pos += 1
ดูเฉลยแล้ว — ไม่นับคะแนน / Answer viewed — not scored
เฉลย / Answerซ่อนเฉลย / Hide
เฉลย / Answer: (C)So that the first value of the week is added to Mondayเพื่อให้ค่าแรกของสัปดาห์บวกเข้ายอดวันจันทร์
คำอธิบายแต่ละตัวเลือก / Explanation for each choice
(A)To clear day_sumเพื่อล้าง day_sum
Wrong: day_sum must keep its totals from week to week.ผิด: day_sum ต้องเก็บยอดสะสมไว้ทุกสัปดาห์
(B)To count the weeksเพื่อนับจำนวนสัปดาห์
Wrong: pos counts the position of the day, not the weeks.ผิด: pos นับตำแหน่งของวัน ไม่ได้นับสัปดาห์
(C)So that the first value of the week is added to Mondayเพื่อให้ค่าแรกของสัปดาห์บวกเข้ายอดวันจันทร์
Correct! Every week has to start at the position of Monday.ถูกต้อง! ทุกสัปดาห์ต้องเริ่มจากตำแหน่งของวันจันทร์
(D)To make the loop finish soonerเพื่อให้ loop จบเร็วขึ้น
Wrong: the number of rounds depends on the values in week.ผิด: จำนวนรอบของ loop ขึ้นกับจำนวนค่าใน week
ข้อ 4
4 / 10
After the 2 weeks in this code are added up, what is day_sum[5]? week_data = [ [15, 42, 8, 3, 61, 27, 30], [22, 18, 5, 12, 20, 90, 19], ]
day_sum = [0] * 7 for week in week_data: pos = 0 for sold in week: day_sum[pos] += sold pos += 1หลังรวม 2 สัปดาห์ในโค้ดนี้ day_sum[5] มีค่าเท่าไร? week_data = [ [15, 42, 8, 3, 61, 27, 30], [22, 18, 5, 12, 20, 90, 19], ]
day_sum = [0] * 7 for week in week_data: pos = 0 for sold in week: day_sum[pos] += sold pos += 1
ดูเฉลยแล้ว — ไม่นับคะแนน / Answer viewed — not scored
เฉลย / Answerซ่อนเฉลย / Hide
เฉลย / Answer: (D)117117
คำอธิบายแต่ละตัวเลือก / Explanation for each choice
(A)2727
Wrong: 27 is the value of the first week only.ผิด: 27 คือยอดของสัปดาห์แรกเท่านั้น
(B)9090
Wrong: 90 is the value of the second week only.ผิด: 90 คือยอดของสัปดาห์ที่ 2 เท่านั้น
(C)8181
Wrong: 81 is the total in day_sum[4].ผิด: 81 คือยอดรวมของ day_sum[4]
(D)117117
Correct! 27 + 90 = 117.ถูกต้อง! 27 + 90 = 117
ข้อ 5
5 / 10
Why does best_day start at 0 while d starts at 1? (day_sum holds the totals of Monday to Sunday at positions 0 to 6.) best_day = 0 for d in range(1, 7): if day_sum[d] > day_sum[best_day]: best_day = dทำไม best_day เริ่มที่ 0 แต่ d เริ่มที่ 1? (day_sum เก็บยอดรวมของวันจันทร์ถึงวันอาทิตย์ที่ตำแหน่ง 0 ถึง 6) best_day = 0 for d in range(1, 7): if day_sum[d] > day_sum[best_day]: best_day = d
ดูเฉลยแล้ว — ไม่นับคะแนน / Answer viewed — not scored
เฉลย / Answerซ่อนเฉลย / Hide
เฉลย / Answer: (A)Monday is the starting value, then it is compared with the other daysให้วันจันทร์เป็นค่าตั้งต้น แล้วเทียบกับวันที่เหลือ
คำอธิบายแต่ละตัวเลือก / Explanation for each choice
(A)Monday is the starting value, then it is compared with the other daysให้วันจันทร์เป็นค่าตั้งต้น แล้วเทียบกับวันที่เหลือ
Correct! Monday is the answer for now, so the comparison starts from Tuesday.ถูกต้อง! วันจันทร์คือคำตอบชั่วคราว จึงเริ่มเทียบจากวันอังคาร
(B)Because Monday does not countเพราะวันจันทร์ไม่นับ
Wrong: Monday can be the answer, as with [9, 1, 1, 1, 1, 1, 1].ผิด: วันจันทร์เป็นคำตอบได้ เช่น ข้อมูล [9, 1, 1, 1, 1, 1, 1]
(C)Because a list starts at index 1เพราะ list เริ่มที่ index 1
Wrong: a list starts at index 0.ผิด: list เริ่มที่ index 0
(D)To skip Sundayเพื่อข้ามวันอาทิตย์
Wrong: range(1, 7) includes 6, the position of Sunday.ผิด: range(1, 7) มี 6 ซึ่งเป็นตำแหน่งของวันอาทิตย์
ข้อ 6
6 / 10
The program shows the name of the day with the highest total. If week_data has these 3 weeks, what is the output? week_data = [ [15, 42, 8, 3, 61, 27, 30], [22, 18, 5, 12, 20, 90, 19], [0, 0, 0, 0, 50, 0, 0], ]
ดูเฉลยแล้ว — ไม่นับคะแนน / Answer viewed — not scored
เฉลย / Answerซ่อนเฉลย / Hide
เฉลย / Answer: (B)FridayFriday
คำอธิบายแต่ละตัวเลือก / Explanation for each choice
(A)SaturdaySaturday
Wrong: Saturday stays at 117 but Friday rises to 131.ผิด: วันเสาร์ยังเป็น 117 แต่วันศุกร์เพิ่มเป็น 131
(B)FridayFriday
Correct! Friday rises from 81 to 131, which is more than the 117 of Saturday.ถูกต้อง! วันศุกร์เพิ่มจาก 81 เป็น 131 ซึ่งมากกว่าวันเสาร์ 117
(C)MondayMonday
Wrong: the total of Monday is only 37.ผิด: วันจันทร์มียอดรวมเพียง 37
(D)SundaySunday
Wrong: the total of Sunday is only 49.ผิด: วันอาทิตย์มียอดรวมเพียง 49
ข้อ 7
7 / 10
If > is changed to < in the condition, what does the program show? (day_sum holds the totals of Monday to Sunday at positions 0 to 6.) best_day = 0 for d in range(1, 7): if day_sum[d] > day_sum[best_day]: best_day = d print(day_names[best_day])ถ้าเปลี่ยน > เป็น < ในเงื่อนไข โปรแกรมจะแสดงอะไร? (day_sum เก็บยอดรวมของวันจันทร์ถึงวันอาทิตย์ที่ตำแหน่ง 0 ถึง 6) best_day = 0 for d in range(1, 7): if day_sum[d] > day_sum[best_day]: best_day = d print(day_names[best_day])
ดูเฉลยแล้ว — ไม่นับคะแนน / Answer viewed — not scored
เฉลย / Answerซ่อนเฉลย / Hide
เฉลย / Answer: (C)The day with the lowest totalวันที่ยอดรวมน้อยสุด
คำอธิบายแต่ละตัวเลือก / Explanation for each choice
(A)The best day, the same as beforeวันที่ขายมากสุดเหมือนเดิม
Wrong: the condition is reversed, so the answer changes.ผิด: เงื่อนไขกลับด้าน คำตอบจึงเปลี่ยน
(C)The day with the lowest totalวันที่ยอดรวมน้อยสุด
Correct! The program keeps the day with the smaller total each time.ถูกต้อง! โปรแกรมจะจำวันที่ยอดน้อยกว่าไว้เรื่อยๆ
(D)Always Mondayวันจันทร์เสมอ
Wrong: best_day still changes when a smaller total is found.ผิด: best_day ยังเปลี่ยนได้เมื่อเจอวันที่ยอดน้อยกว่า
ข้อ 8
8 / 10
What does best_day hold? (day_sum holds the totals of Monday to Sunday at positions 0 to 6.) best_day = 0 for d in range(1, 7): if day_sum[d] > day_sum[best_day]: best_day = d print(day_names[best_day])best_day เก็บค่าอะไร? (day_sum เก็บยอดรวมของวันจันทร์ถึงวันอาทิตย์ที่ตำแหน่ง 0 ถึง 6) best_day = 0 for d in range(1, 7): if day_sum[d] > day_sum[best_day]: best_day = d print(day_names[best_day])
ดูเฉลยแล้ว — ไม่นับคะแนน / Answer viewed — not scored
เฉลย / Answerซ่อนเฉลย / Hide
เฉลย / Answer: (D)The position of the day in the listตำแหน่งของวันใน list
คำอธิบายแต่ละตัวเลือก / Explanation for each choice
(A)The highest totalยอดขายสูงสุด
Wrong: the highest total is day_sum[best_day].ผิด: ยอดขายสูงสุดคือ day_sum[best_day]
(B)The name of the dayชื่อวัน
Wrong: the name is day_names[best_day].ผิด: ชื่อวันคือ day_names[best_day]
(C)The number of weeksจำนวนสัปดาห์
Wrong: the program does not count the weeks.ผิด: โปรแกรมไม่ได้นับจำนวนสัปดาห์
(D)The position of the day in the listตำแหน่งของวันใน list
Correct! This position gives both the name and the total of the day.ถูกต้อง! ตำแหน่งนี้ใช้หาทั้งชื่อวันและยอดรวมได้
ข้อ 9
9 / 10
The program shows the name of the day with the highest total. If week_data has this one row, what is the output? week_data = [ [1, 1, 1, 1, 1, 1, 9], ]
ดูเฉลยแล้ว — ไม่นับคะแนน / Answer viewed — not scored
เฉลย / Answerซ่อนเฉลย / Hide
เฉลย / Answer: (A)SundaySunday
คำอธิบายแต่ละตัวเลือก / Explanation for each choice
(A)SundaySunday
Correct! The last value is Sunday, and it is the largest.ถูกต้อง! ค่าสุดท้ายคือวันอาทิตย์ และมีค่ามากที่สุด
(B)MondayMonday
Wrong: Monday sold only 1.ผิด: วันจันทร์ขายได้แค่ 1
(C)SaturdaySaturday
Wrong: this page lists the days from Monday to Sunday, so the last value is Sunday.ผิด: หน้านี้เรียงวันจากจันทร์ถึงอาทิตย์ ค่าสุดท้ายจึงเป็นวันอาทิตย์
(D)99
Wrong: the program shows the name of the day, not the total.ผิด: โปรแกรมแสดงชื่อวัน ไม่ใช่ยอดขาย
ข้อ 10
10 / 10
If Monday has the highest total, what happens to best_day? (day_sum holds the totals of Monday to Sunday at positions 0 to 6.) best_day = 0 for d in range(1, 7): if day_sum[d] > day_sum[best_day]: best_day = dถ้าวันจันทร์มียอดรวมสูงสุด เกิดอะไรกับ best_day? (day_sum เก็บยอดรวมของวันจันทร์ถึงวันอาทิตย์ที่ตำแหน่ง 0 ถึง 6) best_day = 0 for d in range(1, 7): if day_sum[d] > day_sum[best_day]: best_day = d
ดูเฉลยแล้ว — ไม่นับคะแนน / Answer viewed — not scored
เฉลย / Answerซ่อนเฉลย / Hide
เฉลย / Answer: (C)It stays 0 all the wayคงเป็น 0 ตลอด
คำอธิบายแต่ละตัวเลือก / Explanation for each choice
(A)It becomes 6เปลี่ยนเป็น 6
Wrong: the condition is never true, so best_day does not change.ผิด: เงื่อนไขไม่เป็นจริงเลย best_day จึงไม่เปลี่ยน
(B)It becomes 1เปลี่ยนเป็น 1
Wrong: d = 1 does not change best_day unless the condition is true.ผิด: d = 1 ไม่ได้ทำให้ best_day เปลี่ยน ถ้าเงื่อนไขไม่เป็นจริง
(C)It stays 0 all the wayคงเป็น 0 ตลอด
Correct! No day is larger than Monday, so the value never changes.ถูกต้อง! ไม่มีวันไหนมากกว่าวันจันทร์ จึงไม่มีการเปลี่ยนค่า
(D)An error occursเกิด error
Wrong: the program runs normally and shows Monday.ผิด: โปรแกรมทำงานปกติและแสดง Monday