730 likes | 846 Views
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.
E N D
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!
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!
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
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]
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…
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
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
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!
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) ?
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) ?
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) ?
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
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
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) ?
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
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) ?
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) ?
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
Douglas Adams's 42 puzzle answer: 42 question: unknown
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…
Thinking sequentially factorial 5! = 120 5! = 5 * 4 * 3 * 2 * 1 N! = N * (N-1) * (N-2) * … * 3 * 2 * 1
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! =
Warning This is legal code! deffac(N): return N * fac(N-1) I wonder how this code will STACK up!?
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...
deffac(N): returnfac(N) Roadsigns and recursion examples of self-fulfilling danger
Thinking recursively ! deffac(N): if N <= 1: return 1 Base Case
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
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!
Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5)
Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4)
Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) 4 * fac(3)
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)
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)
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
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
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
Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) 4 * 6
Behind the curtain… deffac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * 24
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
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!
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
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?
Recursion Examples def mylen(s): """ input: any string, s output: the number of characters in s """ if: else: base case test!
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
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
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 )
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!
Behind the curtain… mylen('cs5')