Example 6 — Check a Palindrome (debugging) ตัวอย่างที่ 6 — ตรวจคำที่อ่านกลับหลังได้เหมือนเดิม (debugging)

A debugging problem: the program below should tell whether a word reads the same forwards and backwards, but it hides one bug. โจทย์ debugging: โปรแกรมด้านล่างควรบอกว่าคำอ่านจากหน้าไปหลังและหลังไปหน้าได้เหมือนกันหรือไม่ แต่มี bug ซ่อนอยู่ 1 จุด

How to work on this page: read the problem and the flowchart, then check the code against the flowchart box by box. Run the test list, find the wrong line, and fix the program in the code box yourself. Run the test list again until every test gives its expected output, and only then open the solution. To review the kinds of error, the kinds of test case and the bug-hunting tools, see 11 Testing and Debugging.

วิธีทำโจทย์หน้านี้: อ่านโจทย์และ flowchart แล้วตรวจโค้ดเทียบกับ flowchart ทีละกล่อง จากนั้นรันตามรายการทดสอบ หาบรรทัดที่ผิด และแก้โปรแกรมในกล่องโค้ดเอง รันรายการทดสอบซ้ำจนทุกข้อได้ผลตามที่ควรได้ แล้วจึงเปิดดูเฉลย ทบทวนชนิดของ error, ชนิดของ test case และเครื่องมือหา bug ได้ที่บท 11 การทดสอบและการแก้ไขโปรแกรม

1 The problem โจทย์

Read one word that contains only lowercase English letters, then tell whether the word reads the same forwards and backwards. A word like this is called a palindrome. If it does, show <word> reads the same both ways; if it does not, show <word> does not read the same both ways. A word of one letter and an empty word (nothing typed) both count as reading the same, because they have no pair of letters that differ.

รับคำ 1 คำที่มีเฉพาะตัวพิมพ์เล็กภาษาอังกฤษ แล้วบอกว่าคำนั้นอ่านจากหน้าไปหลังและหลังไปหน้าได้เหมือนกันหรือไม่ คำแบบนี้เรียกว่า palindrome ถ้าเหมือนกันให้แสดง <คำ> reads the same both ways ถ้าไม่เหมือนให้แสดง <คำ> does not read the same both ways คำที่มีตัวอักษรตัวเดียวและคำว่าง (ไม่พิมพ์อะไรเลย) นับว่าอ่านได้เหมือนกัน เพราะไม่มีตัวอักษรคู่ไหนที่ต่างกัน

  • Example:ตัวอย่าง: level → level reads the same both ways
  • Example:ตัวอย่าง: robot → robot does not read the same both ways

The program works in two parts: first it builds backward, the word written from the last letter to the first, one letter at a time; then it compares the word with backward letter by letter.

โปรแกรมทำงาน 2 ช่วง ช่วงแรกสร้าง backward คือคำเดิมที่เขียนจากตัวสุดท้ายไปตัวแรก ทีละตัวอักษร ช่วงที่สองเทียบคำเดิมกับ backward ทีละตัวอักษร

New in this program:
  • word[i] is the letter at position i counted from the left, starting at 0.
  • A negative index counts from the end: word[-1] is the last letter and word[-2] the one before it.
  • backward += ... adds a letter to the end of the text in backward.
  • != means “is not equal to”.
  • mirror holds True or False and is used as a flag: it starts as True and changes to False as soon as a pair of letters differs.
คำสั่งใหม่ในโปรแกรมนี้:
  • word[i] คือตัวอักษรตำแหน่งที่ i นับจากซ้าย เริ่มที่ 0
  • ดัชนีติดลบนับจากท้าย word[-1] คือตัวสุดท้าย และ word[-2] คือตัวรองสุดท้าย
  • backward += ... ต่อตัวอักษรเข้าท้ายข้อความใน backward
  • != แปลว่า “ไม่เท่ากับ”
  • mirror เก็บค่า True หรือ False ใช้เป็นตัวแปรธง คือตั้งไว้เป็น True ก่อน แล้วเปลี่ยนเป็น False เมื่อพบตัวอักษรคู่ที่ต่างกัน
