# Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a

• View
229

9

Tags:

Embed Size (px)

### Text of Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python",...

• Slide 1
• chapter 16 Recursion: Another Control Mechanism
• Slide 2
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. a function that calls itself The very basic meaning of a recursive function is a function that calls itself Leads to some funny definitions: Def: recursion. see recursion However, when you first see it, it looks odd. Look at the recursive function that calculates factorial (code listing 16-2)
• Slide 3
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. It doesnt do anything! This is a common complaint when one first sees a recursive function. What exactly is it doing? It doesnt seem to do anything! Our goal is to understand what it means to write a recursive function from a programmers and computers view.
• Slide 4
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Defining a recursive function
• Slide 5
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. 1) divide and conquer Recursion is a natural outcome of a divide and conquer approach to problem solving A recursive function defines how to break a problem down (divide) and how to reassemble (conquer) the sub-solutions into an overall solution
• Slide 6
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. 2) base case recursion is a process not unlike loop iteration. You must define how long (how many iterations) recursion will proceed through until it stops The base case defines this limit Without the base case, recursion will continue infinitely (just like an infinite loop)
• Slide 7
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Simple Example Lao-Tzu: A journey of 1000 miles begins with a single step def journey (steps): the first step is easy (base case) the n th step is easy having complete the previous n-1 steps (divide and conquer)
• Slide 8
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Code Listing 16-1
• Slide 9
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.
• Slide 10
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Factorial You remember factorial? factorial(4) = 4! = 4 * 3 * 2 * 1 The result of the last step, multiply by 1, is defined based on all the previous results that have been calculated
• Slide 11
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Code Listing 16-2
• Slide 12
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.
• Slide 13
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Trace the calls 4! = 4 * 3! start first function invocation 3! = 3! * 2 start second function invocation 2! = 2 * 1! start third function invocation 1! = 1 fourth invocation, base case 2! = 2 * 1 = 2 third invocation finishes 3! = 3 * 2 = 6 second invocation finishes 4! = 4 * 6 = 24 first invocation finishes
• Slide 14
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Another: Fibonacci Depends on the Fibonacci results for the two previous values in the sequence The base values are Fibo(0) == 1 and Fibo(1) == 1 Fibo (x) = fibo(x-1) + fibo(x-2)
• Slide 15
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Code Listing 16-3
• Slide 16
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.
• Slide 17
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Trace fibo(4) = fibo(3) + fibo(2) fibo(3) = fibo(2) + fibo(1) fibo(2) = fibo(1) + fibo(0) = 2 # base case fibo(3) = 2 + fibo(1) = 3 # base case fibo(4) = 3 + 2 = 5
• Slide 18
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Reverse string We know Python has a very simple way to reverse a string, but lets see if we can write a recursive function that reverses a string without using slicing.
• Slide 19
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Code Listing 16-4 Template for reversal
• Slide 20
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.
• Slide 21
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Base Case What string do we know how to trivially reverse? Yes, a string with one character, when reversed, gives back exactly the same string We use this as our base case
• Slide 22
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Code Listing 16-5
• Slide 23
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. def reverser (aStr): if len(aStr) == 1: # base case return aStr # recursive step # divide into parts # conquer/reassemble theStr = raw_input("Reverse what string: ") result = reverser(theStr) print Reverse of %s is %s\n" % (theStr,result)
• Slide 24
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Recursive step We must base the recursive step on what came before, plus the extra step we are presently in. Thus the reverse of a string is the reverse of all but the first character of the string, which is placed at the end. We assume the rev function will reverse the rest rev(s) = rev(s[1:]) + s
• Slide 25
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Code Listing 16-6
• Slide 26
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.
• Slide 27
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.
• Slide 28
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. How does Python keep track?
• Slide 29
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. The Stack A Stack is a data structure, like a List or a Dictionary, but with a few different characteristics A Stack is a sequence A stack only allows access to one end of its data, the top of the stack
• Slide 30
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.
• Slide 31
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Operations pop : remove top of stack. Stack is one element smaller push(val) : add val to the stack. Val is now the top. Stack is one element larger top : Reveals the top of the stack. No modification to stack
• Slide 32
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.
• Slide 33
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Stack of function calls Python maintains a stack of function calls. If a function calls another function recursively, the new function is pushed onto the calling stack and the previous function waits The top is always the active function When a pop occurs, the function below becomes active
• Slide 34
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Code Listing 16-7
• Slide 35
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.
• Slide 36 >> factorial(4) Enter factorial n = 4 Before recur"> >> factorial(4) Enter factorial n = 4 Before recursive call f(3) Enter factorial n = 3 Before recursive call f(2) Enter factorial n = 2 Before recursive call f(1) Enter factorial n = 1 Base case. After recursive call f(1) = 1 After recursive call f(2) = 2 After recursive call f(3) = 6 24"> >> factorial(4) Enter factorial n = 4 Before recur" title=""The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. >>> factorial(4) Enter factorial n = 4 Before recur">
• "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. >>> factorial(4) Enter factorial n = 4 Before recursive call f(3) Enter factorial n = 3 Before recursive call f(2) Enter factorial n = 2 Before recursive call f(1) Enter factorial n = 1 Base case. After recursive call f(1) = 1 After recursive call f(2) = 2 After recursive call f(3) = 6 24

Recommended ##### Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion
Documents ##### Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That
Documents ##### RECURSION CITS1001. 2 Scope of this lecture Concept of recursion Simple examples of recursion
Documents ##### Recursion Recursion Recursion Recursion Recursion Recursion Recursion Recursion Recursion
Documents ##### Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures
Documents ##### Recursion with Turtles - cs111/archive/cs111_spring15/...¢  Looping Turtles # Draws "flowers" with numPetals
Documents ##### Chapter 4 Working with Strings. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Sequence of characters
Documents ##### Chapter 17 Recursion. Chapter Scope The concept of recursion Recursive methods Infinite recursion When to use (and not use) recursion Using recursion
Documents Documents