Example 7 — Check a Palindrome with One Loop ตัวอย่างที่ 7 — ตรวจคำที่อ่านกลับหลังได้เหมือนเดิมด้วย loop เดียว

The same problem as Example 6, solved with one loop and no extra backward word. โจทย์เดียวกับตัวอย่างที่ 6 แต่เขียนด้วย loop เดียว และไม่ต้องสร้างคำกลับหลังเพิ่ม

This page solves the problem of Example 6 again, with a shorter program. Read the problem, follow the flowchart and run the code, then see in card 3 why this program is better than the one in Example 6.

หน้านี้แก้โจทย์ของตัวอย่างที่ 6อีกครั้งด้วยโปรแกรมที่สั้นกว่า อ่านโจทย์ ไล่ตาม flowchart และรันโค้ด แล้วดูในการ์ดที่ 3 ว่าโปรแกรมนี้ดีกว่าโปรแกรมในตัวอย่างที่ 6 อย่างไร

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
Commands used 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.
  • word[-(i + 1)] is the letter at position i counted from the end: when i = 0 it is word[-1], the last letter; when i = 1 it is word[-2], and so on.
  • != 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] คือตัวรองสุดท้าย
  • word[-(i + 1)] คือตัวอักษรตำแหน่งที่ i นับจากท้าย เมื่อ i = 0 คือ word[-1] ตัวสุดท้าย เมื่อ i = 1 คือ word[-2] และต่อไปเรื่อยๆ
  • != แปลว่า “ไม่เท่ากับ”
  • mirror เก็บค่า True หรือ False ใช้เป็นตัวแปรธง คือตั้งไว้เป็น True ก่อน แล้วเปลี่ยนเป็น False เมื่อพบตัวอักษรคู่ที่ต่างกัน
2 Main program — flowchart and code โปรแกรมหลัก — flowchart และโค้ด

The program starts by reading a word into word. Next it sets mirror = True as a flag that assumes the word reads the same, sets i = 0 as the letter position, and checks whether i < len(word). If it is, it checks whether word[i] != word[-(i + 1)], that is, whether the letter at position i from the left differs from the letter at position i from the end; if that is true it sets mirror = False. Either way it increases i by 1 and goes back to check i < len(word) again. This repeats until the condition is false; then it leaves the loop and checks whether mirror == True. If it is, it shows that the word reads the same both ways, otherwise it shows that it does not, with print, and the program ends.

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

Start Read a word word = input(...) compare each letter with its partner from the end Assume it reads the same mirror = True Start at the first letter i = 0 More letters to compare? i < len(word)? Yes No Letters do not match? word[i] != word[-(i + 1)]? 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. Change the word in the [input] box or use the form under the output, then press Run. โค้ดด้านล่างเขียนตาม flowchart นี้ทุกกล่อง แก้คำในกล่อง [input] หรือใช้ช่องใต้ผลลัพธ์ แล้วกด Run
[main]
[input]
[output]
Type a word: level
level reads 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. Press Submit with an empty box to test an empty word.

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

Results of the program ผลของโปรแกรม
Inputข้อมูลที่ป้อน Outputผลลัพธ์
levellevel reads the same both ways
racecarracecar reads the same both ways
noonnoon reads the same both ways
robotrobot does not read the same both ways
abab does not read the same both ways
xx reads the same both ways
(empty)(ว่าง)  reads the same both ways (starts with a space: the empty word, then the space that print puts between its values) (ขึ้นต้นด้วยช่องว่าง: คำว่าง ตามด้วยช่องว่างที่ print ใส่คั่นระหว่างค่า)
3 Why this program is better than Example 6 โปรแกรมนี้ดีกว่าตัวอย่างที่ 6 อย่างไร

Example 6 (after its bug is fixed) works in two parts: the first loop builds backward letter by letter, and the second loop compares word[i] with backward[i]. But the letter at position i of backward is simply word[-(i + 1)], so there is no need to build backward first: this program reads each pair of letters straight from word.

