CS 61A Lecture 9 Friday, September 14 The Sequence Abstraction 2 - - PowerPoint PPT Presentation

cs 61a lecture 9
SMART_READER_LITE
LIVE PREVIEW

CS 61A Lecture 9 Friday, September 14 The Sequence Abstraction 2 - - PowerPoint PPT Presentation

CS 61A Lecture 9 Friday, September 14 The Sequence Abstraction 2 The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. 2 The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. There isn't just one


slide-1
SLIDE 1

CS 61A Lecture 9

Friday, September 14

slide-2
SLIDE 2

The Sequence Abstraction

2

slide-3
SLIDE 3

The Sequence Abstraction

2

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

slide-4
SLIDE 4

The Sequence Abstraction

There isn't just one sequence type (in Python or in general)

2

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

slide-5
SLIDE 5

The Sequence Abstraction

There isn't just one sequence type (in Python or in general) This abstraction is a collection of behaviors:

2

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

slide-6
SLIDE 6

The Sequence Abstraction

There isn't just one sequence type (in Python or in general) This abstraction is a collection of behaviors:

2

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 for the first element.

slide-7
SLIDE 7

The Sequence Abstraction

There isn't just one sequence type (in Python or in general) This abstraction is a collection of behaviors:

2

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 for the first element. 0 , 1 , 2 , 3 , 4 , 5 , 6 .

slide-8
SLIDE 8

The Sequence Abstraction

There isn't just one sequence type (in Python or in general) This abstraction is a collection of behaviors:

2

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 for the first element. 0 , 1 , 2 , 3 , 4 , 5 , 6 . The sequence abstraction is shared among several types.

slide-9
SLIDE 9

Tuples are Sequences

(Demo)

3

slide-10
SLIDE 10

Box-and-Pointer Notation

4

slide-11
SLIDE 11

The Closure Property of Data Types

5

slide-12
SLIDE 12

The Closure Property of Data Types

  • A method for combining data values satisfies the closure

property if:

5

slide-13
SLIDE 13

The Closure Property of Data Types

  • A method for combining data values satisfies the closure

property if:

  • The result of combination can itself be combined using the

same method.

5

slide-14
SLIDE 14

The Closure Property of Data Types

  • A method for combining data values satisfies the closure

property if:

  • The result of combination can itself be combined using the

same method.

  • Closure is the key to power in any means of combination

because it permits us to create hierarchical structures.

5

slide-15
SLIDE 15

The Closure Property of Data Types

  • A method for combining data values satisfies the closure

property if:

  • The result of combination can itself be combined using the

same method.

  • Closure is the key to power in any means of combination

because it permits us to create hierarchical structures.

  • Hierarchical structures are made up of parts, which

themselves are made up of parts, and so on.

5

slide-16
SLIDE 16

The Closure Property of Data Types

  • A method for combining data values satisfies the closure

property if:

  • The result of combination can itself be combined using the

same method.

  • Closure is the key to power in any means of combination

because it permits us to create hierarchical structures.

  • Hierarchical structures are made up of parts, which

themselves are made up of parts, and so on.

5

Tuples can contain tuples as elements

slide-17
SLIDE 17

Recursive Lists

6

slide-18
SLIDE 18

Recursive Lists

Constructor:

def rlist(first, rest): """Return a recursive list from its first element and the rest."""

6

slide-19
SLIDE 19

Recursive Lists

Constructor:

def rlist(first, rest): """Return a recursive list from its first element and the rest."""

6

Selectors:

def first(s): """Return the first element of a recursive list s.""" def rest(s): """Return the rest of the elements of a recursive list s."""

slide-20
SLIDE 20

Recursive Lists

Constructor:

def rlist(first, rest): """Return a recursive list from its first element and the rest."""

6

Selectors:

def first(s): """Return the first element of a recursive list s.""" def rest(s): """Return the rest of the elements of a recursive list s."""

Behavior condition(s):

slide-21
SLIDE 21

Recursive Lists

Constructor:

def rlist(first, rest): """Return a recursive list from its first element and the rest."""

6

Selectors:

def first(s): """Return the first element of a recursive list s.""" def rest(s): """Return the rest of the elements of a recursive list s."""

Behavior condition(s): If a recursive list s is constructed from a first element f and a recursive list r, then

slide-22
SLIDE 22

Recursive Lists

Constructor:

def rlist(first, rest): """Return a recursive list from its first element and the rest."""

6

Selectors:

def first(s): """Return the first element of a recursive list s.""" def rest(s): """Return the rest of the elements of a recursive list s."""

Behavior condition(s): If a recursive list s is constructed from a first element f and a recursive list r, then

  • first(s) returns f, and
slide-23
SLIDE 23

Recursive Lists