2 Flowchart and code (contains a bug) flowchart และโค้ด (มี bug)

The program starts by reading a word into word. Next it creates backward = "" to collect the word written backward, sets i = 0 as the letter position and checks whether i < len(word). If it is, it adds the letter word[-i] to the end of backward, increases i by 1 and checks again. When the condition is false it leaves the loop, sets mirror = True and i = 0, and checks whether i < len(word). If it is, it checks whether word[i] != backward[i]; if that is true it sets mirror = False. Either way it increases i by 1 and goes back to check i < len(word) again. When every letter has been compared it leaves the loop and checks mirror: if it is True it shows that the word reads the same both ways, otherwise it shows that it does not, with print, and the program ends.

โปรแกรมนี้เริ่มต้นด้วยรับคำจากผู้ใช้เก็บใน word ถัดไปสร้างตัวแปร backward = "" สำหรับเก็บคำที่เขียนกลับหลัง กำหนดค่า i = 0 เป็นตัวชี้ตำแหน่งตัวอักษร แล้วตรวจสอบเงื่อนไขว่า i < len(word) หรือไม่ ถ้าใช่ให้ต่อตัวอักษร word[-i] เข้าท้าย backward แล้วเพิ่ม i ขึ้น 1 และกลับไปตรวจสอบอีกครั้ง เมื่อเงื่อนไขเป็นเท็จจึงออกจาก loop หลังจากนั้นกำหนดค่า mirror = True และ i = 0 แล้วตรวจสอบว่า i < len(word) หรือไม่ ถ้าใช่ให้ตรวจสอบเงื่อนไขว่า word[i] != backward[i] หรือไม่ ถ้าใช่ให้กำหนด mirror = False ไม่ว่าทางใดทางหนึ่ง หลังจากนั้นเพิ่ม i ขึ้น 1 แล้วกลับไปตรวจสอบ i < len(word) อีกครั้ง เมื่อเทียบครบทุกตัวอักษรจึงออกจาก loop แล้วตรวจสอบ mirror ถ้าเป็น True ให้แสดงว่าคำนี้อ่านได้เหมือนกันทั้งสองทาง ถ้าไม่ใช่ให้แสดงว่าอ่านไม่เหมือนกัน ด้วย print และจบการทำงานของโปรแกรม

Start Read a word word = input(...) build the word backward Start an empty backward word backward = "" Start at the first letter i = 0 More letters to copy? i < len(word)? No Yes Add one letter to backward backward += word[-i] Move to the next letter i += 1 compare the two words letter by letter Assume it reads the same mirror = True Back to the first letter i = 0 More letters to compare? i < len(word)? Yes No Letters do not match? word[i] != backward[i]? Yes No Mark as not the same mirror = False Move to the next letter i += 1 Still the same? mirror == True? Yes No Show that it reads the same print(word, "reads the same both ways") Show that it does not print(word, "does not read the same both ways") End
The code below follows this flowchart box by box, including its bug. Change the word in the [input] box or use the form under the output, then press Run. โค้ดด้านล่างเขียนตาม flowchart นี้ทุกกล่อง รวมทั้ง bug ด้วย แก้คำในกล่อง [input] หรือใช้ช่องใต้ผลลัพธ์ แล้วกด Run
[main]
[input]
[output]
Type a word: level
level does not read the same both ways

Try it yourself — type a word and press Submit. It replaces the [input] box and runs the code that is in the box right now (including your fix). Press Submit with an empty box to test an empty word.

ลองป้อนค่าเอง — พิมพ์คำแล้วกดส่ง คำจะแทนที่กล่อง [input] แล้วรันโค้ดที่อยู่ในกล่องตอนนี้ (รวมถึงส่วนที่คุณแก้แล้ว) ถ้ากดส่งโดยไม่พิมพ์อะไรจะเป็นการทดสอบคำว่าง

3 Test list รายการทดสอบ

Run each input yourself (type it into the form in card 2) and compare the result with the expected output column. The last column shows what the code with the bug gives.

รันทีละข้อด้วยตัวเอง (พิมพ์ลงในช่องของการ์ดที่ 2) แล้วเทียบผลกับคอลัมน์ผลที่ควรได้ คอลัมน์สุดท้ายคือผลจากโค้ดที่ยังมี bug

