Blackboard Exercise 5: Worked solutions
Algorithms, Programming, and Data Representation · Module 92313 · Blackboard Exercise 5
Build the recursive pattern from its base case and smaller calls, trace the Sierpinski counts, and use the call stack to move into and out of nested dream levels.
Exercise 5.1: Recursion
Connected topic: The recursive pattern (§8.1) · Tracing calls (§8.2)
5.1.1 Define recursion
Try first: Define recursion in fewer than three sentences, including how a recursive process stops.
Attempted it? Open the worked steps
5.1.1 Define recursion · worked solution
Recursion solves a problem by having a function call itself on a simpler instance of the same problem; a base case answers directly, and each recursive case must make progress toward it.
Check: the definition includes both the self-call and the stopping case, so it rules out a call that repeats forever without getting simpler.
5.1.2 Complete the factorial function
Try first: Fill the blanks with a base-case comparison, its return value, and the decrement in the recursive call.
Attempted it? Open the worked steps
5.1.2 Complete the factorial function · worked solution
Use the course's positive-integer convention for this version:
factorial(1)is the base case, and for x > 1 the recurrence is x! = x · (x − 1)!.Fill the blanks with
1,1, and1:def factorial(x): if x == 1: return 1 return x * factorial(x - 1)For
factorial(5), calls descend tofactorial(1); then the waiting calls return 1, 2, 6, 24, and 120.The sheet does not specify whether zero is an allowed input. If the function must also accept zero, use
x == 0as the base case and return 1, which follows the convention 0! = 1.Check: the argument decreases by one on each call, so every positive integer input reaches 1; the value then multiplies back up.
Exercise 5.2: Sierpinski triangle
Connected topic: The recursive pattern (§8.1) · Tracing calls (§8.2)
5.2.1 Continue the triangle pattern
Try first: Continue the given stages for n = 2 and n = 3 while leaving the empty central triangles blank.
Attempted it? Open the worked steps
5.2.1 Continue the triangle pattern · worked solution
Start at n = 0 with one solid triangle. At every next step, split each black triangle into four equal smaller triangles and keep the three corner triangles black; the middle triangle stays blank.
So the counts at the requested steps are:
Step n Black triangles Construction 0 1 One solid starting triangle. 1 3 Three black corner triangles. 2 9 Each of the 3 black triangles produces 3 black corners. 3 27 Each of the 9 black triangles produces 3 black corners. For the drawing, repeat that replacement inside every black triangle at n = 1 to obtain step 2, then repeat it inside all nine black triangles to obtain step 3. Previously empty central triangles remain empty.
Check: the number triples at each step, and the large central gaps remain blank rather than being recoloured.
5.2.2 Write a recursive triangle counter
Try first: Turn the repeated three-way growth into a recurrence with a base case for the starting triangle.
Attempted it? Open the worked steps
5.2.2 Write a recursive triangle counter · worked solution
Let
black_triangles(n)return the number at step n. The domain is a non-negative integer step, and step zero contains one black triangle.For n > 0, each black triangle creates three black corner triangles, so the total is three times the previous step's count:
T(0) = 1, T(n) = 3 · T(n − 1)
That recurrence gives a fully recursive implementation:
def black_triangles(n): if n == 0: return 1 return 3 * black_triangles(n - 1)The base case returns directly. Each recursive call reduces
n, and the returned smaller count is multiplied by three on the way back.Check:
black_triangles(0)returns 1 andblack_triangles(1)returns 3, matching the first two drawings.
5.2.3 Count the triangles at step 6
Try first: Use the recurrence table to find the black-triangle count at n = 6.
Attempted it? Open the worked steps
5.2.3 Count the triangles at step 6 · worked solution
Continue multiplying the previous count by three:
n Calculation T(n) 0 Base case 1 1 3 × 1 3 2 3 × 3 9 3 3 × 9 27 4 3 × 27 81 5 3 × 81 243 6 3 × 243 729 Equivalently, T(6) = 36 = 729 black triangles.
Check: six recursive steps multiply the starting count 1 by 3 six times, giving 729.
Exercise 5.3: Recursion application
Connected topic: The recursive pattern (§8.1) · Tracing calls (§8.2)
5.3.1 Recursion and repetition
Try first: State the difference between recursion and repetition in one sentence.
Attempted it? Open the worked steps
5.3.1 Recursion and repetition · worked solution
Recursion repeats work through a function calling itself on a smaller problem, whereas repetition repeats a block with a loop.
Check: both can perform work repeatedly; the distinction is how the next repetition is expressed.
5.3.2 Write the recursive dream function
Try first: Make the dream level progress to the maximum, do the job once, unwind one level at a time, and check the totem after waking.
Attempted it? Open the worked steps
5.3.2 Write the recursive dream function · worked solution
Assume level 0 means awake,
current_levelis the level the dreamer is in now, and the helper'slevelargument names the destination level. Start withdream(0, max_level), wheremax_levelis non-negative.The recursive case enters the next deeper level with
play_lullaby(current_level + 1), then callsdreamfor that level. The base case runsdo_important_job()once at the maximum level.When the deeper call returns,
kick(current_level)wakes the dreamer to this call's level. That line runs after the recursive call, so it executes on the way back up the call stack.Here is one complete version. It rejects a maximum below the current level, returns totems only from the outer awake-level call, and checks the totem after the last upward transition:
def dream(current_level, max_level): if current_level < 0 or max_level < current_level: raise ValueError("expected 0 <= current_level <= max_level") if current_level == max_level: do_important_job() else: next_level = current_level + 1 play_lullaby(next_level) dream(next_level, max_level) kick(current_level) if current_level == 0: return check_totem()For
dream(0, 3), the calls go 0 → 1 → 2 → 3. The job runs at 3; returns then callkick(2),kick(1), andkick(0). Once awake at 0,check_totem()runs.Check:
play_lullabyruns once per downward move,kickonce per upward move, the job runs once at the maximum, and the totem check runs only after the outermost call wakes.
Personal worked solutions based on APDR Blackboard Exercise 5, written in my own words; no exercise-sheet prose or figures are reproduced. The dream helpers are abstract functions supplied by the prompt. Not an official FAU publication; check course details against the current materials. Made by Thanate Amornjiraporn (Focus).