Skip to content

Nov 13: Quiz5, PA3 (final project!), and Recursive functions

Learning Objectives

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

  • Describe the requirements and programming topics of PA 3.
  • Identify the base case and recursive step of the factorial function.
  • Trace a recursive function by hand to predict the number of calls.
  • Write short recursive functions based on mathematical sequences.

Announcements

Quiz 5

  • Two portions to the quiz: written and coding
  • Log in as student
  • Start exam recording
  • Log in to Canvas only
  • Raise your hand when complete with Canvas written portion
  • Open Thonny for coding portion only
  • Submit code through Canvas (will go to Gradescope)
  • Log out when finished

PA3 Overview

Submission Points Due Notes
Part A. Tests 10 points Tue 11/18 write assert statements
Part B. Code 50 points Thu 11/20 implement 9 functions
Part C. Code 40 points Tue 12/02 recursion + open ended

POGIL Activity

  • Recursive Functions
    • If you are absent today, complete this activity at home
    • Bring your completed activity to class or office hours

Model 1

factorial.py
1
2
3
4
5
6
7
8
9
def factorial(n):
    # base case
    if n == 0:
        return 1
    # general case
    product = 1
    for i in range(n, 0, -1):
        product *= i
    return product

Model 2

fibonacci.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def fibonacci(n):
    # base case
    if n == 1 or n == 2:
        return 1
    # general case
    return fibonacci(n - 1) + fibonacci(n - 2)

if __name__ == "__main__":
    for i in range(1, 6):
        print(fibonacci(i))