-
Notifications
You must be signed in to change notification settings - Fork 2
/
abstractions.py
99 lines (74 loc) · 2.83 KB
/
abstractions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""Data Abstractions"""
from utils import mean
#############################
# Phase 1: Data Abstraction #
#############################
# Reviews
def make_review(restaurant_name, rating):
"""Return a review data abstraction."""
return [restaurant_name, rating]
def review_restaurant_name(review):
"""Return the restaurant name of the review, which is a string."""
return review[0]
def review_rating(review):
"""Return the number of stars given by the review, which is a
floating point number between 1 and 5."""
return review[1]
# Users
def make_user(name, reviews):
"""Return a user data abstraction."""
return [name, {review_restaurant_name(r): r for r in reviews}]
def user_name(user):
"""Return the name of the user, which is a string."""
return user[0]
def user_reviews(user):
"""Return a dictionary from restaurant names to reviews by the user."""
return user[1]
### === +++ USER ABSTRACTION BARRIER +++ === ###
def user_reviewed_restaurants(user, restaurants):
"""Return the subset of restaurants reviewed by user.
Arguments:
user -- a user
restaurants -- a list of restaurant data abstractions
"""
names = list(user_reviews(user))
return [r for r in restaurants if restaurant_name(r) in names]
def user_rating(user, restaurant_name):
"""Return the rating given for restaurant_name by user."""
reviewed_by_user = user_reviews(user)
user_review = reviewed_by_user[restaurant_name]
return review_rating(user_review)
# Restaurants
def make_restaurant(name, location, categories, price, reviews):
"""Return a restaurant data abstraction containing the name, location,
categories, price, and reviews for that restaurant."""
# BEGIN Question 2
return [name, location, categories, price, [review_restaurant_name(x) for x in reviews], [review_rating(x) for x in reviews]]
# END Question 2
def restaurant_name(restaurant):
"""Return the name of the restaurant, which is a string."""
# BEGIN Question 2
return restaurant[0]
# END Question 2
def restaurant_location(restaurant):
"""Return the location of the restaurant, which is a list containing
latitude and longitude."""
# BEGIN Question 2
return restaurant[1]
# END Question 2
def restaurant_categories(restaurant):
"""Return the categories of the restaurant, which is a list of strings."""
# BEGIN Question 2
return restaurant[2]
# END Question 2
def restaurant_price(restaurant):
"""Return the price of the restaurant, which is a number."""
# BEGIN Question 2
return restaurant[3]
# END Question 2
def restaurant_ratings(restaurant):
"""Return a list of ratings, which are numbers from 1 to 5, of the
restaurant based on the reviews of the restaurant."""
# BEGIN Question 2
return restaurant[5]
# END Question 2