Constructor:

def rlist(first, rest): """Return a recursive list from its first element and the rest."""

6

Selectors:

def first(s): """Return the first element of a recursive list s.""" def rest(s): """Return the rest of the elements of a recursive list s."""

Behavior condition(s): If a recursive list s is constructed from a first element f and a recursive list r, then

  • first(s) returns f, and
  • rest(s) returns r, which is a recursive list.
slide-24
SLIDE 24

Implementing Recursive Lists with Pairs

7

slide-25
SLIDE 25

Implementing Recursive Lists with Pairs

7

1 , 2 , 3 , 4

slide-26
SLIDE 26

Implementing Recursive Lists with Pairs

7

1 , 2 , 3 , 4

slide-27
SLIDE 27

Implementing Recursive Lists with Pairs

7

A recursive list is a pair 1 , 2 , 3 , 4

slide-28
SLIDE 28

Implementing Recursive Lists with Pairs

7

A recursive list is a pair The first element of the pair is the first element of the list 1 , 2 , 3 , 4

slide-29
SLIDE 29

Implementing Recursive Lists with Pairs

7

A recursive list is a pair The first element of the pair is the first element of the list The second element of the pair is the rest

  • f the list

1 , 2 , 3 , 4

slide-30
SLIDE 30

Implementing Recursive Lists with Pairs

7

A recursive list is a pair The first element of the pair is the first element of the list The second element of the pair is the rest

  • f the list

None represents the empty list 1 , 2 , 3 , 4

slide-31
SLIDE 31

Implementing Recursive Lists with Pairs

7

A recursive list is a pair The first element of the pair is the first element of the list The second element of the pair is the rest

  • f the list

None represents the empty list (Demo) 1 , 2 , 3 , 4

slide-32
SLIDE 32

Implementing the Sequence Abstraction

8

slide-33
SLIDE 33

Implementing the Sequence Abstraction

8

  • 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 for the first element.

slide-34
SLIDE 34

Implementing the Sequence Abstraction

8

  • 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 for the first element. def len_rlist(s): """Return the length of recursive list s.""" length = 0 while s != empty_rlist: s, length = rest(s), length + 1 return length

slide-35
SLIDE 35

Implementing the Sequence Abstraction

8

  • 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 for the first element. def len_rlist(s): """Return the length of recursive list s.""" length = 0 while s != empty_rlist: s, length = rest(s), length + 1 return length def getitem_rlist(s, i): """Return the element at index i of recursive list s.""" while i > 0: s, i = rest(s), i - 1 return first(s)

slide-36
SLIDE 36

Implementing the Sequence Abstraction

8

  • 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 for the first element. (Demo) def len_rlist(s): """Return the length of recursive list s.""" length = 0 while s != empty_rlist: s, length = rest(s), length + 1 return length def getitem_rlist(s, i): """Return the element at index i of recursive list s.""" while i > 0: s, i = rest(s), i - 1 return first(s)

slide-37
SLIDE 37

Environment Diagram for getitem_rlist

9

slide-38
SLIDE 38

Sequence Iteration

10

(Demo) Not on Midterm 1

slide-39
SLIDE 39

Sequence Iteration

10

(Demo) def count(s, value): total = 0 for elem in s: if elem == value: total = total + 1 return total Name bound in the first frame

  • f the current environment

Not on Midterm 1

slide-40
SLIDE 40

For Statement Execution Procedure

11

Not on Midterm 1

slide-41
SLIDE 41

For Statement Execution Procedure

11

for <name> in <expression>: <suite> Not on Midterm 1

slide-42
SLIDE 42

For Statement Execution Procedure

11

for <name> in <expression>: <suite>

  • 1. Evaluate the header <expression>, which must yield an

iterable value. Not on Midterm 1

slide-43
SLIDE 43

For Statement Execution Procedure

11

for <name> in <expression>: <suite>

  • 1. Evaluate the header <expression>, which must yield an

iterable value.

  • 2. For each element in that sequence, in order:

Not on Midterm 1

slide-44
SLIDE 44

For Statement Execution Procedure

11

for <name> in <expression>: <suite>

  • 1. Evaluate the header <expression>, which must yield an

iterable value.

  • 2. For each element in that sequence, in order:
  • A. Bind <name> to that element in the local environment.

Not on Midterm 1

slide-45
SLIDE 45

For Statement Execution Procedure

11

for <name> in <expression>: <suite>

  • 1. Evaluate the header <expression>, which must yield an

iterable value.

  • 2. For each element in that sequence, in order:
  • A. Bind <name> to that element in the local environment.
  • B. Execute the <suite>.

Not on Midterm 1

slide-46
SLIDE 46

Sequence Unpacking in For Statements

12

