Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle:...
Transcript of Decomposition - University of California, Berkeleycs61a/sp20/assets/... · A design principle:...
![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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/4.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/13.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/21.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/22.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/23.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/24.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/26.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/27.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/28.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/29.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/30.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/31.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/32.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/33.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/34.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/35.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/36.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/37.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/38.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/39.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/40.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/41.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/42.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/43.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/44.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/45.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/46.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/47.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/48.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/49.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/50.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/51.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/52.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/53.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/54.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/55.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/56.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/57.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/58.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/59.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/60.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/61.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/62.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/63.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/64.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/65.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/66.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/67.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/68.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/69.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/70.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/71.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/72.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/73.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/74.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/75.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051915/600757a524e45613126c83df/html5/thumbnails/76.jpg)
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