61A Lecture 20 Announcements Sets Sets >>> import re - - PowerPoint PPT Presentation

61a lecture 20 announcements sets sets
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 20 Announcements Sets Sets >>> import re - - PowerPoint PPT Presentation

61A Lecture 20 Announcements Sets Sets >>> import re >>> text = \ # list of words ... re.split(r\s+, ... open(shakespeare.txt).read()) >>> W = set(text) >>> { w for w in W


slide-1
SLIDE 1

61A Lecture 20

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Sets

slide-4
SLIDE 4

Sets

4

>>> import re >>> text = \ # list of words ... re.split(r’\s+’, ... open(‘shakespeare.txt’).read()) >>> W = set(text) >>> { w for w in W ... if w == w[-1::-1] and len(w)==5 } {'madam', 'refer', 'rever', 'minim', ‘level’ }

slide-5
SLIDE 5

Sets

One more built-in Python container type

4

>>> import re >>> text = \ # list of words ... re.split(r’\s+’, ... open(‘shakespeare.txt’).read()) >>> W = set(text) >>> { w for w in W ... if w == w[-1::-1] and len(w)==5 } {'madam', 'refer', 'rever', 'minim', ‘level’ }

slide-6
SLIDE 6

Sets

One more built-in Python container type

  • Set literals are enclosed in braces

4

>>> import re >>> text = \ # list of words ... re.split(r’\s+’, ... open(‘shakespeare.txt’).read()) >>> W = set(text) >>> { w for w in W ... if w == w[-1::-1] and len(w)==5 } {'madam', 'refer', 'rever', 'minim', ‘level’ }

slide-7
SLIDE 7

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction

4

>>> import re >>> text = \ # list of words ... re.split(r’\s+’, ... open(‘shakespeare.txt’).read()) >>> W = set(text) >>> { w for w in W ... if w == w[-1::-1] and len(w)==5 } {'madam', 'refer', 'rever', 'minim', ‘level’ }

slide-8
SLIDE 8

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order, just like dictionary entries

4

>>> import re >>> text = \ # list of words ... re.split(r’\s+’, ... open(‘shakespeare.txt’).read()) >>> W = set(text) >>> { w for w in W ... if w == w[-1::-1] and len(w)==5 } {'madam', 'refer', 'rever', 'minim', ‘level’ }

slide-9
SLIDE 9

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order, just like dictionary entries

>>> s = {3, 2, 1, 4, 4}

4

>>> import re >>> text = \ # list of words ... re.split(r’\s+’, ... open(‘shakespeare.txt’).read()) >>> W = set(text) >>> { w for w in W ... if w == w[-1::-1] and len(w)==5 } {'madam', 'refer', 'rever', 'minim', ‘level’ }

slide-10
SLIDE 10

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order, just like dictionary entries

>>> s = {3, 2, 1, 4, 4} >>> s {1, 2, 3, 4}

4

>>> import re >>> text = \ # list of words ... re.split(r’\s+’, ... open(‘shakespeare.txt’).read()) >>> W = set(text) >>> { w for w in W ... if w == w[-1::-1] and len(w)==5 } {'madam', 'refer', 'rever', 'minim', ‘level’ }

slide-11
SLIDE 11

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order, just like dictionary entries

>>> s = {3, 2, 1, 4, 4} >>> s {1, 2, 3, 4} >>> 3 in s True

4

>>> import re >>> text = \ # list of words ... re.split(r’\s+’, ... open(‘shakespeare.txt’).read()) >>> W = set(text) >>> { w for w in W ... if w == w[-1::-1] and len(w)==5 } {'madam', 'refer', 'rever', 'minim', ‘level’ }

slide-12
SLIDE 12

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order, just like dictionary entries

>>> s = {3, 2, 1, 4, 4} >>> s {1, 2, 3, 4} >>> 3 in s True >>> len(s) 4

4

>>> import re >>> text = \ # list of words ... re.split(r’\s+’, ... open(‘shakespeare.txt’).read()) >>> W = set(text) >>> { w for w in W ... if w == w[-1::-1] and len(w)==5 } {'madam', 'refer', 'rever', 'minim', ‘level’ }

slide-13
SLIDE 13

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order, just like dictionary entries

>>> s = {3, 2, 1, 4, 4} >>> s {1, 2, 3, 4} >>> 3 in s True >>> len(s) 4 >>> s.union({1, 5}) {1, 2, 3, 4, 5}

