Decomposition Announcements Modular Design Separation of Concerns - - PowerPoint PPT Presentation

decomposition announcements modular design separation of
SMART_READER_LITE
LIVE PREVIEW

Decomposition Announcements Modular Design Separation of Concerns - - PowerPoint PPT Presentation

Decomposition Announcements Modular Design Separation of Concerns 4 Separation of Concerns A design principle: Isolate different parts of a program that address different concerns 4 Separation of Concerns A design principle: Isolate


slide-1
SLIDE 1

Decomposition

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Modular Design

slide-4
SLIDE 4

Separation of Concerns

4

slide-5
SLIDE 5

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns

4

slide-6
SLIDE 6

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

slide-7
SLIDE 7

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

Hog Game Simulator Game Commentary Player Strategies Hog

slide-8
SLIDE 8

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

  • Game rules

Hog Game Simulator Game Commentary Player Strategies Hog

slide-9
SLIDE 9

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

  • Game rules
  • Ordering of events

Hog Game Simulator Game Commentary Player Strategies Hog

slide-10
SLIDE 10

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

  • Game rules
  • Ordering of events
  • State tracking to

determine the winner Hog Game Simulator Game Commentary Player Strategies Hog

slide-11
SLIDE 11

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

  • Game rules
  • Ordering of events
  • State tracking to

determine the winner

  • Event descriptions

Hog Game Simulator Game Commentary Player Strategies Hog

slide-12
SLIDE 12

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

  • Game rules
  • Ordering of events
  • State tracking to

determine the winner

  • Event descriptions
  • State tracking to

generate commentary Hog Game Simulator Game Commentary Player Strategies Hog

slide-13
SLIDE 13

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

  • Game rules
  • Ordering of events
  • State tracking to

determine the winner

  • Event descriptions
  • State tracking to

generate commentary

  • Decision rules

Hog Game Simulator Game Commentary Player Strategies Hog

slide-14
SLIDE 14

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

  • Game rules
  • Ordering of events
  • State tracking to

determine the winner

  • Event descriptions
  • State tracking to

generate commentary

  • Decision rules
  • Strategy parameters

(e.g., margins &
 number of dice) Hog Game Simulator Game Commentary Player Strategies Hog

slide-15
SLIDE 15

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

  • Game rules
  • Ordering of events
  • State tracking to

determine the winner

  • Event descriptions
  • State tracking to

generate commentary

  • Decision rules
  • Strategy parameters

(e.g., margins &
 number of dice) Hog Game Simulator Game Commentary Player Strategies Hog

slide-16
SLIDE 16

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

  • Game rules
  • Ordering of events
  • State tracking to

determine the winner

  • Event descriptions
  • State tracking to

generate commentary

  • Decision rules
  • Strategy parameters

(e.g., margins &
 number of dice) Hog Game Simulator Game Commentary Player Strategies Hog Ants Game Simulator Actions Tunnel Structure Ants

slide-17
SLIDE 17

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

  • Game rules
  • Ordering of events
  • State tracking to

determine the winner

  • Event descriptions
  • State tracking to

generate commentary

  • Decision rules
  • Strategy parameters

(e.g., margins &
 number of dice)

  • Order of actions

Hog Game Simulator Game Commentary Player Strategies Hog Ants Game Simulator Actions Tunnel Structure Ants

slide-18
SLIDE 18

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

  • Game rules
  • Ordering of events
  • State tracking to

determine the winner

  • Event descriptions
  • State tracking to

generate commentary

  • Decision rules
  • Strategy parameters

(e.g., margins &
 number of dice)

  • Order of actions
  • Food tracking

Hog Game Simulator Game Commentary Player Strategies Hog Ants Game Simulator Actions Tunnel Structure Ants

slide-19
SLIDE 19

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

  • Game rules
  • Ordering of events
  • State tracking to

determine the winner

  • Event descriptions
  • State tracking to

generate commentary

  • Decision rules
  • Strategy parameters

(e.g., margins &
 number of dice)

  • Order of actions
  • Food tracking
  • Game ending conditions

Hog Game Simulator Game Commentary Player Strategies Hog Ants Game Simulator Actions Tunnel Structure Ants

