CS 61A Discussion 7 Iterators/Generators & Linked Lists Albert - - PowerPoint PPT Presentation

cs 61a discussion 7
SMART_READER_LITE
LIVE PREVIEW

CS 61A Discussion 7 Iterators/Generators & Linked Lists Albert - - PowerPoint PPT Presentation

CS 61A Discussion 7 Iterators/Generators & Linked Lists Albert Xu Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/ Announcements Linked Lists a new type of list regular lists look 1 4 2 3 something like


slide-1
SLIDE 1

CS 61A Discussion 7

Iterators/Generators & Linked Lists

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

Albert Xu

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Linked Lists

a new type of list

1 4 2 3

regular lists look something like this

slide-4
SLIDE 4

Linked Lists

a new type of list

1 1 4 2 3

regular lists look something like this

4 2 3

the same list as a linked list

slide-5
SLIDE 5

Linked Lists

a new type of list

1 1 4 2 3

regular lists look something like this

4 2 3

the same list as a linked list

Each element of the linked list is like a link in a chain, and contains a single element of the linked list. It also tells us where to find the next element in the linked list!

slide-6
SLIDE 6

Linked Lists

a new type of list

1 1 4 2 3

regular lists look something like this

4 2 3

the same list as a linked list by the way, why do linked lists exist?

Each element of the linked list is like a link in a chain, and contains a single element of the linked list. It also tells us where to find the next element in the linked list!

slide-7
SLIDE 7

Linked Lists

a new type of list

1 1 4 2 3

regular lists look something like this

4 2 3

the same list as a linked list by the way, why do linked lists exist? it’s fast to add/delete elements!

Each element of the linked list is like a link in a chain, and contains a single element of the linked list. It also tells us where to find the next element in the linked list!

slide-8
SLIDE 8

Link Class

under the hood

1 4 2 3

first rest

slide-9
SLIDE 9

Link Class

under the hood

1 4 2 3

an instance of the Link class

first rest

slide-10
SLIDE 10

Link Class

under the hood

1 4 2 3

an instance of the Link class

  • Each instance of the Link class has a first, which stores the

element,

first rest

slide-11
SLIDE 11

Link Class

under the hood

1 4 2 3

an instance of the Link class

  • Each instance of the Link class has a first, which stores the

element,

  • …and rest attribute, which stores a pointer to the next Link.

first rest

slide-12
SLIDE 12

Link Class

under the hood

1 4 2 3

an instance of the Link class

  • Each instance of the Link class has a first, which stores the

element,

  • …and rest attribute, which stores a pointer to the next Link.
  • Finally, the last link in the list has a rest attribute which points

to an object called Link.empty!

first rest

slide-13
SLIDE 13

Iterators, Iterables

iterable iterator

  • h my
slide-14
SLIDE 14

Iterators, Iterables

  • h my

iterator iterable

slide-15
SLIDE 15

Iterators, Iterables

  • h my

iterator iterable

iter as in iteration!

slide-16
SLIDE 16

Iterators, Iterables

iterator

  • h my

both iterators and iterables implement the __iter__ method, meaning that given an instance x of an iterator or iterable, calling iter(x) will give you a new iterator over x!

iterable

iter as in iteration!

slide-17
SLIDE 17

Iterators, Iterables

iterator

  • h my

iterable

implying that it is ABLE to be iterated over!

slide-18
SLIDE 18

Iterators, Iterables

iterator

  • h my

an iterable is any object, sequence or not, that can be iterated over! Examples include lists, tuples, sets, strings, and dictionaries!

iterable

implying that it is ABLE to be iterated over!

slide-19
SLIDE 19

Iterators, Iterables

iterable iterator

  • h my

implying that it is ABLE to be iterated over!

an iterable is any object, sequence or not, that can be iterated over! Examples include lists, tuples, sets, strings, and dictionaries!

this is the thing DOING the iteration!

slide-20
SLIDE 20

Iterators, Iterables

iterable iterator

  • h my

implying that it is ABLE to be iterated over!

an iterable is any object, sequence or not, that can be iterated over! Examples include lists, tuples, sets, strings, and dictionaries!

this is the thing DOING the iteration!

an iterator is an object that lets you get the next element of a sequence repeatedly until no more elements exist.

slide-21
SLIDE 21

A Good Comparison

*borrowed from Kevin T. Li

slide-22
SLIDE 22

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a)

slide-23
SLIDE 23

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter)

slide-24
SLIDE 24

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1

slide-25
SLIDE 25

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter)

slide-26
SLIDE 26

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2

slide-27
SLIDE 27

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter)

slide-28
SLIDE 28

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a)

slide-29
SLIDE 29

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter)

slide-30
SLIDE 30

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3