4

>>> import re >>> text = \ # list of words ... re.split(r’\s+’, ... open(‘shakespeare.txt’).read()) >>> W = set(text) >>> { w for w in W ... if w == w[-1::-1] and len(w)==5 } {'madam', 'refer', 'rever', 'minim', ‘level’ }

slide-14
SLIDE 14

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order, just like dictionary entries

>>> s = {3, 2, 1, 4, 4} >>> s {1, 2, 3, 4} >>> 3 in s True >>> len(s) 4 >>> s.union({1, 5}) {1, 2, 3, 4, 5} >>> s.intersection({6, 5, 4, 3}) {3, 4}

4

>>> import re >>> text = \ # list of words ... re.split(r’\s+’, ... open(‘shakespeare.txt’).read()) >>> W = set(text) >>> { w for w in W ... if w == w[-1::-1] and len(w)==5 } {'madam', 'refer', 'rever', 'minim', ‘level’ }

slide-15
SLIDE 15

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order, just like dictionary entries

>>> s = {3, 2, 1, 4, 4} >>> s {1, 2, 3, 4} >>> 3 in s True >>> len(s) 4 >>> s.union({1, 5}) {1, 2, 3, 4, 5} >>> s.intersection({6, 5, 4, 3}) {3, 4} >>> s {1, 2, 3, 4}

4

>>> import re >>> text = \ # list of words ... re.split(r’\s+’, ... open(‘shakespeare.txt’).read()) >>> W = set(text) >>> { w for w in W ... if w == w[-1::-1] and len(w)==5 } {'madam', 'refer', 'rever', 'minim', ‘level’ }

slide-16
SLIDE 16

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order, just like dictionary entries

>>> s = {3, 2, 1, 4, 4} >>> s {1, 2, 3, 4} >>> 3 in s True >>> len(s) 4 >>> s.union({1, 5}) {1, 2, 3, 4, 5} >>> s.intersection({6, 5, 4, 3}) {3, 4} >>> s {1, 2, 3, 4}

4

(Demo) >>> import re >>> text = \ # list of words ... re.split(r’\s+’, ... open(‘shakespeare.txt’).read()) >>> W = set(text) >>> { w for w in W ... if w == w[-1::-1] and len(w)==5 } {'madam', 'refer', 'rever', 'minim', ‘level’ }

slide-17
SLIDE 17

Implementing Sets

5

slide-18
SLIDE 18

Implementing Sets

What we should be able to do with a set:

5

slide-19
SLIDE 19

Implementing Sets

What we should be able to do with a set:

  • Membership testing: Is a value an element of a set?

5

slide-20
SLIDE 20

Implementing Sets

What we should be able to do with a set:

  • Membership testing: Is a value an element of a set?
  • Union: Return a set with all elements in set1 or set2

5

slide-21
SLIDE 21

Implementing Sets

What we should be able to do with a set:

  • Membership testing: Is a value an element of a set?
  • Union: Return a set with all elements in set1 or set2

Union 1 3 4 2 3 5 1 3 4 2 5

5

slide-22
SLIDE 22

Implementing Sets

What we should be able to do with a set:

  • Membership testing: Is a value an element of a set?
  • Union: Return a set with all elements in set1 or set2
  • Intersection: Return a set with any elements in set1 and set2

Union 1 3 4 2 3 5 1 3 4 2 5

5

slide-23
SLIDE 23

Implementing Sets

What we should be able to do with a set:

  • Membership testing: Is a value an element of a set?
  • Union: Return a set with all elements in set1 or set2
  • Intersection: Return a set with any elements in set1 and set2

Union 1 3 4 2 3 5 1 3 4 2 5 Intersection 1 3 4 2 3 5 3

5

slide-24
SLIDE 24

Implementing Sets

What we should be able to do with a set:

  • Membership testing: Is a value an element of a set?
  • Union: Return a set with all elements in set1 or set2
  • Intersection: Return a set with any elements in set1 and set2
  • Adjoin: Return a set with all elements in s and a value v

Union 1 3 4 2 3 5 1 3 4 2 5 Intersection 1 3 4 2 3 5 3

5

slide-25
SLIDE 25

Implementing Sets

