SLIDE 1
61A Lecture 20 Announcements Sets Sets >>> import re - - PowerPoint PPT Presentation
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 2
SLIDE 3
Sets
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
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
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
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
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
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
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
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
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
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
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
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
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
Implementing Sets
5
SLIDE 18
Implementing Sets
What we should be able to do with a set:
5
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
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
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
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
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
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
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
Sets as Linked Lists
SLIDE 27
Sets as Unordered Sequences
Proposal 1: A set is represented by a linked list that contains no duplicate items.
7
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
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
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
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
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
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
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
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
Sets as Unordered Sequences
8
SLIDE 37
Sets as Unordered Sequences
8
def adjoin(s, v): if contains(s, v): return s else: return Link(v, s)
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
Θ(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
Θ(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
Θ(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
Θ(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
Θ(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
Θ(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
Θ(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
Θ(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
Θ(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
Sets as Ordered Linked Lists
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
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
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
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
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
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
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
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
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
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
Searching an Ordered List
11
SLIDE 60
Searching an Ordered List
11
>>> s = Link(1, Link(3, Link(5)))
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Set Operations
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
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
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
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
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
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
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
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
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
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
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
Set Mutation
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
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
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
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
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
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
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
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
Adding to a Set Represented as an Ordered List
19
s:
SLIDE 98
Adding to a Set Represented as an Ordered List
19
def add(s, v): s:
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Adding to a Set Represented as an Ordered List
19