Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1

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

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))

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”)

5 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))

6 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))

7 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.

8 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"

9 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))

10 (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))

11 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?

12 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).

13 Let’s try this : 1. Write a recursive function that prints out every other number starting at 1 and ending at 10 2. 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)

14 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))

15 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))

16 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))

17 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))


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

Similar presentations


Ads by Google