61A Lecture 18 Announcements Sequences The Sequence Abstraction 4 - - PowerPoint PPT Presentation

61a lecture 18 announcements sequences the sequence
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 18 Announcements Sequences The Sequence Abstraction 4 - - PowerPoint PPT Presentation

61A Lecture 18 Announcements Sequences The Sequence Abstraction 4 The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. 4 The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. There isn't just one


slide-1
SLIDE 1

61A Lecture 18

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Sequences

slide-4
SLIDE 4

The Sequence Abstraction

4

slide-5
SLIDE 5

The Sequence Abstraction

red, orange, yellow, green, blue, indigo, violet.

4

slide-6
SLIDE 6

The Sequence Abstraction

There isn't just one sequence class or data abstraction (in Python or in general). red, orange, yellow, green, blue, indigo, violet.

4

slide-7
SLIDE 7

The Sequence Abstraction

There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: red, orange, yellow, green, blue, indigo, violet.

4

slide-8
SLIDE 8

The Sequence Abstraction

There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: red, orange, yellow, green, blue, indigo, violet.

  • Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0.

4

slide-9
SLIDE 9

The Sequence Abstraction

There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: red, orange, yellow, green, blue, indigo, violet.

  • Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0. 0 , 1 , 2 , 3 , 4 , 5 , 6 .

4

slide-10
SLIDE 10

The Sequence Abstraction

There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: red, orange, yellow, green, blue, indigo, violet.

  • Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0. 0 , 1 , 2 , 3 , 4 , 5 , 6 . There is built-in syntax associated with this behavior, or we can use functions.

4

slide-11
SLIDE 11

The Sequence Abstraction

There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: red, orange, yellow, green, blue, indigo, violet.

  • Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0. 0 , 1 , 2 , 3 , 4 , 5 , 6 . There is built-in syntax associated with this behavior, or we can use functions. A list is a kind of built-in sequence

4

slide-12
SLIDE 12

Linked Lists

slide-13
SLIDE 13

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

6

slide-14
SLIDE 14

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

6

3 , 4 , 5

slide-15
SLIDE 15

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

6

3 , 4 , 5 first: 3 rest: Link instance

slide-16
SLIDE 16

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

6

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance

slide-17
SLIDE 17

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

6

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance

slide-18
SLIDE 18

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

6

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance

slide-19
SLIDE 19

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

6

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance A linked list is a pair

slide-20
SLIDE 20

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

6

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance A linked list is a pair The first (zeroth) element is an attribute value

slide-21
SLIDE 21

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

6

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance A linked list is a pair The first (zeroth) element is an attribute value The rest of the elements are stored in a linked list

slide-22
SLIDE 22

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

6

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance A linked list is a pair The first (zeroth) element is an attribute value The rest of the elements are stored in a linked list A class attribute represents an empty linked list

slide-23
SLIDE 23

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

6

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link(3, Link(4, Link(5, Link.empty))) A linked list is a pair The first (zeroth) element is an attribute value The rest of the elements are stored in a linked list A class attribute represents an empty linked list

slide-24
SLIDE 24

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

7

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link.empty , Link.empty ) Link(3, Link(4, Link(5 )))

slide-25
SLIDE 25

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

7

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link.empty , Link.empty ) Link(3, Link(4, Link(5 )))

slide-26
SLIDE 26

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

7

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link.empty , Link.empty ) Link(3, Link(4, Link(5 )))

slide-27
SLIDE 27

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

7

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link.empty , Link.empty ) Link(3, Link(4, Link(5 )))

slide-28
SLIDE 28

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

7

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance , Link.empty ) Link(3, Link(4, Link(5 )))

slide-29
SLIDE 29

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

7

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link(3, Link(4, Link(5 )))

slide-30
SLIDE 30

Linked List Class

8

Link(3, Link(4, Link(5 )))

slide-31
SLIDE 31

Linked List Class

8

Linked list class: attributes are passed to __init__ Link(3, Link(4, Link(5 )))

slide-32
SLIDE 32

Linked List Class

class Link:

8

Linked list class: attributes are passed to __init__ Link(3, Link(4, Link(5 )))

slide-33
SLIDE 33

Linked List Class

class Link:

8

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): Link(3, Link(4, Link(5 )))

slide-34
SLIDE 34

Linked List Class

class Link:

8

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) Link(3, Link(4, Link(5 )))

slide-35
SLIDE 35

Linked List Class

class Link:

8

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Link(3, Link(4, Link(5 )))

slide-36
SLIDE 36

Linked List Class

class Link:

8

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Link(3, Link(4, Link(5 ))) Returns whether rest is a Link

slide-37
SLIDE 37

Linked List Class

class Link:

8

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Link(3, Link(4, Link(5 ))) Returns whether rest is a Link help(isinstance): Return whether an object is an instance of a class or of a subclass thereof.

slide-38
SLIDE 38

Linked List Class

class Link: empty = ()

8

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Link(3, Link(4, Link(5 ))) Returns whether rest is a Link help(isinstance): Return whether an object is an instance of a class or of a subclass thereof.

slide-39
SLIDE 39

Linked List Class