Inputข้อมูลที่ป้อน Kindชนิด Expected outputผลที่ควรได้ Output of the buggy codeผลจากโค้ดที่มี bug
level Normal level reads the same both ways level does not read the same both ways ✗
racecar Normal racecar reads the same both ways racecar does not read the same both ways ✗
noon Normal (even number of letters)Normal (จำนวนตัวอักษรเป็นเลขคู่) noon reads the same both ways noon does not read the same both ways ✗
robot Normal robot does not read the same both ways robot does not read the same both ways ✓
ab Boundary (2 letters)Boundary (2 ตัวอักษร) ab does not read the same both ways ab reads the same both ways ✗
x Boundary (1 letter)Boundary (ตัวอักษรเดียว) x reads the same both ways x reads the same both ways ✓
(empty)(ว่าง) Boundary (no letters)Boundary (ไม่มีตัวอักษร)  reads the same both ways (starts with a space: the empty word, then the space that print puts between its values) (ขึ้นต้นด้วยช่องว่าง: คำว่าง ตามด้วยช่องว่างที่ print ใส่คั่นระหว่างค่า)  reads the same both ways ✓
Anna Error (has a capital letter)Error (มีตัวพิมพ์ใหญ่) should be rejected, but this program does not check its input; after the fix it gives Anna does not read the same both ways ควรถูกปฏิเสธ แต่โปรแกรมนี้ไม่ได้ตรวจข้อมูลที่รับเข้ามา หลังแก้แล้วจะได้ Anna does not read the same both ways Anna does not read the same both ways
4 Your tasks ภารกิจ
  1. Check the code against the flowchart box by box, and note anything that looks suspicious.
  2. Run every input in the test list and note whether the actual output matches the expected output.
  3. Why does robot give the right answer even though the program has a bug? And why does level, which should pass, fail, while ab, which should fail, passes?
  4. Which kind of error is this bug, and why?
  5. Find the wrong line:
    • add print(backward) after the first loop, to see whether backward is already wrong when it is built, or
    • add print(i, -i, word[-i], backward) inside the first loop,
    then fill in the trace table below for the word level.
  6. Fix the code in the box and run the test list again. Every Normal and Boundary input must give its expected output.
  7. Think further: Anna breaks the rule of the problem, yet the program does not reject it. Where would you add a check? (Example 2 in 11 Testing and Debugging shows one way.)
  1. ตรวจโค้ดเทียบกับ flowchart ทีละกล่อง แล้วจดจุดที่สงสัย
  2. รันทุกข้อในรายการทดสอบ แล้วจดผลที่ได้จริงว่าตรงกับผลที่ควรได้หรือไม่
  3. ทำไม robot ได้ผลถูกทั้งที่โปรแกรมมี bug และทำไม level ที่ควรผ่านกลับไม่ผ่าน แต่ ab ที่ไม่ควรผ่านกลับผ่าน
  4. bug นี้เป็น error ชนิดไหน เพราะอะไร
  5. หาบรรทัดที่ผิด
    • ใส่ print(backward) หลัง loop แรก เพื่อดูว่า backward ผิดตั้งแต่ช่วงสร้างคำหรือไม่ หรือ
    • ใส่ print(i, -i, word[-i], backward) ใน loop แรก
    แล้วกรอก trace table ด้านล่างของคำว่า level
  6. แก้โค้ดในกล่อง แล้วรันรายการทดสอบซ้ำ ข้อที่เป็น Normal และ Boundary ต้องได้ผลตามตารางทุกข้อ
  7. ชวนคิดต่อ: Anna ผิดเงื่อนไขของโจทย์ แต่โปรแกรมไม่ปฏิเสธ ควรเพิ่มการตรวจตรงไหน (ตัวอย่างที่ 2 ในบท 11 การทดสอบและการแก้ไขโปรแกรม มีวิธีหนึ่งให้ดู)
Trace table: word = "level" (first loop) trace table: word = "level" (loop แรก)
i-iword[-i]backward
0   
1   
2   
3   
4   
5 Solution เฉลย
Show the fixed code ดูโค้ดที่แก้แล้ว