ตัวอย่างที่ 6 (หลังแก้ bug) ทำงาน 2 ช่วง loop แรกสร้าง backward ทีละตัวอักษร และ loop ที่สองเทียบ word[i] กับ backward[i] แต่ตัวอักษรตำแหน่งที่ i ของ backward ก็คือ word[-(i + 1)] อยู่แล้ว จึงไม่ต้องสร้าง backward ก่อน โปรแกรมนี้อ่านตัวอักษรแต่ละคู่จาก word ได้เลย

  Example 6 (fixed)ตัวอย่างที่ 6 (หลังแก้ bug) Example 7ตัวอย่างที่ 7
Number of loopsจำนวน loop 2 (build backward, then compare)2 (สร้าง backward แล้วเทียบ) 1
Extra variableตัวแปรเพิ่ม keeps a whole backward copy of the word in backwardเก็บคำกลับหลังทั้งคำไว้ใน backward noneไม่มี
Rounds for a 5-letter word (level)จำนวนรอบเมื่อคำยาว 5 ตัว (level) 10 (5 to add letters + 5 to compare)10 (ต่อตัวอักษร 5 + เทียบ 5) 5
Lines in the Process partจำนวนบรรทัดในส่วน Process 7 4

Run the code below to see each pair that the loop compares. It is the program of card 2 with one print added inside the loop. For level every pair matches, so mirror stays True. Change the input to robot: the first pair, r and t, already differs, so mirror is False after round 0, yet the loop still runs all 5 rounds.

รันโค้ดด้านล่างเพื่อดูตัวอักษรแต่ละคู่ที่ loop เทียบ โค้ดนี้คือโปรแกรมในการ์ดที่ 2 ที่เพิ่ม print 1 บรรทัดใน loop สำหรับ level ทุกคู่ตรงกัน mirror จึงเป็น True ตลอด ลองเปลี่ยน input เป็น robot คู่แรก r กับ t ก็ต่างกันแล้ว mirror จึงเป็น False ตั้งแต่รอบที่ 0 แต่ loop ยังทำงานต่อจนครบ 5 รอบ

[trace]
[input]
[output]
Type a word: level
0 -1 l l True
1 -2 e e True
2 -3 v v True
3 -4 e e True
4 -5 l l True
mirror = True

Think further: in the output for level, round 0 compares l with l, and round 4 compares the same two letters again. Every pair is checked twice. How could the loop check each pair only once, and how many rounds would a 5-letter word need then?

ชวนคิดต่อ: ในผลของ level รอบที่ 0 เทียบ l กับ l และรอบที่ 4 ก็เทียบตัวอักษร 2 ตัวนี้ซ้ำอีกครั้ง ทุกคู่จึงถูกเทียบ 2 ครั้ง จะเขียน loop ให้เทียบแต่ละคู่เพียงครั้งเดียวได้อย่างไร และคำยาว 5 ตัวจะเหลือกี่รอบ

Show the answer ดูคำตอบ

Round i compares position i from the left with position i from the end, so once i passes the middle of the word the loop only repeats pairs it has already checked. For a word of N letters it is enough to check i = 0 up to i = (N - 1) // 2. When N is odd, the last of these rounds compares the middle letter with itself, which can never differ, so that round can be dropped too: i = 0 up to N // 2 - 1, which is range(len(word) // 2).

รอบที่ i เทียบตำแหน่งที่ i นับจากซ้ายกับตำแหน่งที่ i นับจากท้าย เมื่อ i เลยกึ่งกลางคำไปแล้ว loop จะเทียบซ้ำแต่คู่ที่ตรวจไปแล้ว สำหรับคำยาว N ตัว จึงตรวจแค่ i = 0 ถึง i = (N - 1) // 2 ก็พอ และเมื่อ N เป็นเลขคี่ รอบสุดท้ายของช่วงนี้คือการเทียบตัวกลางกับตัวเอง ซึ่งไม่มีทางต่างกัน จึงตัดรอบนี้ออกได้อีก เหลือ i = 0 ถึง N // 2 - 1 ซึ่งก็คือ range(len(word) // 2)