class Link: empty = ()

8

Some zero-length sequence Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Link(3, Link(4, Link(5 ))) Returns whether rest is a Link help(isinstance): Return whether an object is an instance of a class or of a subclass thereof.

slide-40
SLIDE 40

Linked List Class

class Link: empty = ()

8

Some zero-length sequence Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest (Demo) Link(3, Link(4, Link(5 ))) Returns whether rest is a Link help(isinstance): Return whether an object is an instance of a class or of a subclass thereof.

slide-41
SLIDE 41

Sequence Operations

slide-42
SLIDE 42

Linked List Class

10

More special method names: __len__ __getitem__ Element selection [] Built-in len function

slide-43
SLIDE 43

Linked List Class

10

More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences

slide-44
SLIDE 44

Linked List Class

class Link: empty = ()

10

More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences def __init__(self, first, rest=empty): assert ... self.first = first self.rest = rest

slide-45
SLIDE 45

Linked List Class

class Link: empty = ()

10

More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences def __init__(self, first, rest=empty): assert ... self.first = first self.rest = rest def __getitem__(self, i): if i == 0: return self.first else: return self.rest[i-1]

slide-46
SLIDE 46

Linked List Class

class Link: empty = ()

10

This element selection syntax More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences def __init__(self, first, rest=empty): assert ... self.first = first self.rest = rest def __getitem__(self, i): if i == 0: return self.first else: return self.rest[i-1]

slide-47
SLIDE 47

Linked List Class

class Link: empty = ()

10

This element selection syntax Calls this method More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences def __init__(self, first, rest=empty): assert ... self.first = first self.rest = rest def __getitem__(self, i): if i == 0: return self.first else: return self.rest[i-1]

slide-48
SLIDE 48

Linked List Class

class Link: empty = ()

10

This element selection syntax Calls this method More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences def __init__(self, first, rest=empty): assert ... self.first = first self.rest = rest def __getitem__(self, i): if i == 0: return self.first else: return self.rest[i-1] def __len__(self): return 1 + len(self.rest)

slide-49
SLIDE 49

Linked List Class

class Link: empty = () Recursive call to __len__

10

This element selection syntax Calls this method More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences def __init__(self, first, rest=empty): assert ... self.first = first self.rest = rest def __getitem__(self, i): if i == 0: return self.first else: return self.rest[i-1] def __len__(self): return 1 + len(self.rest)

slide-50
SLIDE 50

Linked List Class

class Link: empty = () Recursive call to __len__

10

(Demo) Methods can be recursive too! This element selection syntax Calls this method More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences def __init__(self, first, rest=empty): assert ... self.first = first self.rest = rest def __getitem__(self, i): if i == 0: return self.first else: return self.rest[i-1] def __len__(self): return 1 + len(self.rest)

slide-51
SLIDE 51

Property Methods

slide-52
SLIDE 52

Property Methods

Often, we want the value of instance attributes to stay in sync

12

For example, what if we wanted a Ratio to keep its proportion when its numerator changes

slide-53
SLIDE 53

Property Methods

Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5)))

12

For example, what if we wanted a Ratio to keep its proportion when its numerator changes

slide-54
SLIDE 54

Property Methods

Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4

12

For example, what if we wanted a Ratio to keep its proportion when its numerator changes

slide-55
SLIDE 55

Property Methods

Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6

12

For example, what if we wanted a Ratio to keep its proportion when its numerator changes

slide-56
SLIDE 56

Property Methods

Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6

12

For example, what if we wanted a Ratio to keep its proportion when its numerator changes

slide-57
SLIDE 57

Property Methods

Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6 >>> s Link(3, Link(6, Link(5)))

12

For example, what if we wanted a Ratio to keep its proportion when its numerator changes

slide-58
SLIDE 58

Property Methods

Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6 >>> s Link(3, Link(6, Link(5)))

12

No method calls! For example, what if we wanted a Ratio to keep its proportion when its numerator changes

slide-59
SLIDE 59

Property Methods

Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6 >>> s Link(3, Link(6, Link(5))) The @property decorator on a method designates that it will be called whenever it is looked up on an instance

12

No method calls! For example, what if we wanted a Ratio to keep its proportion when its numerator changes

slide-60
SLIDE 60

Property Methods

Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6 >>> s Link(3, Link(6, Link(5))) The @property decorator on a method designates that it will be called whenever it is looked up on an instance A @<attribute>.setter decorator on a method designates that it will be called whenever that attribute is assigned. <attribute> must be an existing property method.

12

No method calls! For example, what if we wanted a Ratio to keep its proportion when its numerator changes

slide-61
SLIDE 61

Property Methods

Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6 >>> s Link(3, Link(6, Link(5))) The @property decorator on a method designates that it will be called whenever it is looked up on an instance (Demo) A @<attribute>.setter decorator on a method designates that it will be called whenever that attribute is assigned. <attribute> must be an existing property method.

12

No method calls! For example, what if we wanted a Ratio to keep its proportion when its numerator changes

slide-62
SLIDE 62

Linked List Processing

(Demo)

[<map exp> for <name> in <iter exp> if <filter exp>]