slide-31
SLIDE 31

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter)

slide-32
SLIDE 32

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration

slide-33
SLIDE 33

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter)

slide-34
SLIDE 34

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration

slide-35
SLIDE 35

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter)

slide-36
SLIDE 36

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter) 1

slide-37
SLIDE 37

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter) 1 >>> a

slide-38
SLIDE 38

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter) 1 >>> a [1, 2, 3]

slide-39
SLIDE 39

Generators

how do you write your own iterators?

slide-40
SLIDE 40

Generators

how do you write your own iterators?

Using a list

slide-41
SLIDE 41

Generators

how do you write your own iterators?

Using a list

>>> fibs = [1, 1, 2, 3, 5, 8] >>> fib_iter = iter(fibs)

slide-42
SLIDE 42

Generators

how do you write your own iterators?

Using a list

>>> fibs = [1, 1, 2, 3, 5, 8] >>> fib_iter = iter(fibs)

Two issues: 1) I have to calculate out values of fib myself 2) I can only make a finite iterator

slide-43
SLIDE 43

Generators

how do you write your own iterators?

Using a list

>>> fibs = [1, 1, 2, 3, 5, 8] >>> fib_iter = iter(fibs)

Two issues: 1) I have to calculate out values of fib myself 2) I can only make a finite iterator

Using a generator

slide-44
SLIDE 44

Generators

how do you write your own iterators?

Using a list

>>> fibs = [1, 1, 2, 3, 5, 8] >>> fib_iter = iter(fibs)

Two issues: 1) I have to calculate out values of fib myself 2) I can only make a finite iterator

Using a generator

>>> def fibs(): prev = 0 current = 1 while True: yield current current = current + prev prev = current

slide-45
SLIDE 45

Generators

how do you write your own iterators?

Using a list

>>> fibs = [1, 1, 2, 3, 5, 8] >>> fib_iter = iter(fibs)

Two issues: 1) I have to calculate out values of fib myself 2) I can only make a finite iterator

Using a generator

>>> def fibs(): prev = 0 current = 1 while True: yield current current = current + prev prev = current >>> fib_iter = fibs()

slide-46
SLIDE 46

Generators

how do you write your own iterators?

Using a list

>>> fibs = [1, 1, 2, 3, 5, 8] >>> fib_iter = iter(fibs)

Two issues: 1) I have to calculate out values of fib myself 2) I can only make a finite iterator

Using a generator

>>> def fibs(): prev = 0 current = 1 while True: yield current current = current + prev prev = current >>> fib_iter = fibs()

generator function

slide-47
SLIDE 47

Generators

how do you write your own iterators?

Using a list

>>> fibs = [1, 1, 2, 3, 5, 8] >>> fib_iter = iter(fibs)

Two issues: 1) I have to calculate out values of fib myself 2) I can only make a finite iterator

Using a generator

>>> def fibs(): prev = 0 current = 1 while True: yield current current = current + prev prev = current >>> fib_iter = fibs()

generator function generator object (generator)

slide-48
SLIDE 48

Everything Takes Time

slide-49
SLIDE 49

Everything Takes Time

given the choice, we always want our programs to run faster

slide-50
SLIDE 50

Everything Takes Time

but some things are inherently slower than others… given the choice, we always want our programs to run faster

slide-51
SLIDE 51

Describing Runtime

Each operation(arithmetic, printing, changing variables) in a function takes

  • time. To figure out how long a program

takes, we could count all these

  • perations up.
slide-52
SLIDE 52

Each operation(arithmetic, printing, changing variables) in a function takes

  • time. To figure out how long a program

takes, we could count all these

  • perations up.

how many operations?

Describing Runtime

slide-53
SLIDE 53

Each operation(arithmetic, printing, changing variables) in a function takes

  • time. To figure out how long a program

takes, we could count all these

  • perations up.

But given larger and larger input sizes, functions will take longer. The question we are curious about is: How long does a function take, asymptotically, as input size gets really large? how many operations?

Describing Runtime

slide-54
SLIDE 54

Describing Runtime

But given larger and larger input sizes, functions will take longer. The question we are curious about is: How long does a function take, asymptotically, as input size gets really large?

slide-55
SLIDE 55

But given larger and larger input sizes, functions will take longer. The question we are curious about is: How long does a function take, asymptotically, as input size gets really large?

Describing Runtime

slide-56
SLIDE 56

But given larger and larger input sizes, functions will take longer. The question we are curious about is: How long does a function take, asymptotically, as input size gets really large?

Describing Runtime

slide-57
SLIDE 57

But given larger and larger input sizes, functions will take longer. The question we are curious about is: How long does a function take, asymptotically, as input size gets really large?

