forked from Ada-C14/ride-share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worksheet.rb
214 lines (194 loc) · 6.4 KB
/
worksheet.rb
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
trips = {
"DR0004": [
{
date: "3rd Feb 2016",
cost: 5,
rider_id: "RD0022",
rating: 5
},
{
date: "4th Feb 2016",
cost: 10,
rider_id: "RD0022",
rating: 4
},
{
date: "5th Feb 2016",
cost: 20 ,
rider_id: "RD0073",
rating: 5
}
],
"DR0001": [
{
date: "3rd Feb 2016",
cost: 10,
rider_id: "RD0003",
rating: 3
},
{
date: "3rd Feb 2016",
cost: 30,
rider_id: "RD0015",
rating: 4
},
{
date: "5th Feb 2016",
cost: 45,
rider_id: "RD0003",
rating: 2
}
],
"DR0002": [
{
date: "3rd Feb 2016 ",
cost: 25,
rider_id: "RD0073",
rating: 5,
},
{
date: "4th Feb 2016 ",
cost: 15,
rider_id: "RD0013",
rating: 1,
},
{
date: "5th Feb 2016",
cost: 35,
rider_id: "RD0066",
rating: 3,
}
],
"DR0003": [
{
date: "4th Feb 2016",
cost: 5,
rider_id: "RD0066",
rating: 5,
},
{
date: "5th Feb 2016 ",
cost: 50,
rider_id: "RD0003",
rating: 2,
}
]
}
# METHODS
#method to figure out number of rides each driver has given
def num_of_rides(trips)
return trips.map do |driver, trip|
"Driver #{driver} made #{trip.length} rides"
end
end
# method for total amount of money each driver has made
def total_earned(trips)
result = trips.map do |driver, trip|
amount = trip.map { |ride| ride[:cost]}.sum
{ :driver => driver.to_s, :amount => amount}
end
return result
end
# method for average rating for each driver
def average_rating(trips)
result = trips.map do |driver, trip|
amount = trip.map { |ride| ride[:rating]}.sum
average_rating = (amount.to_f/trip.size).round(2)
{:driver => driver, :avg_rating => average_rating}
end
return result
end
# method to find Which Driver made the most money
def driver_with_cash(total_earned)
return total_earned.max_by { |driver_earning| driver_earning[:amount]}
end
# method to find which driver had the highest rating
def driver_with_highest_rating(average_rating)
return average_rating.max_by { |driver_rating| driver_rating[:avg_rating]}
end
#CALLING my methods below:
# calling the num_of_rides method
puts "Driver and how many trips they made:"
puts ""
puts num_of_rides(trips)
# calling the total_earned method to see how much each driver made
puts "-------------------------------------"
puts "Driver and total amount of money they earned:"
puts ""
array = total_earned(trips)
array.each do |hash|
puts "#{hash[:driver]} made $#{hash[:amount]}"
end
# calling the average_rating method to see the average rating of each driver
puts "-------------------------------------"
puts "Driver and their average rating:"
puts ""
avg_rating_array = average_rating(trips)
avg_rating_array.each do |driver_and_rating|
puts "#{driver_and_rating[:driver]} had an average rating of #{driver_and_rating[:avg_rating]}"
end
#calling my driver_with_cash method to print out which driver made the most money
puts "-------------------------------------"
puts "Which driver made the most money?"
puts ""
most_income = driver_with_cash(total_earned(trips))
most_income.each do |driver, value|
puts "#{most_income[:driver]} made the most money at $#{most_income[:amount]}"
break
end
#calling my driver_with_highest_rating method to print out which driver had the highest avg_rating
puts "-------------------------------------"
puts "Which driver had the highest rating?"
puts ""
highest_rating = driver_with_highest_rating(average_rating(trips))
highest_rating.each do |driver, value|
puts "#{highest_rating[:driver]} had the highest average rating of #{highest_rating[:avg_rating]}"
break
end
#finding which date each driver made the most money on
puts "-------------------------------------"
puts "Which date did each driver make the most income on?"
puts ""
trips.map do |driver, rides|
puts "#{driver} made the most money on #{rides.max_by {|ride| ride[:cost]}[:date]}"
end
########################################################
# Step 1: Establish the layers
# In this section of the file, as a series of comments,
# create a list of the layers you identify.
#
# things: driver_id, cost, date, rating, rider_id, rides
#
# Which layers are nested in each other?
#
# Trips is a hash, where the key is the driver_id the the value is another array. In the array, each element is a hash, representing each ride that the driver did.
# The hash have a key of the other columns (cost, rating, rider_id, and date) while the value is the specific value of it
#
# Which layers of data "have" within it a different layer?
# the trips layer have an array inside of it. and the array have another hash inside of it
#
# Which layers are "next" to each other?
#
# the trips hash is next to the ride array which is next to the hash with the other 4 keys and values
########################################################
# Step 2: Assign a data structure to each layer
# Copy your list from above, and in this section
# determine what data structure each layer should have
# things: driver_id, cost, date, rating, rider_id, rides
# driver_id is the key to the variable trips, and its value is an array (each ride), in the array, each element is a hash with the keys being cost, date, rating, and rider_id and its values
########################################################
# Step 3: Make the data structure!
# Setup the entire data structure:
# based off of the notes you have above, create the
# and manually write in data presented in rides.csv
# You should be copying and pasting the literal data
# into this data structure, such as "DR0004"
# and "3rd Feb 2016" and "RD0022"
########################################################
# Step 4: Total Driver's Earnings and Number of Rides
# Use an iteration blocks to print the following answers:
# - the number of rides each driver has given
# - the total amount of money each driver has made
# - the average rating for each driver
# - Which driver made the most money?
# - Which driver has the highest average rating?