What gets printed out? def f(n): if (n == 0): return (" ") else: print(“blug ") return f(n-1) f(3)

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Recursion.
DATAFLOW TESTING DONE BY A.PRIYA, 08CSEE17, II- M.s.c [C.S].
Linear vs Binary Search COP What is recursion? // Pre-conditions: exponent is >= to 0 // Post-conditions: returns base exponent int Power(int base,
PYTHON FRUITFUL FUNCTIONS CHAPTER 6 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
Solving Equations = 4x – 5(6x – 10) -132 = 4x – 30x = -26x = -26x 7 = x.
Recursion in Python. Recursion Problems in every area of life can be defined recursively, that is, they can be described in terms of themselves. An English.
Let’s solve the equation: We shall use a scale to represent our equation. The variable x will be replaced with and the numbers will be represented with.
Recursion Definition: A method that calls itself. Recursion can be direct or indirect. Indirect recursion involves a method call that eventually recalls.
Recursion. Binary search example postponed to end of lecture.
Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.
Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
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.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
2.8 – Literal Equations and Dimensional Analysis
More on Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 9 Iteration: Recursion 5/02/09 Python Mini-Course: Day 3 - Lesson 9 1.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
Recursion.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School.
CMPS 1371 Introduction to Computing for Engineers RECURSION.
Graphing Linear Inequalities in Two Variables A linear inequality in two variables takes one of the following forms: The solution of a linear inequality.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
PPT3. Quick function:  Write a function that checks to see if a number is even or not.  What TYPE does this return?
Unit Test Practice Algebra 2 Unit Q1: Solve for y. 4y + 6 = 18 a) c) a) c) b)d) b)d)
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
Chapter 6 Questions Quick Quiz
Duke CPS Recursion l Often phrased as “a function calls itself” ä function actually calls a clone, not itself ä each clone has its own variables/parameters/state.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
The Python Language Petr Přikryl Part IIb Socrates IP, 15th June 2004 TU of Brno, FIT, Czech Republic.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Review of Recursion  a recursive method calls itself  to prevent infinite recursion you need to ensure that: 1. the method reaches a base case 2. each.
5.1 Solving Systems of Equations Objectives: --To identify a system of equations --To determine if a point is a solution to a system --To use graphing.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Functions CMSC 201 – Lab 5. Overview Objectives for today's lab:  Practice breaking down a program into multiple functions  Practice writing function.
Identities, Contradictions and Conditional Equations.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Recursion DRILL: Please take out your notes on Recursion
Python: Logic Gates Damian Gordon.
Module 3.
Intro to Computer Science II
Other things: functions
Recursion Output Input
Ch 12 Recursion Mr. Dave Clausen La Cañada High School
Barb Ericson Georgia Tech
Program Flow Control Selection & repetition
Chapter 12 Supplement: Recursion with Java 1.5
Linear Search Binary Search Tree
More on Functions (Part 2)
Nate Brunelle Today: Conditional Decision Statements
Module 1-10: Recursion.
Nate Brunelle Today: Conditional Decision Statements
Nate Brunelle Today: Conditional Decision Statements
Ch 12 Recursion Mr. Dave Clausen La Cañada High School
Python Reserved Words Poster
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Presentation transcript:

