Computational Thinking 🧩
แนวคิดเชิงคำนวณ
Four ways of thinking that turn a big, messy problem into clear steps a person or a computer can follow. Every example, activity and exercise below comes from the IPST textbook (book pp. 1–23).
🎯 Learning objectives จุดประสงค์ของบทเรียน
By the end of this chapter you can:
- ✅ Explain the computational-thinking process: decomposition, pattern recognition, abstraction and algorithm design.
- ✅ Design an algorithm that solves a problem using computational thinking.
In this chapter: 1.1 Decomposition · 1.2 Pattern recognition · 1.3 Abstraction · 1.4 Algorithm design · 1.5 Case studies
🏫 Starting problem
In daily life you meet situations that are complicated and hard to solve. If you split the complicated problem into smaller ones, it gets easier to understand and to plan a solution.
For example: your classroom is messy, with things scattered everywhere. You want to turn it into an activity room and tell your friends how to help, so that everyone works at the same time and the job is finished quickly.
- One friend picks up paper and paper planes.
- One stacks the books back on the shelves.
- One straightens the desks and chairs into rows.
- One collects the pencils and puts away the props.
📖 Review before you start ทบทวนความรู้ก่อนเรียน
Tick (✓) every statement that is correct.
Describe the way from your home to school, so clearly that a younger student could follow it later without getting lost.
- Leave the front gate and turn left onto the soi.
- Walk to the end of the soi (about 200 m) until you reach the main road.
- Cross at the zebra crossing in front of the 7-Eleven.
- Take the songthaew or bus going towards the market; get off at the second stop.
- The school gate is on the right, next to the temple.
Notice what you did: you split the trip into small steps (decomposition), left out details that do not help (the colour of the houses: abstraction) and put the steps in order (an algorithm).
💡 What is computational thinking?
Computational thinking is a way of analysing a problem so that the answer comes out as steps that a person or a computer can carry out correctly and precisely. Those steps are called an algorithm.
It matters because it helps you solve problems, explain your ideas to other people clearly, and it is the foundation for writing computer programs.
Computational thinking has 4 key components:
| Component | Thai | What it means |
|---|---|---|
| Decomposition | การแบ่งปัญหาใหญ่เป็นปัญหาย่อย | Breaking a complicated problem into smaller, less complicated sub-problems so it is easier to analyse and solve. |
| Pattern recognition | การพิจารณารูปแบบ | Looking for things that are the same or similar between the sub-problems, or between this problem and one that has already been solved. |
| Abstraction | การคิดเชิงนามธรรม | Separating the details that matter from the ones that do not, and replacing many small, detailed steps with one single step. |
| Algorithm design | การออกแบบอัลกอริทึม | Building the solution as steps that a person or a computer can follow to solve the problem. |
🃏 Key-term flashcards Tap to flip
1.1 Decomposition การแบ่งปัญหาใหญ่เป็นปัญหาย่อย
Complicated problems are hard to solve. Splitting a big problem into small sub-problems lowers the complexity, lets you look at every detail properly, and makes it easier to design the steps for each sub-problem.
Try this: you describe a picture, and a friend has to draw it without seeing it.
Draw a village like the one below (it does not have to be identical). One grid square is 20 units and the bottom-left corner of the grid is (0, 0). Then tell a friend how to draw it, as close to your picture as possible, without showing it to them.
Describing the picture so a friend can draw it splits into these sub-problems:
- How many houses are in the picture?
- How do you draw the first house, and where is it?
- How do you draw the second house, and where is it?
- How do you draw the third house, and where is it?
This problem is fairly easy and clear, because it has fixed rules and a fixed result.
Another everyday problem: two-digit addition
You can add two 2-digit numbers easily, but a kindergarten child may only be able to add 1-digit numbers. How would you teach them? Split the big problem:
- How do you add the ones digits?
- How do you add the tens digits (plus anything carried)?
Help break the job of calculating the total price of all the items into sub-problems.
- What does the soap cost? (quantity of soap × price of one soap)
- What does the shampoo cost? … and the same for the books, toothbrushes, glasses, rulers, scissors type 1 and scissors type 2.
- Add the cost of every item together to get the total.
1.2 Pattern recognition การพิจารณารูปแบบ
Some problems split into sub-problems that have the same or a similar pattern. You can reuse the way you solved one sub-problem on the others, so there are fewer steps to design.
For the picture in Example 1.1 you could answer the sub-problems like this:
Sub-problem 1 How many houses are in the picture?
Answer 3 houses.
Sub-problem 2 How do you draw the first house, and where?
Answer Draw the body as a yellow square with sides of 100 units, with its bottom-left corner at (0, 0). On top of the square draw the roof: a purple equilateral triangle with sides of 100 units.
Sub-problem 3 How do you draw the second house, and where?
Answer Draw the body as a red square with sides of 50 units, bottom-left corner at (120, 90). On top draw the roof: a grey equilateral triangle with sides of 50 units.
Sub-problem 4 How do you draw the third house, and where?
Answer Draw the body as a green square with sides of 80 units, bottom-left corner at (200, 10). On top draw the roof: a sky-blue equilateral triangle with sides of 80 units.
That description is complete, and a friend who follows it will draw something close to the original. But we can make it shorter by spotting the pattern: all three houses are built from the same parts (a body, a roof, a bottom-left corner and a size) and only the colours and values change (Fig 1.2).
Look at the information needed for the calculation. Click the words that appear again in every line, then press Check.
1.3 Abstraction การคิดเชิงนามธรรม
A problem has many details: some are needed to solve it and some are not. Abstraction means filtering out the details you do not need, so the whole problem is easier to understand, analyse and solve.
Abstraction also includes hiding detail: replacing a group of fiddly steps with one single step, so the solution can be explained more briefly.
From Example 1.2, every house has the same pattern: a body and a roof in different colours. Hiding detail lets us describe each house like this:
| House | Described with full detail | Described with detail hidden |
|---|---|---|
| 1 | Draw the body as a yellow square, size 100 units per side, bottom-left corner at (0, 0); on top draw the roof as a purple equilateral triangle, 100 units per side. | House 1: size 100, yellow body, purple roof, at (0, 0). |
| 2 | Draw the body as a red square, 50 units per side, bottom-left corner at (120, 90); on top draw the roof as a grey equilateral triangle, 50 units per side. | House 2: size 50, red body, grey roof, at (120, 90). |
| 3 | Draw the body as a green square, 80 units per side, bottom-left corner at (200, 10); on top draw the roof as a sky-blue equilateral triangle, 80 units per side. | House 3: size 80, green body, sky-blue roof, at (200, 10). |
When you hide the detail you do not need to say that the body is a square, that the roof is an equilateral triangle, or that the position means the bottom-left corner. Those are true of every house. You only give what is special about each house: its size, its colours and where it is.
Write down what is the same in every line from Part 2, and underline the parts that are different.
quantity of item × price of item
Everything is the same except the name of the item. So the whole calculation can be described as one hidden-detail step: for each item, multiply its quantity by its price.
1.4 Algorithm design การออกแบบอัลกอริทึม
An algorithm is a list of instructions describing the steps that solve a problem. Each instruction must be something another person can follow without any confusion. Algorithms are usually written as pseudocode (รหัสลำลอง) or a flowchart (ผังงาน).
When a computer is the tool, the algorithm must first be turned into a programming language so the computer can follow it. So how much detail you design into an algorithm depends on who will carry it out, a person or a computer.
Main steps
- Draw a house of size 100 units at (0, 0)
- Draw a house of size 50 units at (120, 90)
- Draw a house of size 80 units at (200, 10)
Sub-steps · drawing a house of size s at (x, y)
- Draw a square with sides of s units, its bottom-left corner at (x, y)
- Draw an equilateral triangle with sides of s units on top of the square
A computer cannot “just draw a square”. Each sub-step has to be spelled out as pen movements.
Main steps
- Draw a house of size 100 units at (0, 0)
- Draw a house of size 50 units at (120, 90)
- Draw a house of size 80 units at (200, 10)
Sub-step 1 · draw a square of size s
- Repeat the following 4 times
- Move forward s units
- Turn left 90°
Sub-step 2 · draw an equilateral triangle of size s
- Repeat the following 3 times
- Move forward s units
- Turn left 120°
Sub-step 3 · draw a house of size s at (x, y)
- Lift the pen
- Move to (x, y)
- Put the pen down
- Face right
- Draw a square of size s (sub-step 1)
- Turn left 90°
- Move forward s units
- Turn right 90°
- Draw an equilateral triangle of size s (sub-step 2)
🐢 Run Example 1.5: the computer draws the village
Press Step to carry out one instruction at a time, or Run to watch the whole algorithm. Change the sizes and positions to design your own village. The algorithm stays the same; only the values change.
| House | Colours | Size s | x | y |
|---|
What is the algorithm for calculating the total price of all the items?
- total ← 0
- Repeat for every item in the basket
- cost ← quantity of the item × price of the item
- total ← total + cost
- Show total
Practice with fresh prices: follow the algorithm and work out the total.
- Draw a village on a grid (1 square = 20 units, bottom-left corner = (0, 0)) without letting your friend see it.
- Write an algorithm for drawing your picture and give it to your friend to draw.
- Check the result your friend drew.
Use the turtle lab above to draw and test your own village. Then swap roles: below, you are the friend. Read the village and write the hidden-detail algorithm (size and position of each house).
1.5 Case studies กรณีศึกษา
The examples below use computational thinking to solve problems. A problem might not need every component (it depends on the problem), but every problem must end with an algorithm that is correct, fast and efficient.
📚 Example 1.6 · Teaching a younger child to sort books
You want to teach a younger child to arrange books in order of height, so the shelf looks neat and books are easy to find. You need steps the child can follow easily, however many books there are and whatever order they start in.
Decomposition
Sorting a whole big pile at once is hard. Splitting it into sub-problems makes it systematic:
Sub-problem 1 Which book should go first?
Answer The tallest book.
Sub-problem 2 From the books left, which one goes second?
Answer The tallest of the books left.
Sub-problem 3 From the books left, which one goes third?
Answer The tallest of the books left.
⋮ … down to the last sub-problem, a pile of size 1.
Taking the tallest book out of a pile of n books leaves a pile of n − 1 books, and sorting that is the same problem, just smaller.
Pattern recognition
Every sub-problem has the same pattern and looks for the same kind of answer:
Abstraction
The steps only need to put the books in order from tallest to shortest, so the only detail needed to choose a book is its height. Colour and thickness are unnecessary details and can be cut out.
If we represent each book by one number (its height), the sub-problem
is solved in exactly the same way as
Algorithm for teaching a child to sort books
Main steps
- Repeat the following until there are no books left in the pile
- Choose the tallest book in the pile
- Put the book from step 1.1 on the table, next to the row of books already placed. If the row is empty, this book goes first.
📚 Watch the algorithm sort a pile
Each number is a book’s height (abstraction!). Step through: the algorithm scans the pile, picks the tallest, and moves it to the shelf.
Sorting algorithms. The book-sorting algorithm is a sorting algorithm called selection sort. It is easy to understand but fairly slow when there are many things to sort. There are many other sorting algorithms. See them animated at toptal.com/developers/sorting-algorithms.
💃 Example 1.7 · Dancing to the rhythm
Find the pattern in this step-dance so your friends can follow it with as few mistakes as possible. Give the steps in a way that is short and easy to remember.
The whole dance is long and hard to remember. Decomposition splits it into groups, and a repeating pattern appears:
Split into groups of four beats, there are only 4 different moves: “R R L L”, “R L R L”, “L R L R” and “L L R R”. That is still hard to remember, so abstraction (hiding detail) gives each move a name:
| Name | Thai | Means |
|---|---|---|
| March right | มาร์ชขวา | R R L L |
| March left | มาร์ชซ้าย | L L R R |
| Shuffle right-left | ขยับขวาซ้าย | R L R L |
| Shuffle left-right | ขยับซ้ายขวา | L R L R |
The algorithm
- Let “march right” mean “R R L L”
- Let “march left” mean “L L R R”
- Let “shuffle right-left” mean “R L R L”
- Let “shuffle left-right” mean “L R L R”
- Dance “march right”, 2 times
- Dance “shuffle right-left” once then “shuffle left-right” once, and repeat that 2 times
- Dance “march left”, 2 times
Practice: here is a new dance. Split it into groups of four and name each group.
🐟 Example 1.8 · The Valentine koi pond
Kru Lita had workers dig a heart-shaped koi pond in the school garden. The pond is d metres deep and each straight side is w metres. Kru Lita wants to know how much water is needed to fill it. Design an algorithm to calculate the volume of water.
This example shows that you do not always have to use the four components in order, and some can happen at the same time. This problem starts with abstraction to see what matters, then uses decomposition together with pattern recognition, uses abstraction again to find the information each sub-problem needs, and finally arranges everything into an algorithm.
Abstraction: analysing the pond
The problem only asks about the pond’s volume, so the size of the lawn and the number of fish are unnecessary. The only thing that matters is the volume of the pond.
Decomposition and pattern recognition
- How do you calculate the pond’s volume?
- Volume = area of the pond × depth of the pond.
- How do you find the pond’s area?
- The pond is a heart, which is made of basic shapes.
- A heart is made of one square and two half-circles (two half-circles have the same area as one circle).
- How do you calculate the area of a square?
- How do you calculate the area of a half-circle?
From this analysis, the sub-problems that lead to the answer are:
Sub-problem 1 How do you calculate the area of a half-circle?
Sub-problem 2 How do you calculate the area of the square?
Sub-problem 3 How do you combine the circle and square areas into the area of the heart?
Sub-problem 4 How do you turn the heart’s area into the volume of the heart-shaped pond?
Abstraction: what information is needed?
- To calculate the pond’s volume you need the area of the heart and the depth, which is d metres.
- To calculate the heart’s area you need the area of the square and the area of both half-circles.
- For the square you need its side length, which is w metres.
- For the circle you need its radius, which is w/2 metres.
Algorithm design
- Let w be the side length of the heart-shaped pond
- Let d be the depth of the pond
- radius ← w / 2
- circle area ← π × radius²
- square area ← w²
- heart area ← circle area + square area
- pond volume ← heart area × d
- Show the pond volume
Practice:
Write an algorithm for something you are interested in, then tell a friend to follow it. For example: folding clothes, or choosing what to wear in different weather (hot, cold, rainy…).
- Check the weather forecast for today
- If it is raining: wear long trousers and closed shoes, and take a raincoat or umbrella
- Otherwise, if the temperature is below 20 °C: wear long sleeves and add a jacket
- Otherwise (hot): wear a light short-sleeved shirt and take a water bottle
- If going to school: put on the school uniform instead of step 2–4 clothes, and only add the rain or cold extras
- Check yourself in the mirror
Check it with the four components: the decision is split by weather (decomposition), the same “if… wear…” pattern repeats (pattern recognition), and colours and brands are ignored (abstraction).
📋 Chapter summary สรุปท้ายบท
Computational thinking is a way of analysing a problem so that the answer comes out as steps, called an algorithm, which can be passed on to another person or a computer to carry out correctly and precisely.
It has 4 components: decomposition, pattern recognition, abstraction and algorithm design. When solving a problem you do not have to use them in order, or use all of them. For some problems you may work on several components at the same time.
💻 End-of-chapter activity กิจกรรมท้ายบท
At the village New Year party you are given the job of washing a lot of plates. Every plate is the same size. When they are washed they must be packed into boxes. Each box holds 3 stacks of 10 plates. Write an algorithm for washing the plates and packing them into boxes.
- Take an empty box; stack count ← 0; plates in this stack ← 0
- Repeat until there are no dirty plates left
- Wash one plate, rinse it and dry it
- Put it on the current stack; plates in this stack ← plates in this stack + 1
- If plates in this stack = 10: put the stack in the box; stack count ← stack count + 1; plates in this stack ← 0
- If stack count = 3: close the box, take a new empty box; stack count ← 0
- Put any unfinished stack into the last box and close it
Decomposition: wash → stack → box. Pattern: every plate is handled the same way. Abstraction: all plates are the same size, so only the count matters.
Practice:
📝 End-of-chapter exercises แบบฝึกหัดท้ายบท
1 · Make the song shorter
Communicate these song lyrics more concisely:
“If we’re feeling good, clap our hands. If we’re feeling good, clap our hands. If we’re happy, let go of every sorrow, why waste any time, clap our hands. If we’re feeling good, laugh out loud. If we’re feeling good, laugh out loud. If we’re happy, let go of every sorrow, why waste any time, laugh out loud. If we’re feeling good, stomp our feet. If we’re feeling good, stomp our feet. If we’re happy, let go of every sorrow, why waste any time, stomp our feet.”
(Thai lyrics: หากพวกเรากำลังสบาย จงปรบมือพลัน …)
- Let the verse with action be:
“If we’re feeling good, action” × 2, then “If we’re happy, let go of every sorrow, why waste any time, action” - Sing the verse with action = “clap our hands”
- Sing the verse with action = “laugh out loud”
- Sing the verse with action = “stomp our feet”
Pattern recognition finds the repeated verse; abstraction hides it behind one name, with only the action changing (just like the house of size s at (x, y)).
2 · Laying turf around the pond
Mr Mobai wants to lay grass turf in his rectangular back garden. In the middle he has dug a circular pond for koi. The rest is covered with turf, and one piece of turf is 30 × 30 cm. How many pieces of turf does he need? (Garden w × h, pond diameter d.)
- garden area ← w × h
- radius ← d / 2
- pond area ← π × radius²
- lawn area ← garden area − pond area
- turf area ← 0.3 × 0.3 = 0.09 m²
- pieces ← lawn area ÷ turf area, rounded up to a whole piece
- Show pieces
Practice:
3 · Draw the robot
Design an algorithm to draw this robot face, both as one a person can follow and as one a computer can follow.
- Draw a square head of size 160 with its bottom-left corner at (40, 20)
- Draw a 30 × 40 rectangle ear on each side of the head, at (10, 100) and (200, 100)
- Draw two diamond eyes of side 35, at (90, 120) and (160, 120)
- Draw a 110 × 24 rectangle mouth at (65, 50)
Sub-step A · rectangle of width a and height b at (x, y)
- Lift the pen, move to (x, y), put the pen down, face right
- Repeat 2 times
- Move forward a; turn left 90°
- Move forward b; turn left 90°
Sub-step B · diamond of side s with its bottom corner at (x, y)
- Lift the pen, move to (x, y), put the pen down, turn to face 45°
- Repeat 4 times
- Move forward s
- Turn left 90°
Main steps
- Rectangle 160 × 160 at (40, 20) · head
- Rectangle 30 × 40 at (10, 100) · left ear
- Rectangle 30 × 40 at (200, 100) · right ear
- Diamond of side 35 at (90, 120) · left eye
- Diamond of side 35 at (160, 120) · right eye
- Rectangle 110 × 24 at (65, 50) · mouth
🤖 Run the robot algorithm
🧠 Mini-quiz
Source: IPST (สสวท.) Basic Science and Technology textbook, Technology (Computing Science), Grade 8 (M.2), Chapter 1. Figures are from the textbook; text translated and adapted by Guru Jazzy.