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.
コーディング前に計画を立てる
- アルゴリズムとは、問題を解決するための明確な手順のリストです。
- 良好なプログラマは、コードを入力する前に手順を計画します。
- 計画は早期にミスを発見し、アイデアを説明するのに役立ちます。
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.
分解
- 分解とは、大きな問題を小さな部分に分割することです。
- 各小さな部分を個別に解決します。
- 小さな部分は、記述、テスト、修正が容易です。
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.
抽象化
- 抽象化とは、重要な詳細のみを残すことです。
- 今すぐ必要のない部分を隠蔽します。
- 地図は抽象化です: 道路を示し、すべての木を示しません。
Structured English
- Structured English writes the steps as plain numbered sentences.
- It is not real code, so anyone can read it.
構造化英語
- 構造化英語は、ステップを単純な番号付き文で記述します。
- 実際のコードではないため、誰でも読めます。
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).
フローチャート
- フローチャートは、矢印で結ばれたボックスでステップを描画します。
- 長方形はステップ、菱形は決定(はい / いいえ)を表します。
( 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.
3つの基本要素
- 順次: ステップが一つずつ順番に実行されます。
- 分岐:
if/elif/elseが経路を選びます。 - 反復:
for/whileがステップを繰り返します。
In Cambridge pseudocode
- The same plan in pseudocode, then in Python below.
Cambridge擬似コードにおける表現
- 先に見出しの擬似コード、次に以下の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.
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.
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).
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.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。