Not on Midterm 1

slide-47
SLIDE 47

Sequence Unpacking in For Statements

>>> pairs = ((1, 2), (2, 2), (2, 3), (4, 4)) >>> same_count = 0

12

Not on Midterm 1

slide-48
SLIDE 48

Sequence Unpacking in For Statements

>>> pairs = ((1, 2), (2, 2), (2, 3), (4, 4)) >>> same_count = 0

12

A sequence of fixed-length sequences Not on Midterm 1

slide-49
SLIDE 49

Sequence Unpacking in For Statements

>>> pairs = ((1, 2), (2, 2), (2, 3), (4, 4)) >>> same_count = 0

12

>>> for x, y in pairs: if x == y: same_count = same_count + 1 >>> same_count 2 A sequence of fixed-length sequences Not on Midterm 1

slide-50
SLIDE 50

Sequence Unpacking in For Statements

>>> pairs = ((1, 2), (2, 2), (2, 3), (4, 4)) >>> same_count = 0

12

>>> for x, y in pairs: if x == y: same_count = same_count + 1 >>> same_count 2 A sequence of fixed-length sequences A name for each element in a fixed-length sequence Not on Midterm 1

slide-51
SLIDE 51

Sequence Unpacking in For Statements

>>> pairs = ((1, 2), (2, 2), (2, 3), (4, 4)) >>> same_count = 0

12

>>> for x, y in pairs: if x == y: same_count = same_count + 1 >>> same_count 2 A sequence of fixed-length sequences A name for each element in a fixed-length sequence Each name is bound to a value, as in multiple assignment Not on Midterm 1

slide-52
SLIDE 52

The Range Type

13

A range is a sequence of consecutive integers.* Not on Midterm 1

slide-53
SLIDE 53

The Range Type

13

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

Not on Midterm 1

slide-54
SLIDE 54

The Range Type

13

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... Not on Midterm 1

slide-55
SLIDE 55

The Range Type

13

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Not on Midterm 1

slide-56
SLIDE 56

The Range Type

13

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Not on Midterm 1

slide-57
SLIDE 57

The Range Type

13

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Not on Midterm 1

slide-58
SLIDE 58

The Range Type

13

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Not on Midterm 1

slide-59
SLIDE 59

The Range Type

13

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Length: ending value - starting value Not on Midterm 1

slide-60
SLIDE 60

The Range Type

13

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Length: ending value - starting value Element selection: starting value + index Not on Midterm 1

slide-61
SLIDE 61

The Range Type

>>> tuple(range(-2, 2)) (-2, -1, 0, 1) >>> tuple(range(4)) (0, 1, 2, 3)

13

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Length: ending value - starting value Element selection: starting value + index Not on Midterm 1

slide-62
SLIDE 62

The Range Type

>>> tuple(range(-2, 2)) (-2, -1, 0, 1) >>> tuple(range(4)) (0, 1, 2, 3)

13

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Length: ending value - starting value Element selection: starting value + index Tuple construction Not on Midterm 1

slide-63
SLIDE 63

The Range Type

>>> tuple(range(-2, 2)) (-2, -1, 0, 1) >>> tuple(range(4)) (0, 1, 2, 3)

13

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Length: ending value - starting value Element selection: starting value + index Tuple construction With a 0 starting value Not on Midterm 1

slide-64
SLIDE 64

The Range Type

>>> tuple(range(-2, 2)) (-2, -1, 0, 1) >>> tuple(range(4)) (0, 1, 2, 3)

13

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Length: ending value - starting value Element selection: starting value + index Tuple construction With a 0 starting value (Demo) Not on Midterm 1

slide-65
SLIDE 65

Membership & Slicing

14

The Python sequence abstraction has two more behaviors! Not on Midterm 1

slide-66
SLIDE 66

Membership & Slicing

14

The Python sequence abstraction has two more behaviors! Membership. Not on Midterm 1

slide-67
SLIDE 67

Membership & Slicing

>>> digits = (1, 8, 2, 8) >>> 2 in digits True >>> 1828 not in digits True

14

The Python sequence abstraction has two more behaviors! Membership. Not on Midterm 1

slide-68
SLIDE 68

Membership & Slicing

>>> digits = (1, 8, 2, 8) >>> 2 in digits True >>> 1828 not in digits True

14

The Python sequence abstraction has two more behaviors! Membership. Slicing. Not on Midterm 1

slide-69
SLIDE 69

Membership & Slicing

>>> digits = (1, 8, 2, 8) >>> 2 in digits True >>> 1828 not in digits True

14

>>> digits[0:2] (1, 8) >>> digits[1:] (8, 2, 8) The Python sequence abstraction has two more behaviors! Membership. Slicing. Not on Midterm 1