Back

Pseudocode Challenge: Lists

divider

Objective

Four problems on lists. One is a translation, and two of them are the two places Lua tables and exam lists genuinely disagree — the ones from session 19. Both are on the Quarter 1 exam.

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
{ ... }
An index below 1 or above LENGTH ends the program with an error.

The good news first. Exam lists start at [1] and so do Lua tables. That agreement is worth more than it sounds, because most languages do not do this and the students who took a different course have to unlearn it.


The Problems


Problem 1 — translate

Rewrite this Lua program in exam notation.

Problem 1
local names = {"Avery", "Blake", "Casey"}
print(names[1])
print(#names)

Two of these three lines change more than the punctuation. The square brackets do not.


Problem 2 — trace

Write the whole list out after every line. What do the last two lines display?

Problem 2
aList ← [10, 20, 30]
APPEND(aList, 40)
INSERT(aList, 2, 15)
REMOVE(aList, 1)
DISPLAY(LENGTH(aList))
DISPLAY(aList[1])

INSERT and REMOVE both move everything after them. If you are not writing the list out each time, you are guessing.


Problem 3 — the pair

What does the exam-notation version display?

Problem 3a — exam notation
first ← [1, 2, 3]
second ← first
second[1] ← 99
DISPLAY(first[1])
DISPLAY(second[1])

And what does the Lua version print?

Problem 3b — Lua
local first = {1, 2, 3}
local second = first
second[1] = 99
print(first[1])
print(second[1])

These do not have the same answer. Explain the difference in one sentence, using the words copy and alias.


Problem 4 — the other pair

Both of these ask a two-element list for its third element. Write down everything each one puts on the screen, in order, and stop where it stops.

Problem 4a — exam notation
scores ← [4, 8]
DISPLAY("start")
DISPLAY(scores[3])
DISPLAY("end")
Problem 4b — Lua
local scores = {4, 8}
print("start")
print(scores[3])
print("end")

The word end is the whole question. One of these reaches it and one does not.

Which behavior is more dangerous? Answer that in one sentence too. The one that stops immediately tells you exactly where the mistake is; the other hands you a value and lets you carry it somewhere else before anything goes wrong.

Submit

Your four answers, including both halves of problems 3 and 4, and the three one-sentence explanations. 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