Skip to content

Nov 21: Prac6, Recursive Search

Learning Objectives

After today's class, you should be able to:

  • Explain the steps for writing a recursive function in Python.
  • Implement a recursive function that searches a file system.

Recursive Functions

[10 min]

How to write a recursive function:

  • Step 1. Base case: how will the recursion stop?
  • Step 2. Current case: what needs to be computed?
  • Step 3. Recursion: how will the arguments change?

Example Programs

Review and discuss examples 1–4 from Nov 17

Example: find_files

[15 min]

A recursive function that searches the file system: filesys2.py

Sample output when run with PA1 simp as command-line args:

['PA1/__pycache__/simplify.cpython-312.pyc',
 'PA1/__pycache__/test_simplify.cpython-312-pytest-8.4.2.pyc',
 'PA1/simplify.py',
 'PA1/test_simplify.py']

Note

Special characters in file paths:

  • ~ (tilde) means home folder
  • . (dot) means current folder
  • .. (dot dot) means parent folder

Practice Quiz 6

[25 min]

Topics: Nested Data and Recursion

PDFs: See Prac6 on Canvas Files

  • 1st sheet: "written potion"
    • Turn off monitor during written portion
  • 2nd sheet: "coding potion"
    • You must use Thonny (not VS Code)
    • Do not turn off recording; just log out