Planning & pseudocode
วางแผนก่อนเขียนโค้ด
- อัลกอริทึม คือรายการขั้นตอนที่ชัดเจนเพื่อแก้ปัญหา
- Good programmers plan the steps before they type code.
- แผนช่วยให้คุณตรวจหาข้อผิดพลาดได้เร็วขึ้นและอธิบายไอเดียของคุณ
การแยกส่วน (Decomposition)
- Decomposition หมายความว่าแบ่งปัญหาใหญ่ออกเป็นส่วนย่อยๆ
- แก้ไขแต่ละส่วนย่อยอย่างอิสระ
- ส่วนย่อยเขียน ทดสอบ และแก้ไขได้ง่ายกว่า
การห่อหุ้ม (Abstraction)
- Abstraction หมายความว่าเก็บไว้เฉพาะรายละเอียดที่สำคัญเท่านั้น
- คุณซ่อนส่วนที่คุณไม่จำเป็นต้องใช้ตอนนี้
- แผนที่เป็นการ h่อหุ้ม: มันแสดงถนน, ไม่ใช่ต้นไม้ทุกต้น
ภาษาอังกฤษเชิงโครงสร้าง (Structured English)
- Structured English เขียนขั้นตอนเป็นประโยค deleteUsering เรียบง่าย
- มันไม่ใช่โค้ดจริง ดังนั้นใครก็ตามจึงอ่านมันได้
1. Read the number.
2. If it can be divided by 2 with no remainder, it is even.
3. Otherwise it is odd.
4. Output the answer.
Flowcharts
- Flowchart วาดขั้นตอนเป็นกล่องเชื่อมต่อกันด้วยลูกศร
- รูปสี่เหลี่ยมผืนผ้าแสดงขั้นตอน; รูปเพชรแสดงการตัดสินใจ (ใช่ / ไม่)
โครงสร้างพื้นฐานสามอย่าง
- ลำดับ (Sequence): ขั้นตอนทำงานต่อเนื่องกันทีละขั้นตอน
- การเลือก (Selection):
if/elif/elseเลือกเส้นทางที่จะทำ - การทำซ้ำ (Iteration):
for/whileทำขั้นตอนเดิมซ้ำหลายครั้ง
In Cambridge pseudocode
- แผนเดียวกันใน pseudocode, แล้วใน Python ด้านล่าง. Exam arrays usually run from 1, and
Countholds how many numbers there are
Total ← 0
FOR I ← 1 TO Count
Total ← Total + Numbers[I]
NEXT I
Average ← Total / Count
OUTPUT Average
numbers = [4, 8, 6]
total = 0
for n in numbers:
total = total + n
average = total / len(numbers)
print(average)
ข้อผิดพลาดที่พบบ่อย
- Pseudocode คือแผนงานที่เขียนเป็นคำพูดธรรมดา — ไม่ต้องรันได้จริง
- แยกปัญหาออกเป็นขั้นตอนย่อยก่อนเริ่มเขียนโค้ด
- วางแผนจาก Input → Process → Output
ลองดูเลย
- แต่ละโจทย์จะมอบแผนงานเป็นข้อความมา ให้แปลงเป็น Python
- กด Check answer เพื่อทดสอบโค้ดของคุณ
An algorithm is ordered steps
Pseudocode is just the steps in order, before you write real code.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Follow this plan: read a whole number; if it divides by 2 with no remainder print Even, otherwise print Odd. For input 4, print Even.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Follow this plan: read a number N; add up every whole number from 1 ไป N; print the total. For input 5, print 15.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Decompose this into a function. Write count_vowels(word) that ผลตอบแทน how many letters of word are vowels (a e i o u, lowercase).
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Follow this plan for every number from 1 ไป 15: if it divides by both 3 และ 5, print FizzBuzz; else if it divides by 3, print Fizz; else if it divides by 5, print Buzz; otherwise print the number.
คลิก Run เพื่อดูผลลัพธ์ที่นี่