The bug is on the line backward += word[-i]. Because i starts at 0, -i is -0, which is the same as 0, so the first round takes the first letter instead of the last one, and backward is wrong from its very first letter. Change it to backward += word[-(i + 1)]. It is a logic error: the program runs to the end with no error message. The compare loop and if mirror are already correct; they give a wrong answer only because they receive a wrong backward from the first part — the part that shows the symptom is not always the part that holds the bug.

bug อยู่ที่บรรทัด backward += word[-i] เพราะ i เริ่มที่ 0 ทำให้ -i คือ -0 ซึ่งเท่ากับ 0 รอบแรกจึงหยิบตัวแรกแทนตัวสุดท้าย และ backward ผิดตั้งแต่ตัวอักษรแรก แก้เป็น backward += word[-(i + 1)] เป็น logic error เพราะโปรแกรมรันจบโดยไม่มีข้อความ error ส่วน loop ที่เทียบคำและ if mirror ถูกต้องอยู่แล้ว แต่ได้คำตอบผิดเพราะรับ backward ที่ผิดมาจากช่วงแรก ช่วงที่แสดงอาการจึงไม่จำเป็นต้องเป็นช่วงที่มี bug

Why some tests hide the bug: robot gives backward = "rtobo", which differs from the word anyway, so the answer is right by luck. x and the empty word have no pair of letters that can differ. ab gives backward = "ab" (first word[0], then word[-1]), which is exactly the word itself, so it wrongly passes.

ทำไม test บางข้อซ่อน bug: robot ได้ backward = "rtobo" ซึ่งต่างจากคำเดิมอยู่แล้ว คำตอบจึงถูกโดยบังเอิญ ส่วน x และคำว่างไม่มีตัวอักษรคู่ไหนที่ต่างกัน และ ab ได้ backward = "ab" (ตัวแรกคือ word[0] แล้วตามด้วย word[-1]) ซึ่งตรงกับคำเดิมพอดี จึงผ่านทั้งที่ไม่ควรผ่าน

Trace table of the buggy code: word = "level" (first loop) trace table ของโค้ดที่มี bug: word = "level" (loop แรก)
i-iword[-i]backward
00'l'"l"
1-1'l'"ll"
2-2'e'"lle"
3-3'v'"llev"
4-4'e'"lleve"
Then the compare loop of the buggy code: word = "level", backward = "lleve" ต่อด้วย loop ที่เทียบคำของโค้ดที่มี bug: word = "level", backward = "lleve"
iword[i]backward[i]mirror at the end of the roundmirror หลังจบรอบ
0'l''l'True
1'e''l'False
2'v''e'False
3'e''v'False
4'l''e'False

After the fix, -(i + 1) runs through -1, -2, -3, -4, -5, so backward grows as l, le, lev, leve, level, and mirror stays True in every round.

หลังแก้แล้ว -(i + 1) มีค่า -1, -2, -3, -4, -5 ตามลำดับ backward จึงเพิ่มทีละตัวเป็น l, le, lev, leve, level และ mirror เป็น True ทุกรอบ

[fixed]
[input]
[output]
Type a word: level
level reads the same both ways

The Run buttons on this page simulate 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 References / แหล่งอ้างอิง

  1. Python docs — Common Sequence Operations (indexing a string, including negative indexes counted from the end) (การใช้ดัชนีกับสตริง รวมถึงดัชนีติดลบที่นับจากท้าย)
  2. Python docs — Comparisons (!=, “not equal”) (!= “ไม่เท่ากับ”)
  3. Python docs — Boolean Type (True and False) (True และ False)
  4. Kinds of error, kinds of test case and bug-hunting tools: 11 Testing and Debugging; flowchart symbols: 01 Basic → Flowchart Basic. ชนิดของ error, ชนิดของ test case และเครื่องมือหา bug: 11 การทดสอบและการแก้ไขโปรแกรม ; สัญลักษณ์ของ flowchart: 01 Basic → Flowchart Basic

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

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

If word = "robot", what is word[-1]?ถ้า word = "robot" แล้ว word[-1] คืออะไร?

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