What we should be able to do with a set:

  • Membership testing: Is a value an element of a set?
  • Union: Return a set with all elements in set1 or set2
  • Intersection: Return a set with any elements in set1 and set2
  • Adjoin: Return a set with all elements in s and a value v

Union 1 3 4 2 3 5 1 3 4 2 5 Intersection 1 3 4 2 3 5 3 Adjoin 1 3 4 2 1 3 4 2

5

slide-26
SLIDE 26

Sets as Linked Lists

slide-27
SLIDE 27

Sets as Unordered Sequences

Proposal 1: A set is represented by a linked list that contains no duplicate items.

7

slide-28
SLIDE 28

Sets as Unordered Sequences

Proposal 1: A set is represented by a linked list that contains no duplicate items.

7

def empty(s): return s is Link.empty

slide-29
SLIDE 29

Sets as Unordered Sequences

Proposal 1: A set is represented by a linked list that contains no duplicate items.

7

def empty(s): return s is Link.empty def contains(s, v): """Return whether set s contains value v. >>> s = Link(1, Link(3, Link(2))) >>> contains(s, 2) True """

slide-30
SLIDE 30

Sets as Unordered Sequences

Proposal 1: A set is represented by a linked list that contains no duplicate items. (Demo)

7

def empty(s): return s is Link.empty def contains(s, v): """Return whether set s contains value v. >>> s = Link(1, Link(3, Link(2))) >>> contains(s, 2) True """

slide-31
SLIDE 31

Sets as Unordered Sequences

Proposal 1: A set is represented by a linked list that contains no duplicate items. (Demo)

7

Time order of growth def empty(s): return s is Link.empty def contains(s, v): """Return whether set s contains value v. >>> s = Link(1, Link(3, Link(2))) >>> contains(s, 2) True """

slide-32
SLIDE 32

Sets as Unordered Sequences

Proposal 1: A set is represented by a linked list that contains no duplicate items. (Demo)

7

Time order of growth

Θ(1)

def empty(s): return s is Link.empty def contains(s, v): """Return whether set s contains value v. >>> s = Link(1, Link(3, Link(2))) >>> contains(s, 2) True """

slide-33
SLIDE 33

Sets as Unordered Sequences

Proposal 1: A set is represented by a linked list that contains no duplicate items. (Demo)

7

Time order of growth

Θ(1)

Time depends on whether & where v appears in s. def empty(s): return s is Link.empty def contains(s, v): """Return whether set s contains value v. >>> s = Link(1, Link(3, Link(2))) >>> contains(s, 2) True """

slide-34
SLIDE 34

Sets as Unordered Sequences

Proposal 1: A set is represented by a linked list that contains no duplicate items. (Demo)

7

Time order of growth

Θ(1) Θ(n)

Time depends on whether & where v appears in s. def empty(s): return s is Link.empty def contains(s, v): """Return whether set s contains value v. >>> s = Link(1, Link(3, Link(2))) >>> contains(s, 2) True """

slide-35
SLIDE 35

Sets as Unordered Sequences

Proposal 1: A set is represented by a linked list that contains no duplicate items. (Demo)

7

Time order of growth

Θ(1) Θ(n)

Time depends on whether & where v appears in s. In the worst case: v 
 does not appear in s 


  • r


In the average case: appears in a uniformly distributed random location def empty(s): return s is Link.empty def contains(s, v): """Return whether set s contains value v. >>> s = Link(1, Link(3, Link(2))) >>> contains(s, 2) True """

slide-36
SLIDE 36

Sets as Unordered Sequences

8

slide-37
SLIDE 37

Sets as Unordered Sequences

8

def adjoin(s, v): if contains(s, v): return s else: return Link(v, s)

slide-38
SLIDE 38

Sets as Unordered Sequences

Time order of worst-case growth

8

def adjoin(s, v): if contains(s, v): return s else: return Link(v, s)

slide-39
SLIDE 39

Θ(n)

Sets as Unordered Sequences

Time order of worst-case growth

8

def adjoin(s, v): if contains(s, v): return s else: return Link(v, s)

slide-40
SLIDE 40

Θ(n)

Sets as Unordered Sequences

Time order of worst-case growth The size of the set

8

def adjoin(s, v): if contains(s, v): return s else: return Link(v, s)

slide-41
SLIDE 41

Θ(n)

Sets as Unordered Sequences

Time order of worst-case growth The size of the set

8

