cs 61a lecture 9
play

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


  1. CS 61A Lecture 9 Friday, September 14

  2. The Sequence Abstraction 2

  3. The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. 2

  4. The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. There isn't just one sequence type (in Python or in general) 2

  5. The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. There isn't just one sequence type (in Python or in general) This abstraction is a collection of behaviors: 2

  6. The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. There isn't just one sequence type (in Python or in general) This abstraction is a collection of behaviors: 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. 2

  7. The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. 0 , 1 , 2 , 3 , 4 , 5 , 6 . There isn't just one sequence type (in Python or in general) This abstraction is a collection of behaviors: 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. 2

  8. The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. 0 , 1 , 2 , 3 , 4 , 5 , 6 . There isn't just one sequence type (in Python or in general) This abstraction is a collection of behaviors: 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. The sequence abstraction is shared among several types. 2

  9. Tuples are Sequences (Demo) 3

  10. Box-and-Pointer Notation 4

  11. The Closure Property of Data Types 5

  12. The Closure Property of Data Types • A method for combining data values satisfies the closure property if: 5

  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

  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

  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

  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. Tuples can contain tuples as elements 5

  17. Recursive Lists 6

  18. Recursive Lists Constructor: def rlist(first, rest): """Return a recursive list from its first element and the rest.""" 6

  19. Recursive Lists Constructor: def rlist(first, rest): """Return a recursive list from its first element and the rest.""" 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.""" 6

  20. Recursive Lists Constructor: def rlist(first, rest): """Return a recursive list from its first element and the rest.""" 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): 6

  21. Recursive Lists Constructor: def rlist(first, rest): """Return a recursive list from its first element and the rest.""" 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 6

  22. Recursive Lists Constructor: def rlist(first, rest): """Return a recursive list from its first element and the rest.""" 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 6

  23. Recursive Lists Constructor: def rlist(first, rest): """Return a recursive list from its first element and the rest.""" 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. 6

  24. Implementing Recursive Lists with Pairs 7

  25. Implementing Recursive Lists with Pairs 1 , 2 , 3 , 4 7

  26. Implementing Recursive Lists with Pairs 1 , 2 , 3 , 4 7

  27. Implementing Recursive Lists with Pairs 1 , 2 , 3 , 4 A recursive list is a pair 7

  28. Implementing Recursive Lists with Pairs 1 , 2 , 3 , 4 A recursive list is a pair The first element of the pair is the first element of the list 7

  29. Implementing Recursive Lists with Pairs 1 , 2 , 3 , 4 A recursive list is a pair The first element of The second element of the pair is the first the pair is the rest element of the list of the list 7

  30. Implementing Recursive Lists with Pairs 1 , 2 , 3 , 4 A recursive list is a pair None represents the empty list The first element of The second element of the pair is the first the pair is the rest element of the list of the list 7

  31. Implementing Recursive Lists with Pairs 1 , 2 , 3 , 4 A recursive list is a pair None represents the empty list The first element of The second element of the pair is the first the pair is the rest element of the list of the list (Demo) 7

  32. Implementing the Sequence Abstraction 8

  33. Implementing the Sequence Abstraction 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. 8

  34. Implementing the Sequence Abstraction 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 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. 8

  35. Implementing the Sequence Abstraction 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) 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. 8

  36. Implementing the Sequence Abstraction def len_rlist(s): """Return the length of recursive list s.""" length = 0 while s != empty_rlist: s, length = rest(s), length + 1 (Demo) 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) 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. 8

  37. Environment Diagram for getitem_rlist 9

  38. Not on Midterm 1 Sequence Iteration (Demo) 10

  39. Not on Midterm 1 Sequence Iteration (Demo) def count(s, value): total = 0 for elem in s: Name bound in the first frame of the current environment if elem == value: total = total + 1 return total 10

  40. Not on Midterm 1 For Statement Execution Procedure 11

  41. Not on Midterm 1 For Statement Execution Procedure for <name> in <expression>: <suite> 11

Recommend


More recommend