0% found this document useful (0 votes)
58 views36 pages

Understanding Recursion in Python

The document discusses recursion and provides examples of recursive functions and problems. Recursion occurs when a function is defined using itself. Examples provided include calculating Fibonacci numbers recursively and solving the Tower of Hanoi puzzle recursively by breaking it down into subproblems. Pseudocode is provided to recursively solve these problems by calling the function on smaller inputs until a base case is reached.

Uploaded by

borateswayam8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views36 pages

Understanding Recursion in Python

The document discusses recursion and provides examples of recursive functions and problems. Recursion occurs when a function is defined using itself. Examples provided include calculating Fibonacci numbers recursively and solving the Tower of Hanoi puzzle recursively by breaking it down into subproblems. Pseudocode is provided to recursively solve these problems by calling the function on smaller inputs until a base case is reached.

Uploaded by

borateswayam8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Recursion

From Wikipedia: Recursion occurs when a thing is defined in terms of


itself or its type.

Example:
Fibonacci numbers

for ,
def my_print():
print(“This is ES 113 class”)
my_print()

my_print()

Question: What is the output of this program?

An infinite loop that prints “This is ES 113 class”


def my_print(count):
if count == 0: Stopping condition
return
print(“This is ES 113 class”)
my_print(count-1)

my_print(3)

Question: What is the output of this program?

Prints “This is ES 113 class” three times.


Write a python code that find the Fibonacci number.

