1 / 17

What gets printed out?

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

alea-avila
Télécharger la présentation

What gets printed out?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Recursion • Recursion is when a function is defined in terms of itself (it calls itself). • Def: A recursive definitionis 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”)

  4. Try: def f3(x): if (x == 1): return x else: return(x + f3(x-2)) print(f3(4)) 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))

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

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

  7. Stacking up Functions 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" def f(x): if (x == 0): return x else: return(x + f(x-1)) print(f(4)) deff2(x): if (x == 1): return (str(x)) else: return(str(x) + f2( x-1)) print(f2(4))

  8. Recursion: Try 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)) 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))

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

  10. defnums(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?

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

  12. Let’s try this: • Write a recursive function that prints out every other number starting at 1 and ending at 10 • Write a recursive function that counts the number of numbers that is evenly divisible by 3 between x and y • Write a recursive function that calculates x to the yth power, assuming we’ve only got multiplication (i.e., you can’t use **) • 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)

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

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

  15. 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 **) defk(x,y): if y == 0: return(1) else: return(x * k(x,y-1)) print(k(3,4)) print(k(2,4))

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

More Related