CS 61A Discussion 5 Trees, Mutation, Box and Pointers, Nonlocal - - PowerPoint PPT Presentation

cs 61a discussion 5
SMART_READER_LITE
LIVE PREVIEW

CS 61A Discussion 5 Trees, Mutation, Box and Pointers, Nonlocal - - PowerPoint PPT Presentation

CS 61A Discussion 5 Trees, Mutation, Box and Pointers, Nonlocal Albert Xu Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/ Announcements Trees An anatomical perspective Nodes Branch Root 1 Labels 2 3 6 7 4


slide-1
SLIDE 1

CS 61A Discussion 5

Trees, Mutation, Box and Pointers, Nonlocal

Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/

Albert Xu

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

An anatomical perspective

Trees

1 2 3 4 5 6 7 8 9

Root Leaves Branch Labels Nodes

Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

slide-4
SLIDE 4

which of these, if any, is a tree?

What is a Tree?

1 2 3 4 5 6 7 8 9

Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

slide-5
SLIDE 5

which of these, if any, is a tree?

What is a Tree?

1 2 3 4 5 6 7 8 9

Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

slide-6
SLIDE 6

which of these, if any, is a tree?

What is a Tree?

1 2 3 4 5 6 7 8 9

Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

slide-7
SLIDE 7

which of these, if any, is a tree?

What is a Tree?

1 2 3 4 5 6 7 8 9

Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

slide-8
SLIDE 8

which of these, if any, is a tree?

What is a Tree?

1 2 3 4 5 6 7 8 9

Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

slide-9
SLIDE 9

which of these, if any, is a tree?

What is a Tree?

1 2 3 4 5 6 7 8 9

Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

slide-10
SLIDE 10

which of these, if any, is a tree?

What is a Tree?

1 2 3 4 5 6 7 8 9

Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

slide-11
SLIDE 11

which of these, if any, is a tree?

What is a Tree?

1 2 3 4 5 6 7 8 9

Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

slide-12
SLIDE 12

which of these, if any, is a tree?

What is a Tree?

1 2 3 4 5 6 7 8 9

Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

slide-13
SLIDE 13

which of these, if any, is a tree?

What is a Tree?

1 2 3 4 5 6 7 8 9

Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

slide-14
SLIDE 14
  • A tree consists of a label, along with a list of branches,

each of which are themselves trees.

  • This is a recursive definition, can you see why?
  • Leaves are trees too! They just don’t have any branches
  • f their own.

Trees - Designing an ADT

2 4 5

slide-15
SLIDE 15
  • A tree consists of a label, along with a list of branches,

each of which are themselves trees.

  • This is a recursive definition, can you see why?
  • Leaves are trees too! They just don’t have any branches
  • f their own.

Trees - Designing an ADT

2 4 5

Label

slide-16
SLIDE 16
  • A tree consists of a label, along with a list of branches,

each of which are themselves trees.

  • This is a recursive definition, can you see why?
  • Leaves are trees too! They just don’t have any branches
  • f their own.

Trees - Designing an ADT

2 4 5

Label branch 0

slide-17
SLIDE 17
  • A tree consists of a label, along with a list of branches,

each of which are themselves trees.

  • This is a recursive definition, can you see why?
  • Leaves are trees too! They just don’t have any branches
  • f their own.

Trees - Designing an ADT

2 4 5

Label branch 0 branch 1

slide-18
SLIDE 18
  • A tree consists of a label, along with a list of branches,

each of which are themselves trees.

  • This is a recursive definition, can you see why?
  • Leaves are trees too! They just don’t have any branches
  • f their own.

Trees - Designing an ADT

2 4 5

Label branches[0] branches[1]

slide-19
SLIDE 19

Trees - Designing an ADT

# Constructor def tree(label, branches=[]): for branch in branches: assert is_tree(branch) return [label] + list(branches) # Selectors def label(tree): return tree[0] def branches(tree): return tree[1:] def is_leaf(tree): return not branches(tree)

slide-20
SLIDE 20

Box and Pointer Diagrams

how do you draw lists on environment diagrams?

slide-21
SLIDE 21

Box and Pointer Diagrams

how do you draw lists on environment diagrams?