def fibo():
if or
return 1
return fibo( + fibo()
def fibo():
if or :
return 1
return fibo( + fibo()

fibo(1) returns 1

ret
1
ns

urn
ur
ret

s1
fibo(2) returns 1
2 1

fibo(3)
def fibo():
if or :
return 1
return fibo(2 + fibo()

3
def fibo():
if or :

ret
1
ns
return 1

urn
ur
ret

s1
return 1 + 1
2 3

fibo(2) returns 2
Write a program that prints that takes an integer and prints stars on
the line for .
def stars():

int(input())
stars

stars()
: denotes the fact that we are printing the the line.
Initially
: denotes the fact that there are lines to be printed
def stars():
count =

while < count:


print ( “*“, end = “ “)

int(input())
stars

Question: Is this program correct?

A lots of problems:
• Infinite while loop
• The first line contains 2 stars
• The program is just printing the first line
def stars():
count =

while < count:


print ( “*“, end=“ ”)

print()
stars(

int(input())
stars

Question: Is this program correct?

One last problem


• The program does not stop. Need a stopping condition.
def stars():
if :
return
count =

while < count:


print ( “*“, end=“ ”)

print()
stars(

int(input())
stars
Given a number , print all binary number on digits in order.

For , print

000
001
010
011
100
101
110
111
def binary(str):

int(input())
binary(“”, )

binary(str):
1. str: binary string that we will print.
2. Initially str=“”
3. : denotes the fact that the binary numbers have digits.
def binary(str):
binary(str+,
binary(str+,)

int(input())
binary(“”, )

Question: Is this program correct?


“”

“0”

“00”

“000”

Need a stopping condition


“0000”
def binary(str):
if len(str) == :
print(str)
return
binary(str+,
binary(str+,)

int(input())
binary(“”, )
“”

“0” “1”

“00” “01” “10” “11”

“000” “001” “010” “011” “100” “101” “110” “111”


Given two numbers , print all digit numbers using whose digits are in
increasing order.

Example: ,

11 :

10 :

12 :
Given two numbers , print all digit numbers using whose digits are in
increasing order.

Example: ,

Your program should output:


12
13
23
def order(string):

int(input())
int(input())
order(“”,, )

order(str):
string: string that we will print.
Initially string=“”
: the string should contain digits
: each digit should be from
def order(string):

while <= :
order(string+ str())

int(input())
int(input())
order(“”,, )

Question: Is this program correct?

Problems
• The program does not stop. Need a stopping condition.
• The program prints all two digit numbers
def order(string):
if len(string) == :
print(string)
return

while <= :
order(string+ str())

int(input())
int(input())
order(“”,, )

Problems
• The program does not stop. Need a stopping condition.
• The program prints all two digit numbers
def order(string):
if len(string) == :
print(string)
return
int(string[-1])+1
while <= :
order(string+ str())

int(input())
int(input())
order(“”,, )

Problems
• Does not work when the string is of length 0.
def order(string):
if len(string) == :
print(string)
return
if len(string)==0:

else:
int(string[-1])+1
while <= :
order(string+ str())

int(input())
int(input())
order(“”,, )
“”

“1” “2” “3”

“12” “13” “23”

def order(string):
if len(string) == :
print(string)
return
if len(string)==0:

else:
int(string[-1])+1
while <= :
order(string+ str())

int(input())
int(input())
order(“”,, )
Tower of Hanoi
A B C

Need to move disk from peg A to peg C under the following


conditions:
1. Only one disk may be moved at a time.
2. Each move consists of taking the upper disk from one of the peg
and placing it on top of another peg.
3. No disk may be placed on top of a disk that is smaller than it.
A B C

Strategy:
1. Move the top two disks from peg A to peg B using peg C.
2. Move the largest disk from peg A to peg C.
3. Move the top two disks from peg B to peg C using peg A.
def tower(A,B,C,):

int(input())
tower(“A”,”B”,”C”,)

tower(A, B, C, ):
1. Move disks from peg1 to peg3 using peg2.
2. Initially,
• A = ”A”
• B = “B”
• C = “C”
3. All the disks are on peg A.
def tower(A,B,C,):
1. Move the top two disks from peg A to peg B using peg C.
2. Move the largest disk from peg A to peg C.
3. Move the top two disks from peg B to peg C using peg A.
int(input())
tower(“A”,”B”,”C”,)

tower(p, peg2, peg3, ):


1. Move disks from peg1 to peg3 using peg2.
2. Initially,
• peg1 = ”A”
• peg2 = “B”
• peg3 = “C”
3. All the disks are on peg A.
def tower(A,B,C,):
1. Move the disks from peg A to peg B using peg C.
2. Move the largest disk from peg A to peg C.
3. Move the disks from peg B to peg C using peg A.
int(input())
tower(“A”,”B”,”C”,)

tower(p, peg2, peg3, ):


1. Move disks from peg1 to peg3 using peg2.
2. Initially,
• peg1 = ”A”
• peg2 = “B”
• peg3 = “C”
3. All the disks are on peg A.
def tower(A,B,C,):
Move the disks from peg A to peg B using peg C.
tower(A,C,B,)
Move the largest disk from peg A to peg C.
print( “move the disk from ” + A + “ to “ + C)
Move the disks from peg B to peg C using peg A.
tower(B,A,C,)
int(input())
tower(“A”,”B”,”C”,)

tower(p, peg2, peg3, ):


1. Move disks from peg1 to peg3 using peg2.
2. Initially,
• peg1 = ”A”
• peg2 = “B”
• peg3 = “C”
3. All the disks are on peg A.
def tower(A,B,C,):
tower(A,C,B,)
print( “move the disk from ” + A + “ to “ + C)
tower(B,A,C,)
int(input())
tower(“A”,”B”,”C”,)

Question: Is this program correct?

• The program does not stop. Need a stopping condition.

• Think about the stopping condition when


def tower(A,B,C,):
if :
return
tower(A,C,B,)
print( “move the disk from ” + A + “ to “ + C)
tower(B,A,C,)
int(input())
tower(“A”,”B”,”C”,)
A,B,C,2

A,C,B,1 B,A,C,1

A,B,C,0 C,A,B,0 B,C,A,0 A,B,C,0

Move the disk from peg A to peg B Move the disk from peg A to peg C Move the disk from peg B to peg C

You might also like