section 3 attendance (no password today) - - PowerPoint PPT Presentation

section 3
SMART_READER_LITE
LIVE PREVIEW

section 3 attendance (no password today) - - PowerPoint PPT Presentation

CS61A section 3 attendance (no password today) http://links.cs61a.org/jasonxu upcoming hw 3 hog contest ~ optional CS61A my thoughts definitely di ffi cult midterm recovery points MT1 12% CS61A my thoughts 10% 20% +3% 25%


slide-1
SLIDE 1 CS61A

section 3

attendance (no password today) upcoming http://links.cs61a.org/jasonxu hw 3 hog contest ~ optional
slide-2
SLIDE 2 CS61A

my thoughts

🤭

definitely difficult midterm recovery points

slide-3
SLIDE 3 CS61A

my thoughts

🤭

definitely difficult

12% 25% Projects 33% 20% MT1 10%

+3%

61A has a lot of resources

slide-4
SLIDE 4

recursion

CS61A things defined by themselves
slide-5
SLIDE 5

recursion

CS61A factorial! def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1)

5! = 5 * 4! 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1 * 0!

slide-6
SLIDE 6

recursion

CS61A factorial! def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1)

5! = 5 * 4! 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1 * 0! 0! = 0 * -1!

uhhhhhhhhh

  • 1! = -1 * -2! …

when do i stop?

slide-7
SLIDE 7

recursion

CS61A factorial! def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1)

5! = 5 * 4! 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1 * 0! 0! = 0 * -1!

uhhhhhhhhh

  • 1! = -1 * -2! …

base case!

slide-8
SLIDE 8

recursion

CS61A factorial! def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1)

5! = 5 * 4! 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1 * 0! 0! = 1

yay! 😎

base case!

slide-9
SLIDE 9

recursion

CS61A factorial! def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1)

how do we come up with this

by definition, 😲 by assuming it works, 🤫

pattern: how can we solve sub-problems to solve the current problem?
slide-10
SLIDE 10

recursion

CS61A factorial!

how do we calculate 5!

5! = 5 * 4! for this to be true, don’t we have to assume that ‘!’ really does what it says well in code we can’t name a function ‘!’

def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1)

we assume that (n-1)! works recursive leap of faith well… i have to test it by tracing it well… big headache

slide-11
SLIDE 11

recursion

CS61A strategy

if you capture all the base cases you can assume it works so you can create the recursive call

slide-12
SLIDE 12

recursion

CS61A analogy

black friday shopping… long line you want to know how many people in front accurately, you only know if you’re the first person

  • therwise, you have to ask the person in

front of you for their position is this a good recursive procedure…?

slide-13
SLIDE 13

recursion

CS61A analogy

black friday shopping… long line you want to know how many people in front accurately, you only know if you’re the first person

  • therwise, you have to ask the person in

front of you for their position and add 1 is this a recursive procedure…?

slide-14
SLIDE 14

recursion

CS61A motivation for it
  • peration
input
slide-15
SLIDE 15

recursion

CS61A things defined by themselves
  • peration
input
  • utput
slide-16
SLIDE 16

recursion

CS61A things defined by themselves
  • peration
input
  • utput :)
slide-17
SLIDE 17

recursion

CS61A things defined by themselves
  • peration
input
  • utput :)

tell me the number of ways to line $26

slide-18
SLIDE 18

recursion

CS61A things defined by themselves
  • peration
input = 26 count :)

?

tell me the number of ways to line $26

slide-19
SLIDE 19

if 0: +1

i have to figure out how to get $26 if i use $1 as my first denomination i have to figure out how to get the $25 . . . if i use $20 as my first denomination i have to figure out how to get the $6

if 0: +1

if i use $1 as my first denomination i have to figure out how to get the $24 . . . the blue boxes are operations!
slide-20
SLIDE 20

if 0: +1

i have to figure out how to get $26 if i use $1 as my first denomination i have to figure out how to get the $25 . . . if i use $20 as my first denomination i have to figure out how to get the $6

if 0: +1

if i use $1 as my first denomination i have to figure out how to get the $24 . . .
  • utput
slide-21
SLIDE 21

if 0: +1

i have to figure out how to get $26 if i use $1 as my first denomination i have to figure out how to get the $25 . . . if i use $20 as my first denomination i have to figure out how to get the $6

if 0: +1

