1 / 73

The last CS 5 lecture you’ll ever need!

Aye! Aye! Aye! mateys…. CS? This is just like magic!. Tutoring hours: afternoons & evenings (LAC). *. The last CS 5 lecture you’ll ever need!. Inducing labor. Reducing labor. ==. for the machine!. for humans!. did somebody say induction?. Hw #1 due this Sunday, 9/13, at 11:59 pm.

conley
Télécharger la présentation

The last CS 5 lecture you’ll ever need!

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. Aye! Aye! Aye! mateys… CS? This is just like magic! Tutoring hours: afternoons & evenings (LAC) * The last CS 5 lecture you’ll ever need! Inducing labor Reducing labor == for the machine! for humans! did somebody say induction? Hw #1due this Sunday, 9/13, at 11:59 pm HMC's legal counsel requires us to include these footnotes… On Warner Brothers' insistence, we affirm that this 'C' does not stand for 'Chamber' and 'S' does not stand for 'Secrets.' * Caution: do not take this statement too literally or it is possible find yourself in twice as many CS 5 lectures as you need!

  2. hw1 if you attended lab (see website…) : - you receive credit for the lab portion of the HW (#1a, #1b) else: - you should complete the two lab problems (#1a, #1b) regardless, you should complete the other problems (#2) Is this Python?? Problem #2: Functions Extra Credit: Files!

  3. Computation's Dual Identity "variables as containers" 42 41 name: y type:int LOC:304 name: x type:int LOC:300 memory location 300 memory location 304 Computation Data Storage

  4. Naming helps! defconvertFromSeconds(s): # total seconds """ convertFromSectons(s): Converts an integer # of seconds into a list of [days, hours, minutes, seconds] input s: an int """ seconds = s % 60 # leftover seconds s = (s / 60) # total minutes minutes = s % 60 # leftover minutes s = s / 60 # total hours hours = s % 24 # leftover hours days = s / 24 # total days return [days, hours, minutes, seconds]

  5. Computation's Dual Identity "variables as containers" 42 41 name: y type:int LOC:304 name: x type:int LOC:300 memory location 300 memory location 304 Computation Data Storage accessed through functions…

  6. Comparing disciplines… def g(x): return x**100 g(x) = x100 CS googlizer Mathematical googlizer defined by what it does defined by what it is and how efficiently it does it procedural structural

  7. How functions look… input(s) name defconvertFromSeconds(s): # total seconds """ convertFromSectons(s): Converts an integer # of seconds into a list of [days, hours, minutes, seconds] input s: an int """ seconds = s % 60 # leftover seconds s = (s / 60) # total minutes minutes = s % 60 # leftover minutes s = s / 60 # total hours hours = s % 24 # leftover hours days = s / 24 # total days return [days, hours, minutes, seconds] docstring code block comments return statement

  8. How functions work… Three functions: What is >>> demo(-4) ? def demo(x): return x + f(x) def f(x): return 11*g(x) + g(x/2) def g(x): return -1 * x Quiz I have a guess… Name(s): The graders have asked - very politely - that names be reasonably legible… Thank you!

  9. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) def g(x): return -1 * x >>> demo(-4) ?

  10. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11*g(x) + g(x/2) >>> demo(-4) ?

  11. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11*g(x) + g(x/2) These are different x's ! >>> demo(-4) ?

  12. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11*g(-4) + g(-4/2) g x = -4 >>> demo(-4) ? return -1.0 * x

  13. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11* 4 + g(-4/2) g x = -4 >>> demo(-4) ? return -1 * -4 4

  14. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1.0 * x return 11* 4 + g(-4/2) >>> demo(-4) ?

  15. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11* 4 + g(-4/2) g x = -2 >>> demo(-4) ? return -1 * -2 2

  16. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1.0 * x return 11* 4 + 2 >>> demo(-4) ?

  17. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1.0 * x 46 return 11* 4 + 2 >>> demo(-4) ?

  18. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + 46 def f(x): return 11*g(x) + g(x/2) def g(x): return -1.0 * x 42 >>> demo(-4) 42

  19. Douglas Adams's 42 puzzle answer: 42 question: unknown

  20. Function stacking def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11* 4 + g(-4/2) g x = -2 >>> demo(-4) return -1 * -2 2 42 "The stack" (1) remembers separate functions' values for variables… (2) remembers where to send results back to…

  21. Function design

  22. Thinking sequentially factorial 5! = 120 5! = 5 * 4 * 3 * 2 * 1 N! = N * (N-1) * (N-2) * … * 3 * 2 * 1

  23. Thinking recursively Recursion == self-reference! factorial 5! = 120 5! = 5 * 4 * 3 * 2 * 1 5! = N! = N * (N-1) * (N-2) * … * 3 * 2 * 1 N! =

  24. Warning This is legal code! deffac(N): return N * fac(N-1) I wonder how this code will STACK up!?

  25. Warning deffac(N): return N * fac(N-1) No base case -- the calls to fac will never stop! Make sure you have a base case, then worry about the recursive step...

  26. deffac(N): returnfac(N) Roadsigns and recursion examples of self-fulfilling danger

  27. Thinking recursively ! deffac(N): if N <= 1: return 1 Base Case

  28. Thinking recursively ! deffac(N): if N <= 1: return 1 else: return N * fac(N-1) Base Case Recursive Step Human: Base case and 1 step Computer: Everything else

  29. Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) >>> fac(1) Result: 1 The base case is No Problem!

  30. Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5)

  31. Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4)

  32. Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) 4 * fac(3)

  33. Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2)

  34. Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2) 2 * fac(1)

  35. Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) "The Stack" 5 * fac(4) 4 * fac(3) 3 * fac(2) Remembers all of the individual calls to fac 2 * fac(1) 1

  36. Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2) 2 * 1

  37. Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) 4 * fac(3) 3 * 2

  38. Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) 4 * 6

  39. Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * 24

  40. Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) Result: 120 0 N*** -> X 1 0 x*** -> N 0 Base Case Look familiar? Recursive Step

  41. Let recursion do the work for you.

  42. Let recursion do the work for you. Exploit self-similarity Less work ! Produce short, elegant code deffac(N): if N <= 1: return 1 else: return N * fac(N-1) You handle the base case – the easiest possible case to think of! Recursion does almost all of the rest of the problem!

  43. But you do need to do one step yourself… deffac(N): if N <= 1: return 1 else: returnfac(N) You handle the base case – the easiest possible case to think of! This will not work

  44. Breaking Up… …is easy to do with Python s = "This has 2 T's" How do we get at the initial character of s? How do we get at ALL THE REST of s? L = [ 42, 21, 7 ] How do we get at the initial element of L? How do we get at ALL the REST of L?

  45. Recursion Examples def mylen(s): """ input: any string, s output: the number of characters in s """ if: else: base case test!

  46. Recursion Examples def mylen(s): """ input: any string, s output: the number of characters in s """ ifs =='': return0 else: rest = s[1:] len_of_rest = mylen( rest ) total_len = 1 + len_of_rest returntotal_len

  47. Recursion Examples def mylen(s): """ input: any string, s output: the number of characters in s """ ifs =='': return0 else: rest = s[1:] len_of_rest = mylen( rest ) return1 + len_of_rest

  48. Recursion Examples def mylen(s): """ input: any string, s output: the number of characters in s """ ifs =='': return0 else: rest = s[1:] return1 + mylen( rest )

  49. Recursion Examples def mylen(s): """ input: any string, s output: the number of characters in s """ ifs =='': return0 else: return1 + mylen(s[1:]) There's not much len left here!

  50. Behind the curtain… mylen('cs5')

More Related