def adjoin(s, v): if contains(s, v): return s else: return Link(v, s) def intersect(set1, set2): in_set2 = lambda v: contains(set2, v) return filter_link(in_set2, set1)

slide-42
SLIDE 42

Θ(n)

Sets as Unordered Sequences

Time order of worst-case growth The size of the set

8

def adjoin(s, v): if contains(s, v): return s else: return Link(v, s) def intersect(set1, set2): in_set2 = lambda v: contains(set2, v) return filter_link(in_set2, set1) Return elements x for which in_set2(x) returns a true value

slide-43
SLIDE 43

Θ(n)

Sets as Unordered Sequences

Θ(n2)

Time order of worst-case growth The size of the set

8

def adjoin(s, v): if contains(s, v): return s else: return Link(v, s) def intersect(set1, set2): in_set2 = lambda v: contains(set2, v) return filter_link(in_set2, set1) Return elements x for which in_set2(x) returns a true value

slide-44
SLIDE 44

Θ(n)

Sets as Unordered Sequences

Θ(n2)

Time order of worst-case growth The size of the set If sets are the same size

8

def adjoin(s, v): if contains(s, v): return s else: return Link(v, s) def intersect(set1, set2): in_set2 = lambda v: contains(set2, v) return filter_link(in_set2, set1) Return elements x for which in_set2(x) returns a true value

slide-45
SLIDE 45

Θ(n)

Sets as Unordered Sequences

Θ(n2)

Time order of worst-case growth The size of the set If sets are the same size

8

def adjoin(s, v): if contains(s, v): return s else: return Link(v, s) def intersect(set1, set2): in_set2 = lambda v: contains(set2, v) return filter_link(in_set2, set1) def union(set1, set2): not_in_set2 = lambda v: not contains(set2, v) set1_not_set2 = filter_link(not_in_set2, set1) return extend_link(set1_not_set2, set2) Return elements x for which in_set2(x) returns a true value

slide-46
SLIDE 46

Θ(n)

Sets as Unordered Sequences

Θ(n2)

Time order of worst-case growth The size of the set If sets are the same size

8

def adjoin(s, v): if contains(s, v): return s else: return Link(v, s) def intersect(set1, set2): in_set2 = lambda v: contains(set2, v) return filter_link(in_set2, set1) def union(set1, set2): not_in_set2 = lambda v: not contains(set2, v) set1_not_set2 = filter_link(not_in_set2, set1) return extend_link(set1_not_set2, set2) Return elements x for which in_set2(x) returns a true value Return a linked list containing all elements in set1_not_set2 followed by all elements in set2

slide-47
SLIDE 47

Θ(n)

Θ(n2)

Sets as Unordered Sequences

Θ(n2)

Time order of worst-case growth The size of the set If sets are the same size

8

def adjoin(s, v): if contains(s, v): return s else: return Link(v, s) def intersect(set1, set2): in_set2 = lambda v: contains(set2, v) return filter_link(in_set2, set1) def union(set1, set2): not_in_set2 = lambda v: not contains(set2, v) set1_not_set2 = filter_link(not_in_set2, set1) return extend_link(set1_not_set2, set2) Return elements x for which in_set2(x) returns a true value Return a linked list containing all elements in set1_not_set2 followed by all elements in set2

slide-48
SLIDE 48

Sets as Ordered Linked Lists

slide-49
SLIDE 49

Sets as Ordered Sequences

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

10

slide-50
SLIDE 50

Sets as Ordered Sequences

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

10

Parts of the program that... Assume that sets are... Using...

slide-51
SLIDE 51

Sets as Ordered Sequences

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

10

Parts of the program that... Assume that sets are... Using... Use sets to contain values

slide-52
SLIDE 52

Sets as Ordered Sequences

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

10

Parts of the program that... Assume that sets are... Using... Use sets to contain values Unordered collections

slide-53
SLIDE 53

Sets as Ordered Sequences

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

10

Parts of the program that... Assume that sets are... Using... Use sets to contain values Unordered collections empty, contains, adjoin, 
 intersect, union

slide-54
SLIDE 54

Sets as Ordered Sequences

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

10

Parts of the program that... Assume that sets are... Using... Use sets to contain values Unordered collections empty, contains, adjoin, 
 intersect, union Implement set operations

slide-55
SLIDE 55

Sets as Ordered Sequences

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

10