slide-22
SLIDE 22

Box and Pointer Diagrams

how do you draw lists on environment diagrams?

slide-23
SLIDE 23

Box and Pointer Diagrams

how do you draw lists on environment diagrams?

slide-24
SLIDE 24

Box and Pointer Diagrams

how do you draw lists on environment diagrams?

slide-25
SLIDE 25

Examining List Slicing

Remember list slicing?

<list>[<start>:<stop>] - returns the part of a list between <start>, inclusive, and <stop>, non-inclusive

slide-26
SLIDE 26

Remember list slicing?

<list>[<start>:<stop>] - returns the part of a list between <start>, inclusive, and <stop>, non-inclusive

If I wrote this:

>>> a = [1,2,3] >>> b = a[2:]

Examining List Slicing

slide-27
SLIDE 27

Remember list slicing?

<list>[<start>:<stop>] - returns the part of a list between <start>, inclusive, and <stop>, non-inclusive

If I wrote this:

>>> a = [1,2,3] >>> b = a[2:]

Question: b is the result of a list slice of a. Did this slice mutate a or create a new list?

Examining List Slicing

slide-28
SLIDE 28

Remember list slicing?

<list>[<start>:<stop>] - returns the part of a list between <start>, inclusive, and <stop>, non-inclusive

If I wrote this:

>>> a = [1,2,3] >>> b = a[2:]

Question: b is the result of a list slice of a. Did this slice mutate a or create a new list?

it created a new list - we’re trying to slice out part

  • f an old list, but we don’t want to be changing

that old list!

Examining List Slicing

slide-29
SLIDE 29

List Mutation

the central question - does it mutate or create a new list?

slide-30
SLIDE 30

List Mutation

the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list?

slide-31
SLIDE 31

List Mutation

the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list?

  • lst = lst[a:b] -
slide-32
SLIDE 32

List Mutation

the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list?

  • lst = lst[a:b] - a new list, as we said earlier.
slide-33
SLIDE 33

List Mutation

the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list?

  • lst = lst[a:b] - a new list, as we said earlier.
  • lst = lst + lst2 -
slide-34
SLIDE 34

List Mutation

the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list?

  • lst = lst[a:b] - a new list, as we said earlier.
  • lst = lst + lst2 - a new list. Adding two lists shouldn’t

change the originals, it should make a third(new) one!

slide-35
SLIDE 35

List Mutation

the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list?

  • lst = lst[a:b] - a new list, as we said earlier.
  • lst = lst + lst2 - a new list. Adding two lists shouldn’t

change the originals, it should make a third(new) one!

  • lst += lst2 -
slide-36
SLIDE 36

List Mutation

the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list?

  • lst = lst[a:b] - a new list, as we said earlier.
  • lst = lst + lst2 - a new list. Adding two lists shouldn’t

change the originals, it should make a third(new) one!

  • lst += lst2 - the same list. This is slightly different, and tricky!

This syntax implies that we’re trying to add a second list to the first

  • ne, so Python just sticks it onto the end of the old list.
slide-37
SLIDE 37

List Mutation

the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list?

  • lst = lst[a:b] - a new list, as we said earlier.
  • lst = lst + lst2 - a new list. Adding two lists shouldn’t

change the originals, it should make a third(new) one!

  • lst += lst2 - the same list. This is slightly different, and tricky!

This syntax implies that we’re trying to add a second list to the first

  • ne, so Python just sticks it onto the end of the old list.
  • lst = list(lst) -
slide-38
SLIDE 38

List Mutation

the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list?

  • lst = lst[a:b] - a new list, as we said earlier.
  • lst = lst + lst2 - a new list. Adding two lists shouldn’t

change the originals, it should make a third(new) one!

  • lst += lst2 - the same list. This is slightly different, and tricky!

This syntax implies that we’re trying to add a second list to the first

  • ne, so Python just sticks it onto the end of the old list.
  • lst = list(lst) - a new list. This is specifically because list

takes whatever iterable is given and sticks each element into a new list, even if the original iterable was already a list.

slide-39
SLIDE 39

Methods that Mutate

some more methods that add to a list

slide-40
SLIDE 40