What gets printed out? def f(n): if (n == 0): return (" ") else: print(“blug ") return f(n-1) f(3)

What about? def f(x): if (x == 0): return x else: return(x + f(x-1)) print(f(4)) def f2(x): if (x == 1): return (str(x)) else: return(str(x) + f2(x-1)) print(f2(4))

Recursion  Recursion is when a function is defined in terms of itself (it calls itself).  Def: A recursive definition is one that defines something in terms of itself (that is, recursively)  Recursion is, in essence, making a function happen again and again without our having to call it (convenient, huh?) #This is recursion def recurse(): recurse() return(recurse()) #This isn’t def nonrecurse(): return(“nope”)

Try: def f(x): return(x + f(x-1)) print(f(4)) def f2(x): if (x == 1): return x else: return(x + f2(x+1)) print(f2(4)) def f3(x): if (x == 1): return x else: return(x + f3(x-2)) print(f3(4))

How about: def f(x): if x == 100: return(“none") else: if (x**2 - 3 *x - 4) == 0: print(str(x) + " solves the equation ") return(f(x+1)) print(f(-100))

Recursion Essentials  We now have the basics: 1. Must formulate a problem in terms of itself. (the function must call itself) 2. Must include a condition for stopping the recursion (base case) 3. Must make sure that we will always hit that stopping condition.

Stacking up Functions def f(x): if (x == 0): return x else: return(x + f(x-1)) print(f(4)) def f2(x): if (x == 1): return (str(x)) else: return(str(x) + f2( x-1)) print(f2(4)) Stack in memory f(4) = 4 + f(3)? f(3) = 3 + f(2)? f(2) = 2 + f(1)? f(1) = 1 + f(0)? f(0) = 0 __________________________ f2(4) = “4” + f2(3) f2(3) =“3” + f2(2) f2(2) = “2” + f2(1) f2(1) = "1"

Recursion: Try def f5 (a): if (a <= 0): return(1) elif ((a%2) ==0): return (f5(a - 1)) else: return (a*f5(a-1)) print(f5(6)) print(f5(5)) print(f5(-1)) def f6(x): if (x <= 1): return str(x) else: return(f6(x-1) + str(x) ) print(f6(5)) def f7(a,b): if (b <= 0): return(a) elif((b%3)== 0): return(f7(a+1,b-1)) else: return(f7(a,b-1)) print(f7(0,13))

(Cool, but challenging) def f4 (a, b): if (b == 0): return (a) if (a < b): return f4 (b, a) else: return f4 (b, a%b) print(f4(27,12)) print(f4(25,50)) print(f4(15,20))

def nums(x,y,z): if y == 1: return z + x else: return(nums(x%y, y//10, z+x//y)) print(nums(1354,1000,0)) print(nums(254,100,0)) What did we just do?

Writing a recursive function:  Write the base case (the stopping case) first!  There can be more than one stopping condition  Figure out how you’re going to get to the base case  (e.g., write it as if it only needs to happen twice, once without the base case and once with, making sure the second case gets you to the first case).

Let’s try this : 1. Write a recursive function that prints out every other number starting at 1 and ending at Write a recursive function that counts the number of numbers that is evenly divisible by 3 between x and y 3. Write a recursive function that calculates x to the yth power, assuming we’ve only got multiplication (i.e., you can’t use **) 4. Write a recursive function that determines whether a number is prime or not (and returns True if it is, and False if it isn’t)

Problem 1:Write a recursive function that prints out every other number starting at 1 and ending at 10 def g(x): if x == 10: returnstr(x)) elif x > 10: return() else: return(str(x) + g(x+2)) print(g(1))

Problem 2: Write a recursive function that sums every even number between 1 and 15 def h(x): if x > 15: return(0) elif x%2 == 0: return (x + h(x + 1)) else: return(h(x+1)) print(h(1))

Problem 3: Write a recursive function that finds x to the yth power, assuming we’ve only got multiplication (i.e., you can’t use **) def k(x,y): if y == 0: return(1) else: return(x * k(x,y-1)) print(k(3,4)) print(k(2,4))

Write a recursive function that determines whether a number is prime or not (and returns True if it is, and False if it isn’t) def f(x,y): if (y>(x//2)): return(True) elif (x%y==0): print(y) return(False) else: return (f(x,y+1)) print(f(6,2)) print(f(137,2)) print(f(55,2)) print(f(29,2)) def f(x,y): if (y>(x//2)): return(True) else: return (x%y!=0 and f(x,y+1)) print(f(6,2)) print(f(137,2)) print(f(55,2)) print(f(29,2))