เฉลย / Answer: (C) 't''t'

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

  • (A) 'r''r'
    Wrong: 'r' is the first letter, given by word[0].ผิด: 'r' คือตัวแรก ได้จาก word[0]
  • (B) 'o''o'
    Wrong: the 'o' near the end is the second to last letter, given by word[-2].ผิด: 'o' ตัวที่อยู่ใกล้ท้ายคือตัวรองสุดท้าย ได้จาก word[-2]
  • (C) 't''t'
    Correct! A negative index counts from the end, and [-1] is the last letter.ถูกต้อง! ดัชนีติดลบนับจากท้าย [-1] คือตัวสุดท้าย
  • (D) It raises IndexErrorเกิด IndexError
    Wrong: -1 is inside the range of a 5-letter word, so there is no error.ผิด: -1 อยู่ในช่วงของคำที่มี 5 ตัวอักษร จึงไม่เกิด error

ข้อ 2

2 / 10

If word = "robot", what is word[-0]?ถ้า word = "robot" แล้ว word[-0] คืออะไร?

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

เฉลย / Answer: (A) 'r', the same as word[0]'r' เหมือน word[0]

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

  • (A) 'r', the same as word[0]'r' เหมือน word[0]
    Correct! -0 equals 0, so it gives the first letter, not the last one.ถูกต้อง! -0 เท่ากับ 0 จึงได้ตัวแรก ไม่ใช่ตัวสุดท้าย
  • (B) 't', the last letter't' ตัวสุดท้าย
    Wrong: the last letter is word[-1]; -0 equals 0.ผิด: ตัวสุดท้ายคือ word[-1] ส่วน -0 เท่ากับ 0
  • (C) An empty text ''ข้อความว่าง ''
    Wrong: a single index always gives one letter when it is inside the range.ผิด: การใช้ดัชนี 1 ตัวคืนตัวอักษร 1 ตัวเสมอเมื่ออยู่ในช่วง
  • (D) It raises IndexErrorเกิด IndexError
    Wrong: -0 is 0, which is inside the range, so there is no error.ผิด: -0 คือ 0 ซึ่งอยู่ในช่วง จึงไม่เกิด error

ข้อ 3

3 / 10

According to the problem, which word should give reads the same both ways?ตามโจทย์ คำไหนควรได้ผลว่า reads the same both ways?

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

เฉลย / Answer: (D) noonnoon

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

  • (A) abab
    Wrong: ab backward is ba, which is different (even though the buggy code says it is the same).ผิด: ab อ่านกลับหลังได้ ba ไม่เหมือนเดิม (แม้โค้ดที่มี bug จะบอกว่าเหมือน)
  • (B) robotrobot
    Wrong: robot backward is tobor.ผิด: robot อ่านกลับหลังได้ tobor
  • (C) AnnaAnna
    Wrong: Anna has a capital letter, which breaks the rule of the problem, and 'A' is not equal to 'a'.ผิด: Anna มีตัวพิมพ์ใหญ่ ผิดเงื่อนไขของโจทย์ และ 'A' ไม่เท่ากับ 'a'
  • (D) noonnoon
    Correct! noon backward is still noon.ถูกต้อง! noon อ่านกลับหลังก็ยังเป็น noon

ข้อ 4

4 / 10

Which kind of error is the bug in this program?bug ในโปรแกรมนี้เป็น error ชนิดใด?

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

เฉลย / Answer: (B) A logic errorlogic error

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

  • (A) A syntax errorsyntax error
    Wrong: the code follows the grammar of Python, so it starts running.ผิด: โค้ดเขียนถูกไวยากรณ์ จึงเริ่มรันได้
  • (B) A logic errorlogic error
    Correct! The program runs to the end with no error message, but the answer is wrong.ถูกต้อง! โปรแกรมรันจบโดยไม่มีข้อความ error แต่คำตอบผิด
  • (C) A run-time errorrun-time error
    Wrong: the program does not stop halfway and shows no error message.ผิด: โปรแกรมไม่หยุดกลางทาง และไม่มีข้อความ error
  • (D) There is no error, because some tests passไม่มี error เพราะบาง test ผ่าน
    Wrong: passing some tests does not mean the program is correct; level still gets a wrong answer.ผิด: ผ่านบาง test ไม่ได้แปลว่าโปรแกรมถูก level ยังได้คำตอบผิด