Methods that Mutate

some more methods that add to a list

  • lst.append(elem)
  • Adds elem to the end of lst
  • Increases list length by ONE always
slide-41
SLIDE 41

Methods that Mutate

some more methods that add to a list

  • lst.append(elem)
  • Adds elem to the end of lst
  • Increases list length by ONE always
  • lst.extend(seq)
  • Adds elements in seq to the end of lst
slide-42
SLIDE 42

Methods that Mutate

some more methods that add to a list

  • lst.append(elem)
  • Adds elem to the end of lst
  • Increases list length by ONE always
  • lst.extend(seq)
  • Adds elements in seq to the end of lst
  • lst.insert(i, elem)
  • Inserts elem into lst at index i
  • The rest of the elements get pushed rightwards
slide-43
SLIDE 43

Methods that Mutate

some more methods that add to a list

  • lst.append(elem)
  • Adds elem to the end of lst
  • Increases list length by ONE always
  • lst.extend(seq)
  • Adds elements in seq to the end of lst
  • lst.insert(i, elem)
  • Inserts elem into lst at index i
  • The rest of the elements get pushed rightwards

*All of these return None!

slide-44
SLIDE 44

Methods that Mutate

some more methods that remove from a list

slide-45
SLIDE 45

Methods that Mutate

some more methods that remove from a list

  • lst.remove(elem)
  • Removes the first instance of elem in lst
  • Throws ValueError if elem is not in lst
  • Returns None
slide-46
SLIDE 46

Methods that Mutate

some more methods that remove from a list

  • lst.remove(elem)
  • Removes the first instance of elem in lst
  • Throws ValueError if elem is not in lst
  • Returns None
  • lst.pop()/lst.pop(i)
  • In the first case, removes the last element of lst
  • In the second case, removes the ith element from lst
  • Returns the removed element!
slide-47
SLIDE 47

Nonlocal

changing some of your programming paradigms

  • remember how we said that each frame has its own set of variables?
slide-48
SLIDE 48

Nonlocal

changing some of your programming paradigms

  • remember how we said that each frame has its own set of variables?
  • if a variable doesn’t exist in the current frame(say, f2), you can look

up its value from a parent frame(say, f1)

slide-49
SLIDE 49

Nonlocal

changing some of your programming paradigms

  • remember how we said that each frame has its own set of variables?
  • if a variable doesn’t exist in the current frame(say, f2), you can look

up its value from a parent frame(say, f1)

  • …but it’s impossible for you to change the value of that parent

frame’s(f1’s) variable from inside f2!

slide-50
SLIDE 50

Nonlocal

changing some of your programming paradigms

  • remember how we said that each frame has its own set of variables?
  • if a variable doesn’t exist in the current frame(say, f2), you can look

up its value from a parent frame(say, f1)

  • …but it’s impossible for you to change the value of that parent

frame’s(f1’s) variable from inside f2!

not anymore!

slide-51
SLIDE 51

Nonlocal

changing some of your programming paradigms

  • remember how we said that each frame has its own set of variables?
  • if a variable doesn’t exist in the current frame(say, f2), you can look

up its value from a parent frame(say, f1)

  • …but it’s impossible for you to change the value of that parent

frame’s(f1’s) variable from inside f2!

not anymore! nonlocal x

slide-52
SLIDE 52

Nonlocal

changing some of your programming paradigms

  • remember how we said that each frame has its own set of variables?
  • if a variable doesn’t exist in the current frame(say, f2), you can look

up its value from a parent frame(say, f1)

  • …but it’s impossible for you to change the value of that parent

frame’s(f1’s) variable from inside f2!

not anymore! nonlocal x

this line, when run inside f2, says that every time we modify x inside the current frame(f2), instead modify f1’s x! Same thing with looking up x.

slide-53
SLIDE 53

Nonlocal Demo

demo!

slide-54
SLIDE 54

Pitfalls of Nonlocal

there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well

slide-55
SLIDE 55

Pitfalls of Nonlocal

there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well

Exhibit A

slide-56
SLIDE 56

Pitfalls of Nonlocal

there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well

Exhibit A