slide-20
SLIDE 20

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

  • Game rules
  • Ordering of events
  • State tracking to

determine the winner

  • Event descriptions
  • State tracking to

generate commentary

  • Decision rules
  • Strategy parameters

(e.g., margins &
 number of dice)

  • Order of actions
  • Food tracking
  • Game ending conditions
  • Characteristics
  • f different

ants & bees Hog Game Simulator Game Commentary Player Strategies Hog Ants Game Simulator Actions Tunnel Structure Ants

slide-21
SLIDE 21

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

  • Game rules
  • Ordering of events
  • State tracking to

determine the winner

  • Event descriptions
  • State tracking to

generate commentary

  • Decision rules
  • Strategy parameters

(e.g., margins &
 number of dice)

  • Order of actions
  • Food tracking
  • Game ending conditions
  • Characteristics
  • f different

ants & bees

  • Entrances & exits

Hog Game Simulator Game Commentary Player Strategies Hog Ants Game Simulator Actions Tunnel Structure Ants

slide-22
SLIDE 22

Separation of Concerns

A design principle: Isolate different parts of a program that address different concerns A modular component can be developed and tested independently

4

  • Game rules
  • Ordering of events
  • State tracking to

determine the winner

  • Event descriptions
  • State tracking to

generate commentary

  • Decision rules
  • Strategy parameters

(e.g., margins &
 number of dice)

  • Order of actions
  • Food tracking
  • Game ending conditions
  • Characteristics
  • f different

ants & bees

  • Entrances & exits
  • Locations of insects

Hog Game Simulator Game Commentary Player Strategies Hog Ants Game Simulator Actions Tunnel Structure Ants

slide-23
SLIDE 23

Example: Restaurant Search

slide-24
SLIDE 24

Restaurant Search Data

Given the following data, look up a restaurant by name and show related restaurants.

6

slide-25
SLIDE 25

Restaurant Search Data

Given the following data, look up a restaurant by name and show related restaurants.

6

{"business_id": "gclB3ED6uk6viWlolSb_uA", "name": "Cafe 3", "stars": 2.0, "price": 1, ...}

slide-26
SLIDE 26

Restaurant Search Data

Given the following data, look up a restaurant by name and show related restaurants.

6

{"business_id": "gclB3ED6uk6viWlolSb_uA", "name": "Cafe 3", "stars": 2.0, "price": 1, ...} {"business_id": "WXKx2I2SEzBpeUGtDMCS8A", "name": "La Cascada Taqueria", "stars": 3.0, "price": 2}

slide-27
SLIDE 27

Restaurant Search Data

Given the following data, look up a restaurant by name and show related restaurants.

6

{"business_id": "gclB3ED6uk6viWlolSb_uA", "name": "Cafe 3", "stars": 2.0, "price": 1, ...} {"business_id": "WXKx2I2SEzBpeUGtDMCS8A", "name": "La Cascada Taqueria", "stars": 3.0, "price": 2} ...

slide-28
SLIDE 28

Restaurant Search Data

Given the following data, look up a restaurant by name and show related restaurants.

6

