Introduction to Recursion, Exercises of Computer Programming

That's the code that can be made into a recursive function call. } } These are the same! Page 11 ...

Typology: Exercises

2022/2023

Uploaded on 02/28/2023

ameen
ameen 🇺🇸

4.6

(5)

236 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS111 Computer Programming
Department of Computer Science
Wellesley College
Introduction to Recursion
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Introduction to Recursion and more Exercises Computer Programming in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science

Wellesley College

Introduction to Recursion

Recursive Patterns Recursion I 2

What is Recursion? Gluing functions together... ...where the sub-problems involve the problem itself! A recursive function is a function that invokes itself. With recursion, the solution to a problem depends on solutions to smaller instances of the same problem Recursion I

Concepts in this slide :

Recursion is an instance of

solving a problem by sub-

division.

Self-Containing Patterns 5 Fractals , found in nature and mathematics, are examples of patterns that contain smaller copies of themselves. This fern image is made of three smaller ferns. Recursion I

Review: functions calling other functions (in anticipation of writing functions that call themselves) def print4(s): print2(s) print2(s) def print2(s): print(s) print(s) print4( 'okay' ) def print4(s): print2(s) print2(s) print4( 'okay' ) def print2(s): print(s) print(s) def print2(s): print(s) print(s) def print4(s): print2(s) print2(s) print4( 'okay' ) Which would work? Why/why not? Recursion I

Our first recursive function: countDown Let’s write a function that prints the integers from n down to 1 ( without using loops ): In [ ]: countDown(5) 5 4 3 2 1 8

def countDown(n):

'''Prints integers from n down to 1'''

In [ ]: countDown(4) 4 3 2 1 print(4) countDown(3) Recursion I

Decomposition

We can think of the countDown(5)

as composed of the print(5)

statement and the invocation of

countDown(4). And so on.

print(5) countDown(4)

Concepts in this slide :

In recursion, the smaller

problems are the same as

the problem we’re trying

to solve.

countDown: More Cases When you’re solving a recursive problem, you can start by writing many elif cases. Continue until you see a pattern develop.

def countDown(n):

'''Prints integers from n down to 1'’’

if n < 1:

pass # Do nothing

elif n == 1:

print(1)

elif n == 2:

print(2)

print(1)

elif n == 3:

print(3)

print(2)

print(1)

elif ...

Recursion I (^10)

Finding the pattern

As you write elif cases, you’ll

eventually see the same code

appearing again and again. That’s

the code that can be made into a

recursive function call.

} } These are the same!

countDown: More Cases You can simplify the pattern by using a recursive call to the same function with different parameters (which will send you into a different elif case).

def countDown(n):

'''Prints integers from n down to 1'’’

if n < 1:

pass # Do nothing

elif n == 1:

print(1)

elif n == 2:

print(2)

print(1)

elif n == 3:

print(3)

countDown(2)

elif ...

Recursion I (^11) } Now it’s recursive

countDown: Recursive Case The recursive case. One we understand how to write a recursive else case, we can eliminate unnecessary elif cases. Sometimes we can leave out the base case too, if it doesn’t do anything.

def countDown(n):

'''Prints integers from

n down to 1'''

if n < 1:

pass # Do nothing

else:

print(n)

countDown(n-1)

def countDownImplicit(n):

'''Prints integers from

n down to 1''’

if n > 0:

print(n)

countDownImplicit(n-1)

Recursion I (^13)

Concepts in this slide :

The recursive case makes

the problem smaller. The

base case can be omitted.

To notice:

  • We got rid of several elif cases at once
  • The recursive step does two things:

a) performs an action that contributes to the solution

b) Invokes the function with “smaller” parameters

  • It is possible to omit the base case when it does nothing

Structure of Recursion All recursive functions must have two types of cases:

  • BASE case : a simple case where the solution is obvious. In this case the function does not invoke itself, since there is no need to decompose the problem into subproblems.
  • RECURSIVE case : a case where the problem
    • is decomposed into subproblems
    • at least one of the subproblems is solved by invoking the function being defined, i.e., the function is invoked in its own body. You should assume the recursive function works correctly for the smaller subproblems (this is known as “wishful thinking”) Recursion I (^14)

Invocation of countDown(3) Anatomy of function call frames

def countDown(n):

'''Prints integers from n down to 1'''

if n>0:

print(n)

countDown(n-1)

if n>0: print(n) countDown(n-1) countDown(3)

In [4]: countDown(3)

n 3

function call frame

There is a local variable in

the frame for:

(1) each parameter

(2) each local name

control arrow shows

what’s currently being

evaluated in function body

Recursion I (^16)

In [4]: countDown(3)

if True: print(n) countDown(n-1) countDown(3) n 3

def countDown(n):

'''Prints integers from n down to 1'''

if n>0:

print(n)

countDown(n-1)

if True: print( 3 ) countDown(n-1) countDown(3) n 3

if True: print( 3 ) countDown( 3 - 1) countDown(3) if True: n 3 print( 3 ) countDown( 2 ) countDown(3) n (^3) if n>0: print(n) countDown(n-1) countDown(2) If 2 >0: n 2 print(n) countDown(n-1) countDown(2) if True: n 2 print(n) countDown(n-1) countDown(2) n 2 if True: print( 3 ) countDown( 2 ) countDown(3) n (^3) if True: print( 2 ) countDown(n-1) countDown(2) n 2

if True: print( 2 ) countDown( 2 - 1) countDown(2) n 2 if n>0: print(n) countDown(n-1) countDown(1) if True: n 1 print( 2 ) countDown( 1 ) countDown(2) n 2 if^1 >0: print(n) countDown(n-1) countDown(1) if True: n 1 print(n) countDown(n-1) countDown(1) if True: n 1 print( 1 ) countDown(n-1) countDown(1) n 1

if True: print( 1 ) countDown( 0 ) countDown(1) if True: n 1 print( 1 ) countDown( 0 ) countDown(1) n 1 if False: print(n) countDown(n-1) countDown(0) n 0 if True: print( 2 ) countDown( 1 ) countDown(2) n 2 if False: print(n) countDown(n-1) countDown(0) n 0 Invocation of countDown(3) Draw the diagram of function call frames Recursion I (^17)

In [2]: countDown(3)

**- 1

  • 2
  • 3**

RuntimeError: maximum recursion depth exceeded

while calling a Python object

"Maximum recursion depth exceeded" In practice, the infinite recursion examples will terminate when Python runs out of resources for creating function call frames, leading to a maximum recursion depth exceeded error message: Recursion I (^19)

What does this function do? def mystery(n): if n < 1: pass else: mystery(n - 1) print(n) What does mystery(3) print? Recursion I (^20)