attempting to nonlocal a variable that already exists in the current frame

slide-57
SLIDE 57

Pitfalls of Nonlocal

there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well

Exhibit A

I L L E G A L ! ! !

attempting to nonlocal a variable that already exists in the current frame

slide-58
SLIDE 58

Pitfalls of Nonlocal

there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well

Exhibit A Exhibit B

I L L E G A L ! ! !

attempting to nonlocal a variable that already exists in the current frame

slide-59
SLIDE 59

Pitfalls of Nonlocal

there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well

Exhibit A Exhibit B

attempting to nonlocal a variable from the global frame

I L L E G A L ! ! !

attempting to nonlocal a variable that already exists in the current frame

slide-60
SLIDE 60

Pitfalls of Nonlocal

there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well

Exhibit A

attempting to nonlocal a variable that already exists in the current frame

Exhibit B

I L L E G A L ! ! ! ILLEGAL!!!

attempting to nonlocal a variable from the global frame

slide-61
SLIDE 61

UnboundLocalError

this is probably a good time to talk about it

*i borrowed this incorrect code from StackOverflow lol

slide-62
SLIDE 62

UnboundLocalError

this is probably a good time to talk about it here is code which throws an UnboundLocalError

*i borrowed this incorrect code from StackOverflow lol

slide-63
SLIDE 63

UnboundLocalError

this is probably a good time to talk about it here is code which throws an UnboundLocalError

*i borrowed this incorrect code from StackOverflow lol

…which is equivalent to

slide-64
SLIDE 64

UnboundLocalError

this is probably a good time to talk about it here is code which throws an UnboundLocalError

*i borrowed this incorrect code from StackOverflow lol

…which is equivalent to

Python has no variable declarations, so it has to guess the scope of your variables! Because you assigned a value of x inside the increment function, it assumes that x is a variable inside the increment frame. But by the order in which we evaluate the RHS of assignment before the left, we look up x before it even exists in the frame!

slide-65
SLIDE 65

UnboundLocalError

this is probably a good time to talk about it here is code which throws an UnboundLocalError

*i borrowed this incorrect code from StackOverflow lol

…which is equivalent to

Python has no variable declarations, so it has to guess the scope of your variables! Because you assigned a value of x inside the increment function, it assumes that x is a variable inside the increment frame. But by the order in which we evaluate the RHS of assignment before the left, we look up x before it even exists in the frame!

this is an analysis of the issue, but it’s not the root cause. How could anonymous SO user easily fix this?

slide-66
SLIDE 66

UnboundLocalError

this is probably a good time to talk about it here is code which throws an UnboundLocalError

*i borrowed this incorrect code from StackOverflow lol

…which is equivalent to

Python has no variable declarations, so it has to guess the scope of your variables! Because you assigned a value of x inside the increment function, it assumes that x is a variable inside the increment frame. But by the order in which we evaluate the RHS of assignment before the left, we look up x before it even exists in the frame!

this is an analysis of the issue, but it’s not the root cause. How could anonymous SO user easily fix this?

trick question!!

slide-67
SLIDE 67

UnboundLocalError

this is probably a good time to talk about it code that errors.

*i borrowed this incorrect code from StackOverflow lol

slide-68
SLIDE 68

UnboundLocalError

this is probably a good time to talk about it code that errors.

*i borrowed this incorrect code from StackOverflow lol

slide-69
SLIDE 69

UnboundLocalError

this is probably a good time to talk about it code that errors.

*i borrowed this incorrect code from StackOverflow lol

Also errors!! remember? ILLEGAL!!!

slide-70
SLIDE 70

UnboundLocalError

this is probably a good time to talk about it code that errors.

*i borrowed this incorrect code from StackOverflow lol

Also errors!! remember? ILLEGAL!!! this one works!

slide-71
SLIDE 71

UnboundLocalError

this is probably a good time to talk about it code that errors.

*i borrowed this incorrect code from StackOverflow lol

Also errors!! remember? ILLEGAL!!! this one works! unfortunately you don’t necessarily learn the global keyword in this class, just fyi!

slide-72
SLIDE 72

Thanks for coming.

Have a great rest of your week! :)

Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/