O(1) - constant!

Describing Runtime

slide-58
SLIDE 58

But given larger and larger input sizes, functions will take longer. The question we are curious about is: How long does a function take, asymptotically, as input size gets really large?

O(1) - constant!

Describing Runtime

slide-59
SLIDE 59

But given larger and larger input sizes, functions will take longer. The question we are curious about is: How long does a function take, asymptotically, as input size gets really large?

O(1) - constant! O ( n )

  • l

i n e a r !

Describing Runtime

slide-60
SLIDE 60

More OoG Examples!

Figure out the orders of growth of the following

  • functions. Talk to the person next to you!

Drawing the graph out may help!

slide-61
SLIDE 61

More OoG Examples!

Figure out the orders of growth of the following

  • functions. Talk to the person next to you!

Drawing the graph out may help!

slide-62
SLIDE 62

More OoG Examples!

Figure out the orders of growth of the following

  • functions. Talk to the person next to you!

Drawing the graph out may help!

O(n

2

) - quadratic!

slide-63
SLIDE 63

More OoG Examples!

Figure out the orders of growth of the following

  • functions. Talk to the person next to you!

Drawing the graph out may help!

O(n

2

) - quadratic!

slide-64
SLIDE 64

More OoG Examples!

Figure out the orders of growth of the following

  • functions. Talk to the person next to you!

Drawing the graph out may help!

O(n

2

) - quadratic!

O(1) - constant time!? wow!!!

slide-65
SLIDE 65

Now, the Rules

  • 1. Eliminate Lower Order Terms

Keep this in mind: How long does a function take, asymptotically, as input size gets really large?

slide-66
SLIDE 66
  • 1. Eliminate Lower Order Terms

O(n2+n) = O(n2)

Keep this in mind: How long does a function take, asymptotically, as input size gets really large?

Now, the Rules

slide-67
SLIDE 67
  • 1. Eliminate Lower Order Terms

O(2n+n2+n) = O(2n)

Keep this in mind: How long does a function take, asymptotically, as input size gets really large?

O(n2+n) = O(n2)

Now, the Rules

slide-68
SLIDE 68
  • 1. Eliminate Lower Order Terms

O(2n+n2+n) = O(2n)

Keep this in mind: How long does a function take, asymptotically, as input size gets really large? Why?

O(n2+n) = O(n2)

Now, the Rules

slide-69
SLIDE 69
  • 1. Eliminate Lower Order Terms

O(2n+n2+n) = O(2n)

Keep this in mind: How long does a function take, asymptotically, as input size gets really large? Why?

What matters is the run time as n gets really large! What happens when n = 1000? n2 = 1,000,000 n = 1,000 Note than n2 is much bigger, and therefore

  • utweighs in importance!

O(n2+n) = O(n2)

Now, the Rules

slide-70
SLIDE 70
  • 1. Eliminate Lower Order Terms
  • 2. Ignore Constants

O(2n+n2+n) = O(2n)

Keep this in mind: How long does a function take, asymptotically, as input size gets really large? Why?

What matters is the run time as n gets really large! What happens when n = 1000? n2 = 1,000,000 n = 1,000 Note than n2 is much bigger, and therefore

  • utweighs in importance!

O(500n) = O(n) O(n2+n) = O(n2)

Now, the Rules

slide-71
SLIDE 71
  • 1. Eliminate Lower Order Terms
  • 2. Ignore Constants

O(2n+n2+n) = O(2n)

Keep this in mind: How long does a function take, asymptotically, as input size gets really large? Why?

What matters is the run time as n gets really large! What happens when n = 1000? n2 = 1,000,000 n = 1,000 Note than n2 is much bigger, and therefore

  • utweighs in importance!

O(500n) = O(n)

Why?

O(n2+n) = O(n2)

Now, the Rules

slide-72
SLIDE 72
  • 1. Eliminate Lower Order Terms
  • 2. Ignore Constants

O(2n+n2+n) = O(2n)

Keep this in mind: How long does a function take, asymptotically, as input size gets really large? Why?

What matters is the run time as n gets really large! What happens when n = 1000? n2 = 1,000,000 n = 1,000 Note than n2 is much bigger, and therefore

  • utweighs in importance!

O(500n) = O(n)

Why?

Some computers are faster, some slower. In fact, it’s likely that Cori supercomputer at LBL is 1000x faster than your laptop. So if you wrote a function that ran in 1000n time, Cori would run it in 1 second and your computer would take 15 minutes for n = 1. But we really only care about the algorithm’s inherent growth, so eliminate constants!

O(n2+n) = O(n2)

Now, the Rules

slide-73
SLIDE 73

Thanks for coming.

Have a great rest of your week! :)

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