Topic: Recursion – Part 2

Slides:



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

Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Ms N Nashandi Dr SH Nggada Week 08 – Recursion. Outline We shall be covering the following What is a recursion? Iteration versus Recursion. Recursion.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Recursion Salim Arfaoui.
CMPT 120 Topic: Python’s building blocks -> More Statements
Recursion Topic 5.
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Topic: Python’s building blocks -> Variables, Values, and Types
Topic: Functions – Part 1
Topic 6 Recursion.
Topic: Programming Languages and their Evolution + Intro to Scratch
Topic: Iterative Statements – Part 1 -> for loop
CMPT 120 Topic: Python Modules.
CMPT 120 Topic: Searching – Part 2
Chapter 10 Recursion Instructor: Yuksel / Demirer.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Abdulmotaleb El Saddik University of Ottawa
Lesson #6 Modular Programming and Functions.
Topic: Python’s building blocks -> Statements
Topic: Python’s building blocks -> Variables, Values, and Types
Lesson #6 Modular Programming and Functions.
CMPT 120 Topic: Searching – Part 1
Introduction to Recursion
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Topic: Recursion – Part 1
CMPT 120 Topic: Functions – Part 4
Topic: Functions – Part 2
Programming Fundamentals
Recursion, Tail Recursion
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
University of Washington Computer Programming I
Lesson #6 Modular Programming and Functions.
Recursion Output Input
Learning to Program in Python
Learning to Program in Python
Recursion Chapter 11.
Fundamentals of Programming
Passing Parameters by value
This Lecture Substitution model
CS302 - Data Structures using C++
Lesson #6 Modular Programming and Functions.
Recursion Taken from notes by Dr. Neil Moore
This Lecture Substitution model
Chapter 18 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
This Lecture Substitution model
CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
CMPT 120 Lecture 19 – Unit 3 – Graphics and Animation
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Lecture 20 – Practice Exercises 4
Lecture 20 – Practice Exercises 4
Software Testing.
Recursive functions.
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Lecture 6 - Recursion.
Presentation transcript:

Topic: Recursion – Part 2 CMPT 120 Topic: Recursion – Part 2

Last Lectures Recursion Examples of recursion occurring in the real world Examples of recursion occurring in the mathematical world What is recursion Iterative code versus Recursive code Demo of recursion

Learning outcomes At the end of this course, a student is expected to: Describe the concept of recursion, of recursive definitions and recursive functions. Use recursion to solve problems: Use box tracing to predict the result of simple recursive code (drawing a call stack) Design recursive code Design recursive functions that recurse, for example, on lists and strings

Today’s Menu Design and implement a recursive function Box trace (hand trace with boxes) a recursive function to figure out what it produces

How to design/implement a recursive function? First, we need to think recursively http://www.notonthehighstreet.com/britishandbespoke/product/paint-your-own-russian-dolls-for-children

Think recursively … hum! Easy for you to say! Let’s give it a try! Problem Statement Write a Python function that returns the factorial of a number passed as a parameter

How? Remember the 2 properties Remember the two properties we saw in the definition of recursion: A simple base case (or cases)—a terminating scenario that does not use recursion to produce an answer A set of rules that reduce all other cases toward the base case

Using these 2 properties as a procedure Base Case Its purpose is to stop the function from calling itself indefinitely So it does not use recursion to produce an answer Recursive Case Use recursion to eventually produce an answer So, this is where the function calls itself with a modified (often smaller) form of the problem How do we get this “modified form of the problem” -> A set of rules Function interface Function name Parameters Returned value

General form of recursive function def recursiveFunction( … ): if (base case): <- may be > 1 result = base case else: result = expression including recursive call(s) return result

Let’s give it a go: -1- factorial function Recursive definition of factorial: Base cases: Rule:

Let’s give it a go: -2- factorial function

Let’s test our recursive function How? By box tracing our recursive function Box tracing -> hand tracing using stack frames (boxes) Basically, we mimic what the visualizer does but on paper

Box tracing a recursive function Knowing how to hand/box trace code is very important Even more important when dealing with recursive functions Why? Because following the execution of recursive functions can become very complicated very quickly Box tracing allows us to figure out what a recursive function does (keeping track of its parameters and local variables), what it produces (keeping track of its returned value) and where the execution flow returns to in a systematic way

Box trace of factorial function

That was fun, no? Let’s try again! Problem Statement Write a Python function that multiplies two numbers without using the multiplication operator

Let’s give it a go: -1- multiplication function Brainstorming:

Let’s give it a go: -2- multiplication function Recursive definition of a multiplication: Base cases: Rule:

Let’s give it a go: -3- multiplication function

Another example: -1- find a character in a string Problem Statement Write a Python function that finds a character in a string

Another example: -2- find a character in a string Base case: Rule:

Another example: -3- find a character in a string

Summary Design and implement a recursive function Box trace (hand trace with boxes) a recursive function to figure out what it produces

Next Lecture Revisiting List