Parts of the program that... Assume that sets are... Using... Use sets to contain values Unordered collections empty, contains, adjoin, 
 intersect, union Implement set operations Ordered linked lists

slide-56
SLIDE 56

Sets as Ordered Sequences

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

10

Parts of the program that... Assume that sets are... Using... Use sets to contain values Unordered collections empty, contains, adjoin, 
 intersect, union Implement set operations Ordered linked lists first, rest, <, >, ==

slide-57
SLIDE 57

Sets as Ordered Sequences

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

10

Parts of the program that... Assume that sets are... Using... Use sets to contain values Unordered collections empty, contains, adjoin, 
 intersect, union Implement set operations Ordered linked lists first, rest, <, >, ==

slide-58
SLIDE 58

Sets as Ordered Sequences

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

10

Parts of the program that... Assume that sets are... Using... Use sets to contain values Unordered collections empty, contains, adjoin, 
 intersect, union Implement set operations Ordered linked lists first, rest, <, >, ==

Different parts of a program may make different assumptions about data

slide-59
SLIDE 59

Searching an Ordered List

11

slide-60
SLIDE 60

Searching an Ordered List

11

>>> s = Link(1, Link(3, Link(5)))

slide-61
SLIDE 61

Searching an Ordered List

11

first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: >>> s = Link(1, Link(3, Link(5)))

slide-62
SLIDE 62

Searching an Ordered List

11

Time order of growth Operation first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: >>> s = Link(1, Link(3, Link(5)))

slide-63
SLIDE 63

Searching an Ordered List

11

contains Time order of growth Operation first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: >>> s = Link(1, Link(3, Link(5)))

slide-64
SLIDE 64

Searching an Ordered List

11

contains Time order of growth Operation first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: >>> s = Link(1, Link(3, Link(5))) >>> contains(s, 1)

slide-65
SLIDE 65

Searching an Ordered List

11

contains Time order of growth Operation first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: >>> s = Link(1, Link(3, Link(5))) >>> contains(s, 1) True

slide-66
SLIDE 66

Searching an Ordered List

11

contains Time order of growth Operation first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: >>> s = Link(1, Link(3, Link(5))) >>> contains(s, 1) True >>> contains(s, 2)

slide-67
SLIDE 67

Searching an Ordered List

11

contains Time order of growth Operation first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: >>> s = Link(1, Link(3, Link(5))) >>> contains(s, 1) True >>> contains(s, 2) False

slide-68
SLIDE 68

Searching an Ordered List

11

Θ(n)

contains Time order of growth Operation first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: >>> s = Link(1, Link(3, Link(5))) >>> contains(s, 1) True >>> contains(s, 2) False

slide-69
SLIDE 69

Searching an Ordered List

11

Θ(n)

contains Time order of growth Operation adjoin first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: >>> s = Link(1, Link(3, Link(5))) >>> contains(s, 1) True >>> contains(s, 2) False

slide-70
SLIDE 70

Searching an Ordered List

11

Θ(n)

contains Time order of growth Operation adjoin first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: >>> s = Link(1, Link(3, Link(5))) >>> contains(s, 1) True >>> contains(s, 2) False >>> t = adjoin(s, 2)

slide-71
SLIDE 71

Searching an Ordered List

11

first: 2 rest: Link instance

Θ(n)

contains Time order of growth Operation adjoin first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: >>> s = Link(1, Link(3, Link(5))) >>> contains(s, 1) True >>> contains(s, 2) False >>> t = adjoin(s, 2)

slide-72
SLIDE 72

Searching an Ordered List

11

first: 1 rest: Link instance first: 2 rest: Link instance

Θ(n)

contains Time order of growth Operation adjoin first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: >>> s = Link(1, Link(3, Link(5))) >>> contains(s, 1) True >>> contains(s, 2) False >>> t = adjoin(s, 2)

slide-73
SLIDE 73

Searching an Ordered List

11

first: 1 rest: Link instance first: 2 rest: Link instance

Θ(n)

contains Time order of growth Operation adjoin first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: >>> s = Link(1, Link(3, Link(5))) >>> contains(s, 1) True >>> contains(s, 2) False >>> t = adjoin(s, 2) t:

slide-74
SLIDE 74

Searching an Ordered List

11

first: 1 rest: Link instance first: 2 rest: Link instance

Θ(n)

contains Time order of growth Operation