ข้อ 5

5 / 10

Which test passes even though the code still has the bug?test ข้อไหนผ่านทั้งที่โค้ดยังมี bug?

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

เฉลย / Answer: (D) robotrobot

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

  • (A) levellevel
    Wrong: the buggy code says level does not read the same.ผิด: โค้ดที่มี bug บอกว่า level อ่านไม่เหมือนกัน
  • (B) abab
    Wrong: the buggy code says ab reads the same, which is wrong.ผิด: โค้ดที่มี bug บอกว่า ab อ่านได้เหมือนกัน ซึ่งผิด
  • (C) noonnoon
    Wrong: the buggy code says noon does not read the same.ผิด: โค้ดที่มี bug บอกว่า noon อ่านไม่เหมือนกัน
  • (D) robotrobot
    Correct! backward becomes "rtobo", which differs from the word anyway, so the answer is right by luck.ถูกต้อง! backward ได้ "rtobo" ซึ่งต่างจากคำเดิมอยู่แล้ว คำตอบจึงถูกโดยบังเอิญ

ข้อ 6

6 / 10

Why does the buggy code say that ab reads the same both ways?ทำไมโค้ดที่มี bug บอกว่า ab อ่านได้เหมือนกัน?

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

เฉลย / Answer: (A) backward becomes "ab", which is exactly the word itselfbackward ได้ "ab" ซึ่งตรงกับคำเดิมพอดี

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

  • (A) backward becomes "ab", which is exactly the word itselfbackward ได้ "ab" ซึ่งตรงกับคำเดิมพอดี
    Correct! The first round takes word[0], which is 'a', and the second takes word[-1], which is 'b'.ถูกต้อง! รอบแรกหยิบ word[0] คือ 'a' รอบสองหยิบ word[-1] คือ 'b'
  • (B) The compare loop never runsloop ที่เทียบคำไม่ได้ทำงานเลย
    Wrong: len(word) is 2, so the loop runs twice.ผิด: len(word) เป็น 2 loop จึงทำงาน 2 รอบ
  • (C) if mirror checks the wrong way roundif mirror ตรวจผิดทาง
    Wrong: if mirror is correct; the problem is the value of backward.ผิด: if mirror ถูกต้องแล้ว ปัญหาอยู่ที่ค่า backward
  • (D) != does not work on letters!= ใช้กับตัวอักษรไม่ได้
    Wrong: != can compare letters; for example, 'a' != 'b' is True.ผิด: != เทียบตัวอักษรได้ เช่น 'a' != 'b' เป็น True

ข้อ 7

7 / 10

Which line fixes the bug correctly?บรรทัดใดแก้ bug ได้ถูกต้อง?

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

เฉลย / Answer: (C) backward += word[-(i + 1)]backward += word[-(i + 1)]

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

  • (A) backward += word[i]backward += word[i]
    Wrong: this copies the word in its normal order, so every word would be said to read the same.ผิด: จะได้คำเดิมเรียงตามปกติ ทุกคำจึงถูกบอกว่าอ่านได้เหมือนกัน
  • (B) backward += word[-i - 2]backward += word[-i - 2]
    Wrong: the first round would take the second to last letter, and the last round would go past the start of the word.ผิด: รอบแรกจะหยิบตัวรองสุดท้าย และรอบสุดท้ายจะเลยขอบคำ
  • (C) backward += word[-(i + 1)]backward += word[-(i + 1)]
    Correct! In the first round -(0 + 1) is -1, so it starts from the last letter.ถูกต้อง! รอบแรก -(0 + 1) คือ -1 จึงเริ่มจากตัวสุดท้าย
  • (D) if word[i] == backward[i]:if word[i] == backward[i]:
    Wrong: the compare loop is already correct; changing it to == would set False when the letters match.ผิด: loop ที่เทียบคำถูกต้องอยู่แล้ว การเปลี่ยนเป็น == ทำให้ตั้ง False เมื่อตัวอักษรตรงกัน

ข้อ 8

8 / 10

