decomposition - university of california, berkeleycs61a/sp20/assets/... · a design principle:...

76
Decomposition

Upload: others

Post on 24-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Decomposition

Page 2: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Announcements

Page 3: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Modular Design

Page 4: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Separation of Concerns

4

Page 5: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Separation of Concerns

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

4

Page 6: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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

Page 7: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 StrategiesHog

Page 8: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 StrategiesHog

Page 9: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 StrategiesHog

Page 10: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 StrategiesHog

Page 11: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 StrategiesHog

Page 12: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 StrategiesHog

Page 13: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 StrategiesHog

Page 14: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 StrategiesHog

Page 15: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 StrategiesHog

Page 16: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 StrategiesHog

Ants Game Simulator Actions Tunnel

StructureAnts

Page 17: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 StrategiesHog

Ants Game Simulator Actions Tunnel

StructureAnts

Page 18: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 StrategiesHog

Ants Game Simulator Actions Tunnel

StructureAnts

Page 19: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 StrategiesHog

Ants Game Simulator Actions Tunnel

StructureAnts

Page 20: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 of different ants & bees

Hog Game Simulator

Game Commentary

Player StrategiesHog

Ants Game Simulator Actions Tunnel

StructureAnts

Page 21: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 of different ants & bees

•Entrances & exits

Hog Game Simulator

Game Commentary

Player StrategiesHog

Ants Game Simulator Actions Tunnel

StructureAnts

Page 22: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 of different ants & bees

•Entrances & exits•Locations of insects

Hog Game Simulator

Game Commentary

Player StrategiesHog

Ants Game Simulator Actions Tunnel

StructureAnts

Page 23: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Example: Restaurant Search

Page 24: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Restaurant Search Data

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

6

Page 25: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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, ...}

Page 26: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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}

Page 27: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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}

...

Page 28: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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", ...}

Page 29: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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", ...}

Page 30: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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", ...}

...

Page 31: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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", ...}

...

Page 32: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Example: Similar Restaurants

Page 33: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Discussion Question: Most Similar Restaurants

8

Page 34: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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

Page 35: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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

Page 36: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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

Page 37: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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."

others = list(Restaurant.all)

8

Page 38: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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."

others = list(Restaurant.all)

others.______________(______________)

8

Page 39: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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."

others = list(Restaurant.all)

others.______________(______________)

return sorted(others, key=________________________________________)__________________

8

Page 40: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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."

others = list(Restaurant.all)

others.______________(______________)

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.

Page 41: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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."

others = list(Restaurant.all)

others.______________(______________)

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.

Page 42: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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."

others = list(Restaurant.all)

others.______________(______________)

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.

Page 43: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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."

others = list(Restaurant.all)

others.______________(______________)

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.

Page 44: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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."

others = list(Restaurant.all)

others.______________(______________)

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.

Page 45: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Example: Reading Files

(Demo)

Page 46: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Set Intersection

Page 47: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Linear-Time Intersection of Sorted Lists

11

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

Page 48: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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.

Page 49: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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.

Page 50: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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.

Page 51: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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.

Page 52: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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.

Page 53: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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.

Page 54: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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.

Page 55: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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.

Page 56: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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.

Page 57: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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.

Page 58: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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

Page 59: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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)

Page 60: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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

Page 61: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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

Page 62: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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

Page 63: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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)

Page 64: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Sets

Page 65: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Sets

13

Page 66: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Sets

One more built-in Python container type

13

Page 67: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Sets

One more built-in Python container type

• Set literals are enclosed in braces

13

Page 68: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

Sets

One more built-in Python container type

• Set literals are enclosed in braces

• Duplicate elements are removed on construction

13

Page 69: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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

Page 70: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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

Page 71: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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

Page 72: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 sTrue

13

Page 73: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 sTrue>>> len(s)4

13

Page 74: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 sTrue>>> len(s)4>>> s.union({'one', 'five'}){'three', 'five', 'one', 'four', 'two'}

13

Page 75: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 sTrue>>> len(s)4>>> s.union({'one', 'five'}){'three', 'five', 'one', 'four', 'two'}>>> s.intersection({'six', 'five', 'four', 'three'}){'three', 'four'}

13

Page 76: Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle: Isolate different parts of a program that address different concerns A modular component

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 sTrue>>> 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