Planning & pseudocode · 规划与伪代码
Plan before you code
- An algorithm is a clear list of steps that solves a problem.
- Good programmers plan the steps before they type code.
- A plan helps you spot mistakes early and explain your idea.
写代码前先规划
- 算法(algorithm)是解决问题的一串清晰步骤。
- 优秀的程序员会在敲代码之前先规划好步骤。
- 规划能帮你及早发现错误,并把思路讲清楚。
Decomposition
- Decomposition means breaking a big problem into smaller parts.
- Solve each small part on its own.
- Small parts are easier to write, test, and fix.
分解
- 分解(decomposition)就是把一个大问题拆成更小的部分。
- 每个小部分单独解决。
- 小部分更容易编写、测试和修改。
Abstraction
- Abstraction means keeping only the details that matter.
- You hide the parts you do not need right now.
- A map is an abstraction: it shows roads, not every tree.
抽象
- 抽象(abstraction)就是只保留重要的细节。
- 你把暂时不需要的部分隐藏起来。
- 地图就是一种抽象:它显示道路,而不是每一棵树。
Structured English
- Structured English writes the steps as plain numbered sentences.
- It is not real code, so anyone can read it.
结构化英语
- 结构化英语(structured English)用平实的编号句子来写步骤。
- 它不是真正的代码,所以任何人都能读懂。
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
- A flowchart draws the steps as boxes joined by arrows.
- A rectangle is a step; a diamond is a decision (yes / no).
流程图
- 流程图(flowchart)把步骤画成用箭头连接的方框。
- 矩形表示一个步骤;菱形表示一个判断(是 / 否)。
( start )
|
[ read n ]
|
< n > 0 ? > --no--> [ output "not positive" ]
| yes
[ output "positive" ]
|
( end )
Three building blocks
- Sequence: steps run one after another.
- Selection:
if/elif/elsechooses a path. - Iteration:
for/whilerepeats steps.
三种基本结构
- 顺序(sequence):步骤一个接一个地执行。
- 选择(selection):
if/elif/else选择一条路径。 - 迭代(iteration):
for/while重复执行步骤。
In Cambridge pseudocode
- The same plan in pseudocode, then in Python below.
用剑桥伪代码表示
- 同一个规划,先用伪代码,再用下面的 Python。
Total ← 0
FOR I ← 0 TO LENGTH(Numbers) - 1
Total ← Total + Numbers[I]
NEXT I
Average ← Total / LENGTH(Numbers)
OUTPUT Average
numbers = [4, 8, 6]
total = 0
for n in numbers:
total = total + n
average = total / len(numbers)
print(average)
Common mistakes
- Pseudocode is a plan in plain words — it does not have to run.
- Break the problem into small steps before you write any code.
- Plan the input → process → output.
常见错误
- 伪代码是用平白语言写的计划——不必能运行。
- 动手写代码前,先把问题拆成小步骤。
- 规划输入 → 处理 → 输出。
Now you try
- Each task gives you a plan in words. Turn it into Python.
- Press Check answer to test your code.
现在轮到你
- 每个任务都用文字给出一个规划。把它变成 Python。
- 按检查答案来测试你的代码。
An algorithm is ordered steps · 算法就是有序的步骤
Pseudocode is just the steps in order, before you write real code. · 伪代码就是按顺序排列的步骤,在写真正代码之前先想清楚。
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. · 按这个规划做:读取一个整数;如果它能被 2 整除(没有余数)就打印 Even,否则打印 Odd。当输入为 4 时,打印 Even。
Click Run to see the output here. · 点击“运行”查看此处输出。
Follow this plan: read a number N; add up every whole number from 1 to · 到 N; print the total. For input 5, print 15. · 按这个规划做:读取一个数 N;把从 1 到 N 的每个整数加起来;打印总和。当输入为 5 时,打印 15。
Click Run to see the output here. · 点击“运行”查看此处输出。
Decompose this into a function. Write count_vowels(word) that returns · 返回值 how many letters of word are vowels (a e i o u, lowercase). · 把它分解成一个函数。编写 count_vowels(word),返回 word 中有多少个字母是元音(小写的 a e i o u)。
Click Run to see the output here. · 点击“运行”查看此处输出。
Follow this plan for every number from 1 to · 到 15: if it divides by both 3 and 5, print FizzBuzz; else if it divides by 3, print Fizz; else if it divides by 5, print Buzz; otherwise print the number. · 对 1 到 15 的每个数按这个计划做:如果能同时被 3 和 5 整除,打印 FizzBuzz;否则如果能被 3 整除,打印 Fizz;否则如果能被 5 整除,打印 Buzz;否则打印这个数。
Click Run to see the output here. · 点击“运行”查看此处输出。