Example 12 — Rotate a Text Picture 90° ตัวอย่างที่ 12 — หมุนภาพตัวอักษร 90°
Use nested loops to read the characters in a new order and turn the picture 90° clockwise. ใช้ loop ซ้อนกันอ่านตัวอักษรใหม่ตามลำดับ เพื่อหมุนภาพตามเข็มนาฬิกา 90°
Read the problem, see how the positions change in the 3×3 picture, then press Run. You can change
picture to a picture of your own, as long as it is a square.
อ่านโจทย์ ดูว่าตำแหน่งเปลี่ยนไปอย่างไรจากภาพ 3×3 แล้วกด Run โดยแก้ picture เป็นภาพของตัวเองได้ ภาพต้องเป็นสี่เหลี่ยมจัตุรัส
picture holds a text picture of N×N characters as a list of strings, one string per row. Show the picture rotated 90° clockwise.
picture เก็บภาพตัวอักษรขนาด N×N เป็น list ของ string แถวละ 1 string จงแสดงภาพที่หมุนตามเข็มนาฬิกา 90°
| c = 0 | c = 1 | c = 2 | |
|---|---|---|---|
| r = 0 | 1 | 2 | 3 |
| r = 1 | 4 | 5 | 6 |
| r = 2 | 7 | 8 | 9 |
| c′ = 0 | c′ = 1 | c′ = 2 | |
|---|---|---|---|
| r′ = 0 | 7 | 4 | 1 |
| r′ = 1 | 8 | 5 | 2 |
| r′ = 2 | 9 | 6 | 3 |
With picture as the rows "123", "456", "789", the result is 741, 852, 963.
เมื่อ picture มีแถวเป็น "123", "456", "789" ภาพผลคือ 741, 852, 963
The first row of the result is the first column of the picture read from bottom to top: 7, 4, 1. Write r′ and c′ for the row and the column of the result, and r and c for the row and the column of the picture the character comes from. Going down a column of the picture moves to the right along a row of the result, so c = r′, and the column of the result counts the rows of the picture from the bottom, so r = size - 1 - c′. The table works this out for every character of the result.
แถวแรกของภาพผลคือคอลัมน์แรกของภาพเดิมอ่านจากล่างขึ้นบน คือ 7, 4, 1 ให้ r′ และ c′ เป็นแถวและคอลัมน์ของภาพผล ส่วน r และ c เป็นแถวและคอลัมน์ของภาพเดิมที่ตัวอักษรนั้นมาจาก การไล่ลงตามคอลัมน์ของภาพเดิมคือการไล่ไปทางขวาตามแถวของภาพผล จึงได้ c = r′ และคอลัมน์ของภาพผลนับแถวของภาพเดิมจากล่างขึ้นบน จึงได้ r = size - 1 - c′ ตารางนี้แสดงการคิดครบทุกตัวอักษรของภาพผล
| New data — the resultข้อมูลใหม่ — ภาพผล | Old data — the pictureข้อมูลเดิม — ภาพเดิม | Equationสมการ | ||||
|---|---|---|---|---|---|---|
| Valueค่า | Row r′แถว r′ | Column c′คอลัมน์ c′ | Row rแถว r | Column cคอลัมน์ c | r | c |
| 7 | 0 | 0 | 2 | 0 | r = size - 1 - c′ | c = r′ |
| 4 | 0 | 1 | 1 | 0 | ||
| 1 | 0 | 2 | 0 | 0 | ||
| 8 | 1 | 0 | 2 | 1 | ||
| 5 | 1 | 1 | 1 | 1 | ||
| 2 | 1 | 2 | 0 | 1 | ||
| 9 | 2 | 0 | 2 | 2 | ||
| 6 | 2 | 1 | 1 | 2 | ||
| 3 | 2 | 2 | 0 | 2 | ||
In the code the loop counters r and c run over the result, so they are r′ and c′ of this table, and the character to take is picture[size - c - 1][r].
ในโค้ด ตัวนับของ loop คือ r และ c ซึ่งไล่ไปตามภาพผล จึงเป็น r′ และ c′ ของตารางนี้ ตัวอักษรที่ต้องหยิบจึงอยู่ที่ picture[size - c - 1][r]
- picture[2][3] is the fourth character of the third row, because picture[2] is a string that can be indexed too.
- len(picture) is the number of rows, which equals the number of columns in a square picture.
- picture[2][3] คือตัวอักษรที่ 4 ของแถวที่ 3 เพราะ picture[2] เป็น string ที่ใช้ index ต่อได้
- len(picture) คือจำนวนแถว ซึ่งเท่ากับจำนวนคอลัมน์ของภาพจัตุรัส
The code keeps two pictures to test with and picks one with case: case = 1 is the 3×3 picture of numbers and case = 2 is the 9×9 smiley face. Change this one value and press Run to see the other picture.
โค้ดเก็บภาพสำหรับทดสอบไว้ 2 ภาพ แล้วเลือกด้วยตัวแปร case โดย case = 1 คือภาพตัวเลขขนาด 3×3 และ case = 2 คือภาพหน้ายิ้มขนาด 9×9 แก้ค่านี้ค่าเดียวแล้วกด Run เพื่อดูอีกภาพหนึ่ง
After the picture is chosen, the program starts by setting the picture in picture and finding its size with len, which it keeps in size. Next it sets r = 0 as the row counter of the result and checks whether r < size. If so, it makes text, an empty string that collects this row, and sets c = 0 as the column counter. It then takes the character at picture[size - c - 1][r], adds it to the end of text and adds 1 to c, repeating until c < size is false, when it leaves the inner loop. It shows the row with print, adds 1 to r and goes back to check r < size again. It repeats this until every row is done, and the program ends.
เมื่อเลือกภาพแล้ว โปรแกรมเริ่มต้นด้วยกำหนดภาพไว้ใน picture แล้วหาขนาดภาพด้วย len เก็บใน size ถัดไปกำหนด r = 0 เป็นตัวนับแถวของภาพผล แล้วตรวจสอบเงื่อนไขว่า r < size หรือไม่ ถ้าใช่ให้สร้าง text เป็น string ว่างสำหรับเก็บแถวนี้ กำหนด c = 0 เป็นตัวนับคอลัมน์ แล้วหยิบตัวอักษรจาก picture[size - c - 1][r] ต่อท้าย text และเพิ่ม c ทีละ 1 ทำแบบนี้จน c < size เป็นเท็จจึงออกจาก loop ด้านใน แล้วแสดงแถวด้วย print และเพิ่ม r ทีละ 1 กลับไปตรวจ r < size อีกครั้ง ทำแบบนี้จนครบทุกแถวแล้วจบการทำงานของโปรแกรม
741 852 963
Change case or picture in card 2 and press Run to check each row of this table.
แก้ case หรือ picture ในการ์ดที่ 2 แล้วกด Run เพื่อตรวจแต่ละแถวของตารางนี้
| picture | Outputผลลัพธ์ |
|---|---|
| "123", "456", "789" (case = 1) | 741, 852, 963 |
| "X" (1×1) | X |
| "ab", "cd" (2×2) | ca, db |
| Smiley face 9×9 (case = 2)หน้ายิ้ม 9×9 (case = 2) | the eyes @ move to the right, the mouth + moves to the leftตา @ ย้ายมาอยู่ด้านขวา ปาก + ย้ายมาอยู่ด้านซ้าย |
Suppose the picture should turn 90° counterclockwise instead. What should the index be?
ถ้าต้องการหมุนทวนเข็มนาฬิกา 90° ต้องแก้ index เป็นอะไร
Show the answer ดูคำตอบ
Use picture[c][size - r - 1]. The first row of the result is now the last column of the picture, read from top to bottom. 123, 456, 789 gives 369, 258, 147.
ใช้ picture[c][size - r - 1] เพราะแถวแรกของภาพผลกลายเป็นคอลัมน์สุดท้ายของภาพเดิม อ่านจากบนลงล่าง 123, 456, 789 จึงได้ 369, 258, 147
369 258 147
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 / แหล่งอ้างอิง
- Python docs — Common Sequence Operations (indexing a list and a string) (การใช้ index กับ list และ string)
-
Python docs — The for statement
(nested loops with
range) (loop ซ้อนกันด้วยrange)