← Back to course
AI Explorers 6-8 / Week 04 / Thursday
4/6
Week 04 · Representation and Reasoning

Thursday

Trees as pseudocode
// From feature tables to decision trees
⏱ about 20 min

Thursday: Trees as Pseudocode

Rocket holds up the paper tree. "This is great for us, but mission control runs on code."

Raven nods. "Then we write the same steps in pseudocode first. Plain words, code shape."

She writes IF at the start of a line, then a test, then THEN and a label.

"The flowchart and the pseudocode are the same algorithm," Nova says. "Just two ways of showing it."

Rocket squints at the lines. "So every branch on the paper becomes an IF or an ELSE?"

"Exactly," Raven says. "Let us trace card E through it and see where it goes."

Same algorithm, two forms

An algorithm is a set of steps for solving a problem.

A flowchart shows the steps as boxes and arrows.

Pseudocode shows the same steps as lines of plain, code-like words.

Programmers often plan complex problems both ways before writing real code.

SKY SORTER, VERSION 1
READ brightness, size, streak
IF streak is yes THEN
    label = "satellite streak"
ELSE IF size >= 8 THEN
    label = "comet"
ELSE IF brightness >= 200 THEN
    label = "planet"
ELSE
    label = "star"
END IF
WRITE label

The sign >= means "is greater than or equal to." So size >= 8 means size 8 or more.

TRACE THE PSEUDOCODE
  • Read the question.
  • Tap your answer.
A new card has brightness 140, size 2, streak no. Which line sets its label?
A card has brightness 250, size 10, streak no. What label?
A card has brightness 100, size 15, streak yes. What label?

Order matters

Pseudocode is checked from the top. The first test that is true decides the label.

If we moved the brightness test to the top, bright comets would be labeled planets.

Changing the order of tests can change the answers, even with the same tests.

PUT SKY SORTER'S TESTS IN THE VERSION 1 ORDER
  • Tap a card.
  • Then tap its spot.
1First test
2Second test
3Third test
4If all are false
StatementTrue or false?
A flowchart and pseudocode can show the same algorithm.?
In this pseudocode, the first true test decides the label.?
Changing the order of the tests can never change a label.?
Pseudocode must be typed into a computer to be useful.?
WHY THIS EXERCISEPlanning in pseudocode helps catch problems before anyone writes real code.
Trace card E (brightness 210, size 3, streak no). What label does version 1 give?
WHY THIS EXERCISEThe pseudocode gives the same answer as the paper tree, because it is the same algorithm.
Card A (brightness 120, size 2, streak no) gets which label?
Card C (brightness 90, size 12, streak no) gets which label?
How many IF or ELSE IF tests are in version 1?
Try it
Write pseudocode for a choice you make every morning, like what to wear.
Use IF, ELSE IF and ELSE. Then trace it for a rainy day and a sunny day.

Excellent tracing! Tomorrow we look for representations and reasoning in everyday life.

← Wednesday