In the buggy code, if you type level, what is backward after the first loop?ในโค้ดที่มี bug ถ้าป้อน level แล้ว backward หลัง loop แรกคืออะไร?

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

เฉลย / Answer: (B) "lleve""lleve"

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

  • (A) "level""level"
    Wrong: this is the value after the bug is fixed.ผิด: นี่คือค่าหลังแก้ bug แล้ว
  • (B) "lleve""lleve"
    Correct! The indexes are 0, -1, -2, -3, -4, which give l, l, e, v, e.ถูกต้อง! ดัชนีคือ 0, -1, -2, -3, -4 จึงได้ l, l, e, v, e
  • (C) "evel""evel"
    Wrong: the loop runs 5 times, so backward has 5 letters.ผิด: loop ทำงาน 5 รอบ backward จึงมี 5 ตัวอักษร
  • (D) """"
    Wrong: every round adds one letter.ผิด: ทุกรอบต่อตัวอักษรเข้าไป 1 ตัว

ข้อ 9

9 / 10

Why is mirror = True set before the compare loop?ทำไมตั้ง mirror = True ไว้ก่อนเข้า loop ที่เทียบคำ?

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

เฉลย / Answer: (A) To assume the word reads the same until a pair of letters differsถือว่าอ่านได้เหมือนกันไว้ก่อน จนกว่าจะพบตัวอักษรคู่ที่ต่างกัน

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

  • (A) To assume the word reads the same until a pair of letters differsถือว่าอ่านได้เหมือนกันไว้ก่อน จนกว่าจะพบตัวอักษรคู่ที่ต่างกัน
    Correct! The flag changes to False only when a pair of letters differs.ถูกต้อง! ตัวแปรธงเปลี่ยนเป็น False เมื่อพบคู่ที่ต่างกันเท่านั้น
  • (B) To make the loop run every roundเพื่อให้ loop ทำงานครบทุกรอบ
    Wrong: the number of rounds depends on range(len(word)), not on mirror.ผิด: จำนวนรอบขึ้นกับ range(len(word)) ไม่ใช่ mirror
  • (C) Because Python requires a variable to start as Trueเพราะ Python บังคับให้ตัวแปรเริ่มเป็น True
    Wrong: Python has no such rule; we choose the starting value to suit the problem.ผิด: Python ไม่มีกฎแบบนี้ เราเลือกค่าเริ่มต้นเองตามโจทย์
  • (D) So that an empty word gives "does not read the same"เพื่อให้คำว่างได้ผลว่าอ่านไม่เหมือนกัน
    Wrong: with no letters, mirror stays True, so an empty word reads the same, as the problem says.ผิด: เมื่อไม่มีตัวอักษร mirror ยังเป็น True คำว่างจึงได้ผลว่าอ่านได้เหมือนกันตามโจทย์

ข้อ 10

10 / 10

Why should Anna be rejected?ทำไม Anna ควรถูกปฏิเสธ?

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

เฉลย / Answer: (D) Because the problem accepts lowercase letters onlyเพราะโจทย์รับเฉพาะตัวพิมพ์เล็ก

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

  • (A) Because it is too longเพราะยาวเกินไป
    Wrong: the problem does not limit the length of the word.ผิด: โจทย์ไม่ได้จำกัดความยาวของคำ
  • (B) Because it has an even number of lettersเพราะมีจำนวนตัวอักษรเป็นเลขคู่
    Wrong: words with an even number of letters, such as noon, are allowed.ผิด: คำที่มีจำนวนตัวอักษรเป็นเลขคู่ เช่น noon ก็ใช้ได้
  • (C) Because the program would stop with an error messageเพราะโปรแกรมจะหยุดด้วยข้อความ error
    Wrong: the program runs to the end and shows Anna does not read the same both ways.ผิด: โปรแกรมรันจบและแสดง Anna does not read the same both ways
  • (D) Because the problem accepts lowercase letters onlyเพราะโจทย์รับเฉพาะตัวพิมพ์เล็ก
    Correct! Anna has a capital letter, so it is an error test case, but this program does not check for it.ถูกต้อง! Anna มีตัวพิมพ์ใหญ่ จึงเป็น error test case แต่โปรแกรมนี้ไม่ได้ตรวจ