Θ(n)

adjoin first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: >>> s = Link(1, Link(3, Link(5))) >>> contains(s, 1) True >>> contains(s, 2) False >>> t = adjoin(s, 2) t:

slide-75
SLIDE 75

Searching an Ordered List

11

first: 1 rest: Link instance first: 2 rest: Link instance

Θ(n)

contains Time order of growth Operation

Θ(n)

adjoin first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: >>> s = Link(1, Link(3, Link(5))) >>> contains(s, 1) True >>> contains(s, 2) False >>> t = adjoin(s, 2) t: (Demo)

slide-76
SLIDE 76

Set Operations

slide-77
SLIDE 77

Intersecting Ordered Linked Lists

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

13

Let n be max of set1, set2 size

slide-78
SLIDE 78

Intersecting Ordered Linked Lists

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

13

def intersect(set1, set2): Let n be max of set1, set2 size

slide-79
SLIDE 79

Intersecting Ordered Linked Lists

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

13

def intersect(set1, set2): if empty(set1) or empty(set2): return Link.empty Let n be max of set1, set2 size

slide-80
SLIDE 80

Intersecting Ordered Linked Lists

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

13

def intersect(set1, set2): if empty(set1) or empty(set2): return Link.empty else: Let n be max of set1, set2 size

slide-81
SLIDE 81

Intersecting Ordered Linked Lists

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

13

def intersect(set1, set2): if empty(set1) or empty(set2): return Link.empty else: e1, e2 = set1.first, set2.first Let n be max of set1, set2 size

slide-82
SLIDE 82

Intersecting Ordered Linked Lists

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

13

def intersect(set1, set2): if empty(set1) or empty(set2): return Link.empty else: e1, e2 = set1.first, set2.first if e1 == e2: return Link(e1, intersect(set1.rest, set2.rest)) Let n be max of set1, set2 size

slide-83
SLIDE 83

Intersecting Ordered Linked Lists

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

13

def intersect(set1, set2): if empty(set1) or empty(set2): return Link.empty else: e1, e2 = set1.first, set2.first if e1 == e2: return Link(e1, intersect(set1.rest, set2.rest)) elif e1 < e2: return intersect(set1.rest, set2) Let n be max of set1, set2 size

slide-84
SLIDE 84

Intersecting Ordered Linked Lists

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

13

def intersect(set1, set2): if empty(set1) or empty(set2): return Link.empty else: e1, e2 = set1.first, set2.first if e1 == e2: return Link(e1, intersect(set1.rest, set2.rest)) elif e1 < e2: return intersect(set1.rest, set2) elif e2 < e1: return intersect(set1, set2.rest) Let n be max of set1, set2 size

slide-85
SLIDE 85

Intersecting Ordered Linked Lists

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

13

def intersect(set1, set2): if empty(set1) or empty(set2): return Link.empty else: e1, e2 = set1.first, set2.first if e1 == e2: return Link(e1, intersect(set1.rest, set2.rest)) elif e1 < e2: return intersect(set1.rest, set2) elif e2 < e1: return intersect(set1, set2.rest) Order of growth? Let n be max of set1, set2 size

slide-86
SLIDE 86

Intersecting Ordered Linked Lists

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

13

def intersect(set1, set2): if empty(set1) or empty(set2): return Link.empty else: e1, e2 = set1.first, set2.first if e1 == e2: return Link(e1, intersect(set1.rest, set2.rest)) elif e1 < e2: return intersect(set1.rest, set2) elif e2 < e1: return intersect(set1, set2.rest)

Θ(n)

Order of growth? Let n be max of set1, set2 size

slide-87
SLIDE 87

Intersecting Ordered Linked Lists

Proposal 2: A set is represented by a linked list with unique elements that is 


  • rdered from least to greatest

13

def intersect(set1, set2): if empty(set1) or empty(set2): return Link.empty else: e1, e2 = set1.first, set2.first if e1 == e2: return Link(e1, intersect(set1.rest, set2.rest)) elif e1 < e2: return intersect(set1.rest, set2) elif e2 < e1: return intersect(set1, set2.rest)

Θ(n)

Order of growth? (Demo) Let n be max of set1, set2 size

slide-88
SLIDE 88

Set Mutation

slide-89
SLIDE 89

Adding to an Ordered List

15

first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s:

slide-90
SLIDE 90

Adding to an Ordered List

