Recursion!

Fred Agbo

November 30, 2026

Welcome Back!

  • Class grouping today: Scan the QR code or go to https://tools.jedrembold.prof/daily
  • Class code is WkiFn
  • Introduce yourselves! What was the highlights of your Thanksgiving week?

Quick Announcements

  • Welcome to the last week of class
    • Last day of class is Friday December 4th
  • Project Infinite Adventure is due on Friday December 4th
    • All assignments including any missed ones are also due on December 4th
      • Bear in mind that I have the descretionary right to grade with points deduction penalty on any late assignments.
  • Remember! Section meeting this week could be helpful in adventure project
  • FINAL EXAM is on Tuesday December 8th from 8:00 a.m. - 11:00 a.m
  • Received Course Evaluation Notification?
    • Please, I need everyone to fillout this evaluation form.
      • I will provide some minutes to allow you to complete this form in class on Wednesday
  • Friday this week will be dedicated to course reviews and Q&A

Daily LO’s

  • What is recursion and what does it look like in code?

  • Can we trace through recursive calls?

  • What are some drawbacks of recursion?

  • How can we use recursion graphically?

  • ———————————-

  • Head to the DIscord and download the starter code files

Group Problem 1: A Bit of Fibonacci

  • The Fibonacci sequence is a classic sequence whose \(n^{th}\) number can be defined recursively: \[F(n) = \begin{cases} 1 & \text{if } n \leq 1 \\ F(n-1) + F(n-2) & \text{otherwise}\end{cases} \]
  • In partners, define a function fib that returns the nth Fibonacci number

Problem 1b: Fibonacci Calls

  • Switch who is typing!
  • It can be interesting to track how many times the function is called with each \(n\) value
  • Define a global dictionary which will have different values of n as the keys, and a counter associated with each
  • Whenever your fib function is called, increment (or initialize) the correct count in your dictionary.
  • When you call fib(5), how many times is fib(1) called?
    • You can just print it your counts after running fib(5)
  • What about with fib(25)?

Problem 2: A-mazing Recursion

  • We looked at one maze-solving algorithm earlier this semester
  • Recursion can be another approach!
  • The maze_solver.py program demonstrates one recursive approach, which also tracks the final path
  • Running the program initially will just give you an interactive window with the maze
  • Your task is to trace through the recursive algorithm by hand
  • Keep in mind that each function call checks 4 directions! It can be useful to track which have been used so far

Problem 2b: A-mazing Solutions

  • Add code to the bottom of the program to actually call the recursive solver and print out the found path
    • You know that the start point is always in the upper left corner
  • Try it with larger mazes! Would our earlier approach worked with these mazes?

Demo

Sierpinski Carpet

  • You likely have seen the Sierpinski Sponge in the middle of the second floor of Ford
  • Let’s try to create a 2D version in PGL using recursion

// reveal.js plugins