Back

Pseudocode Challenge: Walking a List

divider

Objective

Five problems on traversal — going through a list one element at a time. This is the single most common shape in the multiple-choice section, and the exam gives you two ways to do it that look interchangeable and are not.

Do this on paper. There is nothing to run and nothing to type. On exam day you get this notation on a printed sheet and no computer, so practicing it any other way practices the wrong thing.


Reference Card

Everything you need for this page. This is the same notation the College Board uses on the exam.

Exam Notation — what you have so far
a ← expression assign a copy of the result to a
DISPLAY(expression) show the value, FOLLOWED BY A SPACE
IF(condition) no ELSE IF exists - nest an IF inside an ELSE
{ ... }
ELSE
{ ... }
REPEAT n TIMES no counter variable is provided
{ ... }
REPEAT UNTIL(condition) checked BEFORE each pass; can run zero times
{ ... }
aList ← [v1, v2, v3] FIRST ELEMENT IS aList[1]
aList ← bList assigns a COPY of bList
LENGTH(aList) number of elements
APPEND(aList, value) add to the end, length + 1
INSERT(aList, i, value) shift right from i, place value at i
REMOVE(aList, i) delete index i, shift left, length - 1
FOR EACH item IN aList item takes each VALUE, first to last
{ ... } THERE IS NO INDEX. If you need one, you make it.
An index below 1 or above LENGTH ends the program with an error.

FOR EACH gives you the value and nothing else. No position, no index, no way to look at the element before this one. When a question needs any of that, it uses REPEAT with a counter instead — and now you know why.


The Problems


Problem 1 — trace

What does this display, and on how many lines?

Problem 1
scores ← [7, 3, 9]
FOR EACH s IN scores
{
DISPLAY(s)
}

Problem 2 — trace

Same list walked the other way. Track total and i in two columns. What does it display?

Problem 2
values ← [4, 8, 15]
total ← 0
i ← 1
REPEAT LENGTH(values) TIMES
{
total ← total + values[i]
i ← i + 1
}
DISPLAY(total)

Then answer this: i is set to 1 on its own line above the loop, and increased on the last line inside it. Say what would happen if the increase were missing, and say why the exam makes you write those two lines when Lua's numeric for writes them for you.


Problem 3 — trace

What does this display?

Problem 3
items ← [5, 10, 15, 20]
count ← 0
FOR EACH v IN items
{
IF(v > 8)
{
count ← count + 1
}
}
DISPLAY(count)

A decision inside a traversal, counting how many elements pass a test, is the most-reused shape on the exam. It is worth being able to read it in one pass.


Problem 4 — translate

Rewrite this Lua loop in exam notation.

Problem 4
local temps = {60, 75, 90}
for i, t in ipairs(temps) do
print(t)
end

The Lua version declares two names and uses one. Say which one vanishes in the translation, and what you would have to do differently if the body had used it.


Problem 5 — Lua only

There is no exam-notation version of this one, and that is the point. Write down exactly what it prints.

Problem 5
local letters = {"a", nil, "c"}
for i, v in ipairs(letters) do
print(v)
end
print(#letters)

Both answers surprise people. Say what ipairs does when it meets the gap, and what #letters reports.

An exam list cannot have a hole in it, so the reference sheet never has to answer this question. A Lua table can, which is why a traversal that quietly stops early is a Lua bug with no exam equivalent — and one you can only find by counting what came out.

Submit

Your five answers, plus the short explanations from problems 2, 4 and 5. For the traces, write the output exactly as it appears, including spacing.

Worth 3 points, graded on completion. Show your working for the traces — a wrong answer with visible reasoning is worth more to both of us than a right one with none.

Commence Challenge