{"business_id": "gclB3ED6uk6viWlolSb_uA", "name": "Cafe 3", "stars": 2.0, "price": 1, ...} {"business_id": "WXKx2I2SEzBpeUGtDMCS8A", "name": "La Cascada Taqueria", "stars": 3.0, "price": 2} ... {"business_id": "gclB3ED6uk6viWlolSb_uA", "user_id": "xVocUszkZtAqCxgWak3xVQ", "stars": 1, "text":
 "Cafe 3 (or Cafe Tre, as I like to say) used to be the bomb diggity when I first lived in the dorms
 but sadly, quality has dramatically decreased over the years....", "date": "2012-01-19", ...}

slide-29
SLIDE 29

Restaurant Search Data

Given the following data, look up a restaurant by name and show related restaurants.

6

{"business_id": "gclB3ED6uk6viWlolSb_uA", "name": "Cafe 3", "stars": 2.0, "price": 1, ...} {"business_id": "WXKx2I2SEzBpeUGtDMCS8A", "name": "La Cascada Taqueria", "stars": 3.0, "price": 2} ... {"business_id": "gclB3ED6uk6viWlolSb_uA", "user_id": "xVocUszkZtAqCxgWak3xVQ", "stars": 1, "text":
 "Cafe 3 (or Cafe Tre, as I like to say) used to be the bomb diggity when I first lived in the dorms
 but sadly, quality has dramatically decreased over the years....", "date": "2012-01-19", ...} {"business_id": "WXKx2I2SEzBpeUGtDMCS8A", "user_id": "84dCHkhWG8IDtk30VvaY5A", "stars": 2, "text":
 "-Excuse me for being a snob but if I wanted a room temperature burrito I would take one home,
 stick it in the fridge for a day, throw it in the microwave for 45 seconds, then eat it. NOT go to
 a resturant and pay like seven dollars for one...", "date": "2009-04-30", ...}

slide-30
SLIDE 30

Restaurant Search Data

Given the following data, look up a restaurant by name and show related restaurants.

6

{"business_id": "gclB3ED6uk6viWlolSb_uA", "name": "Cafe 3", "stars": 2.0, "price": 1, ...} {"business_id": "WXKx2I2SEzBpeUGtDMCS8A", "name": "La Cascada Taqueria", "stars": 3.0, "price": 2} ... {"business_id": "gclB3ED6uk6viWlolSb_uA", "user_id": "xVocUszkZtAqCxgWak3xVQ", "stars": 1, "text":
 "Cafe 3 (or Cafe Tre, as I like to say) used to be the bomb diggity when I first lived in the dorms
 but sadly, quality has dramatically decreased over the years....", "date": "2012-01-19", ...} {"business_id": "WXKx2I2SEzBpeUGtDMCS8A", "user_id": "84dCHkhWG8IDtk30VvaY5A", "stars": 2, "text":
 "-Excuse me for being a snob but if I wanted a room temperature burrito I would take one home,
 stick it in the fridge for a day, throw it in the microwave for 45 seconds, then eat it. NOT go to
 a resturant and pay like seven dollars for one...", "date": "2009-04-30", ...} ...

slide-31
SLIDE 31

Restaurant Search Data

Given the following data, look up a restaurant by name and show related restaurants.

6

(Demo) {"business_id": "gclB3ED6uk6viWlolSb_uA", "name": "Cafe 3", "stars": 2.0, "price": 1, ...} {"business_id": "WXKx2I2SEzBpeUGtDMCS8A", "name": "La Cascada Taqueria", "stars": 3.0, "price": 2} ... {"business_id": "gclB3ED6uk6viWlolSb_uA", "user_id": "xVocUszkZtAqCxgWak3xVQ", "stars": 1, "text":
 "Cafe 3 (or Cafe Tre, as I like to say) used to be the bomb diggity when I first lived in the dorms
 but sadly, quality has dramatically decreased over the years....", "date": "2012-01-19", ...} {"business_id": "WXKx2I2SEzBpeUGtDMCS8A", "user_id": "84dCHkhWG8IDtk30VvaY5A", "stars": 2, "text":
 "-Excuse me for being a snob but if I wanted a room temperature burrito I would take one home,
 stick it in the fridge for a day, throw it in the microwave for 45 seconds, then eat it. NOT go to
 a resturant and pay like seven dollars for one...", "date": "2009-04-30", ...} ...

slide-32
SLIDE 32

Example: Similar Restaurants

slide-33
SLIDE 33

Discussion Question: Most Similar Restaurants

8

slide-34
SLIDE 34

Discussion Question: Most Similar Restaurants

Implement similar, a Restaurant method that takes a positive integer k and a function similarity that takes two restaurants as arguments and returns a number. Higher similarity values indicate more similar restaurants. The similar method returns a list containing the k most similar restaurants according to the similarity function, but not containing self.

8

slide-35
SLIDE 35

Discussion Question: Most Similar Restaurants

Implement similar, a Restaurant method that takes a positive integer k and a function similarity that takes two restaurants as arguments and returns a number. Higher similarity values indicate more similar restaurants. The similar method returns a list containing the k most similar restaurants according to the similarity function, but not containing self. def similar(self, k, similarity):

8

slide-36
SLIDE 36

Discussion Question: Most Similar Restaurants

Implement similar, a Restaurant method that takes a positive integer k and a function similarity that takes two restaurants as arguments and returns a number. Higher similarity values indicate more similar restaurants. The similar method returns a list containing the k most similar restaurants according to the similarity function, but not containing self. def similar(self, k, similarity):

"Return the K most similar restaurants to SELF, using SIMILARITY for comparison."

8

slide-37
SLIDE 37

Discussion Question: Most Similar Restaurants

Implement similar, a Restaurant method that takes a positive integer k and a function similarity that takes two restaurants as arguments and returns a number. Higher similarity values indicate more similar restaurants. The similar method returns a list containing the k most similar restaurants according to the similarity function, but not containing self. def similar(self, k, similarity):

"Return the K most similar restaurants to SELF, using SIMILARITY for comparison."

  • thers = list(Restaurant.all)

8

slide-38
SLIDE 38

Discussion Question: Most Similar Restaurants

Implement similar, a Restaurant method that takes a positive integer k and a function similarity that takes two restaurants as arguments and returns a number. Higher similarity values indicate more similar restaurants. The similar method returns a list containing the k most similar restaurants according to the similarity function, but not containing self. def similar(self, k, similarity):

"Return the K most similar restaurants to SELF, using SIMILARITY for comparison."

  • thers = list(Restaurant.all)
  • thers.______________(______________)

8

slide-39
SLIDE 39

Discussion Question: Most Similar Restaurants

Implement similar, a Restaurant method that takes a positive integer k and a function similarity that takes two restaurants as arguments and returns a number. Higher similarity values indicate more similar restaurants. The similar method returns a list containing the k most similar restaurants according to the similarity function, but not containing self. def similar(self, k, similarity):

"Return the K most similar restaurants to SELF, using SIMILARITY for comparison."

  • thers = list(Restaurant.all)
  • thers.______________(______________)

return sorted(others, key=________________________________________)__________________

8

slide-40
SLIDE 40

Discussion Question: Most Similar Restaurants

Implement similar, a Restaurant method that takes a positive integer k and a function similarity that takes two restaurants as arguments and returns a number. Higher similarity values indicate more similar restaurants. The similar method returns a list containing the k most similar restaurants according to the similarity function, but not containing self. def similar(self, k, similarity):

"Return the K most similar restaurants to SELF, using SIMILARITY for comparison."

  • thers = list(Restaurant.all)
  • thers.______________(______________)

return sorted(others, key=________________________________________)__________________

8

sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.

slide-41
SLIDE 41

Discussion Question: Most Similar Restaurants

Implement similar, a Restaurant method that takes a positive integer k and a function similarity that takes two restaurants as arguments and returns a number. Higher similarity values indicate more similar restaurants. The similar method returns a list containing the k most similar restaurants according to the similarity function, but not containing self. def similar(self, k, similarity):

"Return the K most similar restaurants to SELF, using SIMILARITY for comparison."

  • thers = list(Restaurant.all)
  • thers.______________(______________)

return sorted(others, key=________________________________________)__________________

8

remove sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.

slide-42
SLIDE 42

Discussion Question: Most Similar Restaurants

Implement similar, a Restaurant method that takes a positive integer k and a function similarity that takes two restaurants as arguments and returns a number. Higher similarity values indicate more similar restaurants. The similar method returns a list containing the k most similar restaurants according to the similarity function, but not containing self. def similar(self, k, similarity):

"Return the K most similar restaurants to SELF, using SIMILARITY for comparison."

  • thers = list(Restaurant.all)
  • thers.______________(______________)

return sorted(others, key=________________________________________)__________________

8

remove self sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.

slide-43
SLIDE 43

Discussion Question: Most Similar Restaurants

Implement similar, a Restaurant method that takes a positive integer k and a function similarity that takes two restaurants as arguments and returns a number. Higher similarity values indicate more similar restaurants. The similar method returns a list containing the k most similar restaurants according to the similarity function, but not containing self. def similar(self, k, similarity):

"Return the K most similar restaurants to SELF, using SIMILARITY for comparison."

  • thers = list(Restaurant.all)
  • thers.______________(______________)

return sorted(others, key=________________________________________)__________________

8

remove self lambda r: -similarity(self, r) sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.

slide-44
SLIDE 44

Discussion Question: Most Similar Restaurants

Implement similar, a Restaurant method that takes a positive integer k and a function similarity that takes two restaurants as arguments and returns a number. Higher similarity values indicate more similar restaurants. The similar method returns a list containing the k most similar restaurants according to the similarity function, but not containing self. def similar(self, k, similarity):

"Return the K most similar restaurants to SELF, using SIMILARITY for comparison."

  • thers = list(Restaurant.all)
  • thers.______________(______________)

return sorted(others, key=________________________________________)__________________

8

remove self lambda r: -similarity(self, r) [:k] sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.

slide-45
SLIDE 45

Example: Reading Files

(Demo)

slide-46
SLIDE 46

Set Intersection

slide-47
SLIDE 47

Linear-Time Intersection of Sorted Lists

11

Given two sorted lists with no repeats, return the number of elements that appear in both.

slide-48
SLIDE 48

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 Given two sorted lists with no repeats, return the number of elements that appear in both.

slide-49
SLIDE 49

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 1 3 5 7 8 Given two sorted lists with no repeats, return the number of elements that appear in both.

slide-50
SLIDE 50

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 1 3 5 7 8 Given two sorted lists with no repeats, return the number of elements that appear in both.

slide-51
SLIDE 51

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 1 3 5 7 8 Given two sorted lists with no repeats, return the number of elements that appear in both.

slide-52
SLIDE 52

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 1 3 5 7 8 Given two sorted lists with no repeats, return the number of elements that appear in both.

slide-53
SLIDE 53

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 1 3 5 7 8 Given two sorted lists with no repeats, return the number of elements that appear in both.

slide-54
SLIDE 54

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 1 3 5 7 8 Given two sorted lists with no repeats, return the number of elements that appear in both.

slide-55
SLIDE 55

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 1 3 5 7 8 Given two sorted lists with no repeats, return the number of elements that appear in both.

slide-56
SLIDE 56

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 1 3 5 7 8 Given two sorted lists with no repeats, return the number of elements that appear in both.

slide-57
SLIDE 57

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 1 3 5 7 8 Given two sorted lists with no repeats, return the number of elements that appear in both.

slide-58
SLIDE 58

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 1 3 5 7 8 Given two sorted lists with no repeats, return the number of elements that appear in both.

def fast_overlap(s, t): """Return the overlap between sorted S and sorted T. >>> fast_overlap([3, 4, 6, 7, 9, 10], [1, 3, 5, 7, 8]) 2 """ i, j, count = 0, 0, 0 while ____________________________________________: if s[i] == t[j]: count, i, j = ____________________________ elif s[i] < t[j]: __________________________________________ else: __________________________________________ return count

slide-59
SLIDE 59

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 1 3 5 7 8 Given two sorted lists with no repeats, return the number of elements that appear in both.

def fast_overlap(s, t): """Return the overlap between sorted S and sorted T. >>> fast_overlap([3, 4, 6, 7, 9, 10], [1, 3, 5, 7, 8]) 2 """ i, j, count = 0, 0, 0 while ____________________________________________: if s[i] == t[j]: count, i, j = ____________________________ elif s[i] < t[j]: __________________________________________ else: __________________________________________ return count i < len(s) and j < len(t)

slide-60
SLIDE 60

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 1 3 5 7 8 Given two sorted lists with no repeats, return the number of elements that appear in both.

def fast_overlap(s, t): """Return the overlap between sorted S and sorted T. >>> fast_overlap([3, 4, 6, 7, 9, 10], [1, 3, 5, 7, 8]) 2 """ i, j, count = 0, 0, 0 while ____________________________________________: if s[i] == t[j]: count, i, j = ____________________________ elif s[i] < t[j]: __________________________________________ else: __________________________________________ return count i < len(s) and j < len(t) count + 1, i + 1, j + 1

slide-61
SLIDE 61

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 1 3 5 7 8 Given two sorted lists with no repeats, return the number of elements that appear in both.

def fast_overlap(s, t): """Return the overlap between sorted S and sorted T. >>> fast_overlap([3, 4, 6, 7, 9, 10], [1, 3, 5, 7, 8]) 2 """ i, j, count = 0, 0, 0 while ____________________________________________: if s[i] == t[j]: count, i, j = ____________________________ elif s[i] < t[j]: __________________________________________ else: __________________________________________ return count i < len(s) and j < len(t) count + 1, i + 1, j + 1 i = i + 1

slide-62
SLIDE 62

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 1 3 5 7 8 Given two sorted lists with no repeats, return the number of elements that appear in both.

def fast_overlap(s, t): """Return the overlap between sorted S and sorted T. >>> fast_overlap([3, 4, 6, 7, 9, 10], [1, 3, 5, 7, 8]) 2 """ i, j, count = 0, 0, 0 while ____________________________________________: if s[i] == t[j]: count, i, j = ____________________________ elif s[i] < t[j]: __________________________________________ else: __________________________________________ return count i < len(s) and j < len(t) count + 1, i + 1, j + 1 i = i + 1 j = j + 1

slide-63
SLIDE 63

Linear-Time Intersection of Sorted Lists

11

3 4 6 7 9 10 1 3 5 7 8 Given two sorted lists with no repeats, return the number of elements that appear in both.

def fast_overlap(s, t): """Return the overlap between sorted S and sorted T. >>> fast_overlap([3, 4, 6, 7, 9, 10], [1, 3, 5, 7, 8]) 2 """ i, j, count = 0, 0, 0 while ____________________________________________: if s[i] == t[j]: count, i, j = ____________________________ elif s[i] < t[j]: __________________________________________ else: __________________________________________ return count i < len(s) and j < len(t) count + 1, i + 1, j + 1 i = i + 1 j = j + 1

(Demo)

slide-64
SLIDE 64

Sets

slide-65
SLIDE 65

Sets

13

slide-66
SLIDE 66

Sets

One more built-in Python container type

13

slide-67
SLIDE 67

Sets

One more built-in Python container type

  • Set literals are enclosed in braces

13

slide-68
SLIDE 68

Sets

One more built-in Python container type

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

13

slide-69
SLIDE 69

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order

13

slide-70
SLIDE 70

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order

>>> s = {'one', 'two', 'three', 'four', 'four'}

13

slide-71
SLIDE 71

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order

>>> s = {'one', 'two', 'three', 'four', 'four'} >>> s {'three', 'one', 'four', 'two'}

13

slide-72
SLIDE 72

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order

>>> s = {'one', 'two', 'three', 'four', 'four'} >>> s {'three', 'one', 'four', 'two'} >>> 'three' in s True

13

slide-73
SLIDE 73

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order

>>> s = {'one', 'two', 'three', 'four', 'four'} >>> s {'three', 'one', 'four', 'two'} >>> 'three' in s True >>> len(s) 4

13

slide-74
SLIDE 74

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order

>>> s = {'one', 'two', 'three', 'four', 'four'} >>> s {'three', 'one', 'four', 'two'} >>> 'three' in s True >>> len(s) 4 >>> s.union({'one', 'five'}) {'three', 'five', 'one', 'four', 'two'}

13

slide-75
SLIDE 75

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order

>>> s = {'one', 'two', 'three', 'four', 'four'} >>> s {'three', 'one', 'four', 'two'} >>> 'three' in s True >>> len(s) 4 >>> s.union({'one', 'five'}) {'three', 'five', 'one', 'four', 'two'} >>> s.intersection({'six', 'five', 'four', 'three'}) {'three', 'four'}

13

slide-76
SLIDE 76

Sets

One more built-in Python container type

  • Set literals are enclosed in braces
  • Duplicate elements are removed on construction
  • Sets have arbitrary order

>>> s = {'one', 'two', 'three', 'four', 'four'} >>> s {'three', 'one', 'four', 'two'} >>> 'three' in s True >>> len(s) 4 >>> s.union({'one', 'five'}) {'three', 'five', 'one', 'four', 'two'} >>> s.intersection({'six', 'five', 'four', 'three'}) {'three', 'four'} >>> s {'three', 'one', 'four', 'two'}

13