15

first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance add(s, 0) s: Try to return the same object as input

slide-91
SLIDE 91

Adding to an Ordered List

16

first: 1 0 rest: Link instance first: 3 rest: Link instance first: 1 rest: Link instance first: 5 rest: Link instance s:

slide-92
SLIDE 92

Adding to an Ordered List

16

first: 1 0 rest: Link instance first: 3 rest: Link instance first: 1 rest: Link instance first: 5 rest: Link instance s: add(s, 3)

slide-93
SLIDE 93

Adding to an Ordered List

16

first: 1 0 rest: Link instance first: 3 rest: Link instance first: 1 rest: Link instance first: 5 rest: Link instance s: add(s, 4) add(s, 3)

slide-94
SLIDE 94

Adding to an Ordered List

17

first: 1 0 rest: Link instance first: 3 rest: Link instance first: 5 4 rest: Link instance first: 1 rest: Link instance first: 5 rest: Link instance s:

slide-95
SLIDE 95

Adding to an Ordered List

17

first: 1 0 rest: Link instance first: 3 rest: Link instance first: 5 4 rest: Link instance first: 1 rest: Link instance first: 5 rest: Link instance add(s, 6) s:

slide-96
SLIDE 96

Adding to an Ordered List

18

first: 1 0 rest: Link instance first: 3 rest: Link instance first: 5 4 rest: Link instance first: 1 rest: Link instance first: 5 rest: Link instance first: 6 rest: Link instance s:

slide-97
SLIDE 97

Adding to a Set Represented as an Ordered List

19

s:

slide-98
SLIDE 98

Adding to a Set Represented as an Ordered List

19

def add(s, v): s:

slide-99
SLIDE 99

Adding to a Set Represented as an Ordered List

19

def add(s, v): """Add v to a set s, returning modified s.””” s:

slide-100
SLIDE 100

Adding to a Set Represented as an Ordered List

19

def add(s, v): """Add v to a set s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) s:

slide-101
SLIDE 101

Adding to a Set Represented as an Ordered List

19

def add(s, v): """Add v to a set s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) s:

slide-102
SLIDE 102

Adding to a Set Represented as an Ordered List

19

def add(s, v): """Add v to a set s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) s:

slide-103
SLIDE 103

Adding to a Set Represented as an Ordered List

19

def add(s, v): """Add v to a set s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) s:

slide-104
SLIDE 104

Adding to a Set Represented as an Ordered List

19

def add(s, v): """Add v to a set s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ s:

slide-105
SLIDE 105

Adding to a Set Represented as an Ordered List

19

def add(s, v): """Add v to a set s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ if empty(s): return Link(v) s:

slide-106
SLIDE 106

Adding to a Set Represented as an Ordered List

19

def add(s, v): """Add v to a set s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ if empty(s): return Link(v) if s.first > v: s.first, s.rest = __________________________ , _____________________________ s:

slide-107
SLIDE 107

Adding to a Set Represented as an Ordered List

19

def add(s, v): """Add v to a set s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ if empty(s): return Link(v) if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ s:

slide-108
SLIDE 108

Adding to a Set Represented as an Ordered List

19

def add(s, v): """Add v to a set s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ if empty(s): return Link(v) if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s s:

slide-109
SLIDE 109

Adding to a Set Represented as an Ordered List

19

def add(s, v): """Add v to a set s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ if empty(s): return Link(v) if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s v s:

slide-110
SLIDE 110

Adding to a Set Represented as an Ordered List

19

def add(s, v): """Add v to a set s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ if empty(s): return Link(v) if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s v Link(s.first, s.rest) s:

slide-111
SLIDE 111

Adding to a Set Represented as an Ordered List

19

def add(s, v): """Add v to a set s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ if empty(s): return Link(v) if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s v Link(s.first, s.rest) Link(v, s.rest) s:

slide-112
SLIDE 112

Adding to a Set Represented as an Ordered List

19

def add(s, v): """Add v to a set s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ if empty(s): return Link(v) if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s v Link(s.first, s.rest) add(s.rest, v) Link(v, s.rest) s:

slide-113
SLIDE 113

Adding to a Set Represented as an Ordered List

19

def add(s, v): """Add v to a set s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ if empty(s): return Link(v) if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s v Link(s.first, s.rest) add(s.rest, v) Link(v, s.rest) s: .