if i use $1 as my first denomination i have to figure out how to get the $24 . . .
  • utput :(
slide-22
SLIDE 22 def count(n): total = 0
  • ptions = [n]
while len(options) > 0: curr = options.pop(0) for change in [1, 5, 10, 20]: val = curr - change if val == 0: total += 1 elif val > 0:
  • ptions.append(val)
return total
slide-23
SLIDE 23

🙄

def count(n): total = 0
  • ptions = [n]
while len(options) > 0: curr = options.pop(0) for change in [1, 5, 10, 20]: val = curr - change if val == 0: total += 1 elif val > 0:
  • ptions.append(val)
return total
slide-24
SLIDE 24

recursion

CS61A things defined by themselves
  • peration
input = 26 count :)

?

slide-25
SLIDE 25

recursion

CS61A things defined by themselves

function

input = 26 count :)

?

slide-26
SLIDE 26

recursion

CS61A things defined by themselves

if 0: +1

i have to figure out how to get $26 if i use $1 as my first denomination i have to figure out how to get the $25 . . . if i use $20 as my first denomination i have to figure out how to get the $6 the blue box is a function!
slide-27
SLIDE 27

recursion

CS61A things defined by themselves

if 0: +1

i have to figure out how to get $26 if i use $1 as my first denomination i have to figure out how to get the $25 . . . if i use $20 as my first denomination i have to figure out how to get the $6 recursive leap of faith
slide-28
SLIDE 28

recursion

CS61A things defined by themselves

if 0: +1

i have to figure out how to get $26 if i use $1 as my first denomination i have to figure out how to get the $25 . . . if i use $20 as my first denomination i have to figure out how to get the $6 recursive leap of faith math (out of scope) any recursive problem you get is recursively possible
slide-29
SLIDE 29

recursion

CS61A things defined by themselves

if 0: +1 if < 0: +0

i have to figure out how to get $26 if i use $1 as my first denomination i have to figure out how to get the $25 . . . if i use $20 as my first denomination i have to figure out how to get the $6 def count_recurse(n): if n < 0: return 0 elif n == 0: return 1 else: return count_recurse(n - 1) + count_recurse(n - 5) + count_recurse(n - 10) + count_recurse(n - 20) count
slide-30
SLIDE 30 def count(n): total = 0
  • ptions = [n]
while len(options) > 0: curr = options.pop(0) for change in [1, 5, 10, 20]: val = curr - change if val == 0: total += 1 elif val > 0:
  • ptions.append(val)
return total def count_recurse(n): if n < 0: return 0 elif n == 0: return 1 else: return count_recurse(n - 1) + count_recurse(n - 5) + count_recurse(n - 10) + count_recurse(n - 20)
slide-31
SLIDE 31 def count(n): total = 0
  • ptions = [n]
while len(options) > 0: curr = options.pop(0) for change in [1, 5, 10, 20]: val = curr - change if val == 0: total += 1 elif val > 0:
  • ptions.append(val)
return total def count_recurse(n): if n < 0: return 0 elif n == 0: return 1 else: return count_recurse(n - 1) + count_recurse(n - 5) + count_recurse(n - 10) + count_recurse(n - 20)

function function

slide-32
SLIDE 32 def count_recurse(n): if n < 0: return 0 elif n == 0: return 1 else: return count_recurse(n - 1) + count_recurse(n - 5) + count_recurse(n - 10) + count_recurse(n - 20)

def count(n): total = 0
  • ptions = [n]
while len(options) > 0: curr = options.pop(0) for change in [1, 5, 10, 20]: val = curr - change if val == 0: total += 1 elif val > 0:
  • ptions.append(val)
return total
slide-33
SLIDE 33

so what does this mean

we have a strategy on how to create recursive functions we can see that recursion isn’t pointless… at least for more complex problems

slide-34
SLIDE 34

recursion

CS61A things defined by themselves def count_recurse(n): if n < 0: return 0 elif n == 0: return 1 else: return count_recurse(n - 1) + count_recurse(n - 5) + count_recurse(n - 10) + count_recurse(n - 20)

$26 $25 $16 $21 $6

subproblem
slide-35
SLIDE 35

recursion

CS61A things defined by themselves

1.set up rules (base cases) 2.assume it works

slide-36
SLIDE 36

recursion

CS61A

🤰 what does this mean