Looploop Rounds for level (N = 5)จำนวนรอบของ level (N = 5) Rounds for noon (N = 4)จำนวนรอบของ noon (N = 4)
range(len(word)) (card 2)(การ์ดที่ 2)54
range((len(word) - 1) // 2 + 1)32
range(len(word) // 2)22

// is integer division: it divides and drops the part after the decimal point, for example 5 // 2 is 2. Use //, not /: 5 / 2 is 2.5, and range accepts only whole numbers, so the program would stop with a TypeError.

// คือการหารปัดเศษทิ้ง เช่น 5 // 2 ได้ 2 ต้องใช้ // ไม่ใช่ / เพราะ 5 / 2 ได้ 2.5 และ range รับเฉพาะจำนวนเต็ม โปรแกรมจะหยุดด้วย TypeError

Run the half-word loop below. For level it checks only 2 pairs and still gives the same answer; try noon, robot and x too.

รัน loop แบบครึ่งคำด้านล่าง สำหรับ level ตรวจแค่ 2 คู่ก็ได้คำตอบเหมือนเดิม ลองป้อน noon, robot และ x ด้วย

[half]
[input]
[output]
Type a word: level
0 -1 l l True
1 -2 e e True
mirror = True

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 (!= and ==) (!= และ ==)
  3. Python docs — Numeric Types (//, integer division, used in the answer of “Think further”) (// การหารปัดเศษทิ้ง ใช้ในคำตอบของ “ชวนคิดต่อ”)
  4. Python docs — Boolean Type (True and False) (True และ False)
  5. The two-loop version of this program: Example 6 — Check a Palindrome (debugging); flowchart symbols: 01 Basic → Flowchart Basic. โปรแกรมแบบ 2 loop ของโจทย์นี้: ตัวอย่างที่ 6 — ตรวจคำที่อ่านกลับหลังได้เหมือนเดิม (debugging) ; สัญลักษณ์ของ flowchart: 01 Basic → Flowchart Basic

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

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

When i = 0, which letter of word is word[-(i + 1)]?เมื่อ i = 0 แล้ว word[-(i + 1)] คือตัวอักษรตัวไหนของ word?

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

เฉลย / Answer: (B) The last letterตัวสุดท้าย

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

  • (A) The first letterตัวแรก
    Wrong: the first letter is word[0], while -(0 + 1) is -1.ผิด: ตัวแรกคือ word[0] ส่วน -(0 + 1) คือ -1
  • (B) The last letterตัวสุดท้าย
    Correct! -(0 + 1) is -1, and word[-1] is the last letter.ถูกต้อง! -(0 + 1) คือ -1 และ word[-1] คือตัวสุดท้าย
  • (C) The second to last letterตัวรองสุดท้าย
    Wrong: the second to last letter is word[-2], which comes when i = 1.ผิด: ตัวรองสุดท้ายคือ word[-2] ได้เมื่อ i = 1
  • (D) It raises IndexErrorเกิด IndexError
    Wrong: -1 is inside the range of any word with at least 1 letter.ผิด: -1 อยู่ในช่วงของคำที่มีอย่างน้อย 1 ตัวอักษร

ข้อ 2

2 / 10

If word = "level", which pair of letters does the program compare in round i = 1?ถ้า word = "level" รอบ i = 1 โปรแกรมเทียบตัวอักษรคู่ไหน?

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

เฉลย / Answer: (C) 'e' and 'e''e' กับ 'e'

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

  • (A) 'l' and 'l''l' กับ 'l'
    Wrong: this pair is compared in rounds i = 0 and i = 4.ผิด: คู่นี้เทียบในรอบ i = 0 และ i = 4
  • (B) 'e' and 'l''e' กับ 'l'
    Wrong: word[-2] of level is 'e', not 'l'.ผิด: word[-2] ของ level คือ 'e' ไม่ใช่ 'l'
  • (C) 'e' and 'e''e' กับ 'e'
    Correct! word[1] is 'e' and word[-2] is 'e'.ถูกต้อง! word[1] คือ 'e' และ word[-2] คือ 'e'
  • (D) 'v' and 'v''v' กับ 'v'
    Wrong: the middle letter 'v' is compared with itself in round i = 2.ผิด: ตัวกลาง 'v' ถูกเทียบกับตัวเองในรอบ i = 2

ข้อ 3

3 / 10

How many loops does the program in Example 7 use?โปรแกรมในตัวอย่างที่ 7 ใช้ loop กี่ชุด?

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

เฉลย / Answer: (A) 11 ชุด

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

  • (A) 11 ชุด
    Correct! One loop compares each letter directly with its partner from the end.ถูกต้อง! loop เดียวเทียบตัวอักษรกับคู่ของมันจากท้ายคำโดยตรง
  • (B) 22 ชุด
    Wrong: two loops is the method of Example 6, which builds backward first.ผิด: 2 loop คือวิธีของตัวอย่างที่ 6 ที่ต้องสร้าง backward ก่อน
  • (C) 33 ชุด
    Wrong: the program has only one for line.ผิด: โปรแกรมมี for เพียงบรรทัดเดียว
  • (D) No loopไม่มี loop
    Wrong: for i in range(len(word)) is a loop.ผิด: for i in range(len(word)) คือ loop

ข้อ 4

4 / 10

Why does Example 7 not need the variable backward?ทำไมตัวอย่างที่ 7 ไม่ต้องมีตัวแปร backward?

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

เฉลย / Answer: (D) Because word[-(i + 1)] is already the letter at position i of the backward wordเพราะ word[-(i + 1)] คือตัวอักษรตำแหน่งที่ i ของคำกลับหลังอยู่แล้ว

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

  • (A) Because Python reverses the word by itselfเพราะ Python กลับคำให้เองอัตโนมัติ
    Wrong: the program never reverses the word; it reads letters from the end with a negative index.ผิด: โปรแกรมไม่ได้กลับคำเลย แต่อ่านตัวอักษรจากท้ายด้วยดัชนีติดลบ
  • (B) Because it compares only the first and the last letterเพราะเทียบแค่ตัวแรกกับตัวสุดท้าย
    Wrong: the loop compares every position, not only the first pair.ผิด: loop เทียบครบทุกตำแหน่ง ไม่ใช่แค่คู่แรก
  • (C) Because mirror stores the backward word insteadเพราะ mirror เก็บคำกลับหลังไว้แทน
    Wrong: mirror holds only True or False.ผิด: mirror เก็บแค่ True หรือ False
  • (D) Because word[-(i + 1)] is already the letter at position i of the backward wordเพราะ word[-(i + 1)] คือตัวอักษรตำแหน่งที่ i ของคำกลับหลังอยู่แล้ว
    Correct! The letter can be read straight from the end of the word, so there is no need to build a backward word first.ถูกต้อง! อ่านตัวอักษรจากท้ายคำเดิมได้ตรงๆ จึงไม่ต้องสร้างคำกลับหลังก่อน

ข้อ 5

5 / 10

For a 5-letter word, how many rounds does the loop in Example 7 run?ถ้าคำยาว 5 ตัวอักษร loop ในตัวอย่างที่ 7 ทำงานกี่รอบ?

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

เฉลย / Answer: (C) 5 rounds5 รอบ

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

  • (A) 1 round1 รอบ
    Wrong: the loop does not stop when a pair differs; it runs len(word) rounds.ผิด: loop ไม่หยุดเมื่อพบคู่ที่ต่างกัน ทำงานจนครบ len(word) รอบ
  • (B) 10 rounds10 รอบ
    Wrong: 10 rounds is Example 6 (5 to add letters + 5 to compare).ผิด: 10 รอบคือตัวอย่างที่ 6 (ต่อตัวอักษร 5 + เทียบ 5)
  • (C) 5 rounds5 รอบ
    Correct! range(5) gives i from 0 to 4.ถูกต้อง! range(5) ให้ i เป็น 0 ถึง 4
  • (D) 4 rounds4 รอบ
    Wrong: range(5) has 5 values: 0, 1, 2, 3, 4.ผิด: range(5) มี 5 ค่า คือ 0, 1, 2, 3, 4

ข้อ 6

6 / 10

If you type ab, what does the program show?ถ้าป้อน ab โปรแกรมจะแสดงอะไร?

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

เฉลย / Answer: (A) ab does not read the same both waysab does not read the same both ways

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

  • (A) ab does not read the same both waysab does not read the same both ways
    Correct! The first round compares 'a' with 'b', which differ.ถูกต้อง! รอบแรกเทียบ 'a' กับ 'b' ซึ่งต่างกัน
  • (B) ab reads the same both waysab reads the same both ways
    Wrong: ab backward is ba, which is different.ผิด: ab อ่านกลับหลังได้ ba ไม่เหมือนเดิม
  • (C) baba
    Wrong: this program does not show the backward word.ผิด: โปรแกรมนี้ไม่ได้แสดงคำกลับหลัง
  • (D) It raises IndexErrorเกิด IndexError
    Wrong: the indexes -1 and -2 are inside the range of a 2-letter word.ผิด: ดัชนี -1 และ -2 อยู่ในช่วงของคำยาว 2 ตัว

ข้อ 7

7 / 10

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

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

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

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

  • (A) 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
  • (B) 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 ไม่มีกฎแบบนี้ เราเลือกค่าเริ่มต้นเองตามโจทย์
  • (C) 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 คำว่างจึงได้ผลว่าอ่านได้เหมือนกันตามโจทย์
  • (D) 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 เมื่อพบคู่ที่ต่างกันเท่านั้น

ข้อ 8

8 / 10

If you type nothing (an empty word), what does the program show?ถ้าไม่พิมพ์อะไรเลย (คำว่าง) โปรแกรมจะแสดงอะไร?

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

เฉลย / Answer: (B) reads the same both ways (starting with a space) reads the same both ways (ขึ้นต้นด้วยช่องว่าง)

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

  • (A) does not read the same both waysdoes not read the same both ways
    Wrong: the loop never runs, so mirror is still True.ผิด: loop ไม่ทำงานเลย mirror จึงยังเป็น True
  • (B) reads the same both ways (starting with a space) reads the same both ways (ขึ้นต้นด้วยช่องว่าง)
    Correct! print shows the empty word, then the space between its values, then the text.ถูกต้อง! print แสดงคำว่าง ตามด้วยช่องว่างที่คั่นระหว่างค่า แล้วจึงเป็นข้อความ
  • (C) IndexErrorIndexError
    Wrong: range(0) has no rounds, so no index is used at all.ผิด: range(0) ไม่มีรอบ จึงไม่มีการใช้ดัชนีเลย
  • (D) Nothing at allไม่แสดงอะไรเลย
    Wrong: the Output part always runs after the loop.ผิด: ส่วน Output ทำงานเสมอหลัง loop

ข้อ 9

9 / 10

If you type robot, what is mirror at the end of round i = 0?ถ้าป้อน robot หลังจบรอบ i = 0 ค่า mirror เป็นอะไร?

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

เฉลย / Answer: (A) FalseFalse

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

  • (A) FalseFalse
    Correct! The first round compares 'r' with 't', which differ.ถูกต้อง! รอบแรกเทียบ 'r' กับ 't' ซึ่งต่างกัน
  • (B) TrueTrue
    Wrong: 'r' is not equal to 't', so mirror = False is set in the first round.ผิด: 'r' ไม่เท่ากับ 't' จึงตั้ง mirror = False ตั้งแต่รอบแรก
  • (C) 'r''r'
    Wrong: mirror holds only True or False.ผิด: mirror เก็บได้แค่ True หรือ False
  • (D) It has no value yetยังไม่มีค่า
    Wrong: mirror is set to True before the loop.ผิด: mirror ถูกตั้งเป็น True ก่อนเข้า loop แล้ว

ข้อ 10

10 / 10

When i = 4, what is -(i + 1)?เมื่อ i = 4 แล้ว -(i + 1) มีค่าเท่าไร?

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

เฉลย / Answer: (C) -5-5

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

  • (A) -4-4
    Wrong: -4 is -i, without adding 1 first.ผิด: -4 คือ -i ไม่ได้บวก 1 ก่อน
  • (B) 55
    Wrong: the minus sign in front of the brackets makes the result negative.ผิด: เครื่องหมายลบหน้าวงเล็บทำให้ผลเป็นลบ
  • (C) -5-5
    Correct! 4 + 1 is 5, and the minus sign makes it -5.ถูกต้อง! 4 + 1 ได้ 5 แล้วเปลี่ยนเป็นลบ ได้ -5
  • (D) -3-3
    Wrong: -(i + 1) adds 1; it does not subtract 1.ผิด: -(i + 1) บวก 1 ไม่ใช่ลบ 1