forked from Ada-C10/ride-share
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathride-share-data.rb
More file actions
320 lines (284 loc) · 6.97 KB
/
ride-share-data.rb
File metadata and controls
320 lines (284 loc) · 6.97 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
########################################################
# Step 1: Establish the layers
# Write a list of the layers here
# Driver_ID (string)
# Date (string)
# Cost (float)
# Rider_ID (string)
# Rating (float)
########################################################
# Step 2: Assign a data structure to each layer
# Copy your list from above, and write what data structure each layer should have
# Write a list of the layers here
# Driver_ID - Hash
# Date - Hash
# Cost - hash within an array
# Rider_ID - hash within an array
# Rating array hash within an array
########################################################
# Step 3: Make the data structure!
# Setup the data structure and manually write in data presented in rides.csv
# {
# driver_id: {
# date: [
# {
# cost:
# rider_id:
# rating:
# }
# ]
# }
# }
ride_share_info = {
"DR0004" => {
"3RD FEB 2016" => [
{
cost: 5,
rider_id: "RD0022",
rating: 5
}
],
"4TH FEB 2016" => [
{
cost: 10,
rider_id: "RD0022",
rating: 4
}
],
"5TH FEB 2016" => [
{
cost: 20,
rider_id: "RD0073",
rating: 5
}
]
},
"DR0001" => {
"3RD FEB 2016" => [
{
cost: 10,
rider_id: "RD0003",
rating: 3
},
{
cost: 30,
rider_id: "RD0015",
rating: 4
}
],
"5TH FEB 2016" => [
{
cost: 45,
rider_id: "RD0003",
rating: 2
}
]
},
"DR0002" => {
"3RD FEB 2016" => [
{
cost: 25,
rider_id: "RD0073",
rating: 5
}
],
"4TH FEB 2016" => [
{
cost: 15,
rider_id: "RD0013",
rating: 1
}
],
"5TH FEB 2016" => [
{
cost: 35,
rider_id: "RD0066",
rating: 3
}
]
},
"DR0003" => {
"4TH FEB 2016" => [
{
cost: 5,
rider_id:"RD0066",
rating: 5
},
],
"5TH FEB 2016" => [
{
cost: 50,
rider_id: "RD0003",
rating: 2
}
]
}
}
########################################################
# Step 4: Total Driver's Earnings and Number of Rides
# Use an iteration block to print driver's total rides and money made
# get total rides
def driver_total_rides(info)
# goal: make an array of hashes [{driver: total_ride_count}]
driver_info = info.map do |driver, rides|
driver_and_rides = {}
# get total ride count per driver
total_ride_count = rides.map { |date, each_ride| each_ride.length}.reduce(:+)
# create hash for each driver
driver_and_rides[driver] = total_ride_count
# add hash to the array
driver_and_rides
end
# return value is the array
return driver_info
end
def total_amount_per_driver(info)
# goal: make an array of hashes [{driver: total_amount}]
# get total amount made per driver
driver_info = info.map do |driver, rides|
driver_and_amount = {}
total_amount = 0
rides.each do |date, each_ride|
each_ride.each do |amount|
total_amount += amount[:cost]
end
# create hash for each driver
driver_and_amount[driver] = total_amount
end
# add hash into the array
driver_and_amount
end
# return value is the array
return driver_info
end
def average_rating(info)
# goal: make an array of hashes [{driver: avg_rating}]
# get average rating per driver
driver_info = info.map do |driver, rides|
driver_and_rating = {}
average = 0.0
total_ride_count = 0.0
total_rate = 0.0
# collect total ride count per driver
rides.each do |date, each_ride|
ride_count = each_ride.length
total_ride_count += ride_count
# collect total rate per driver
each_ride.each do |rate|
total_rate += rate[:rating]
end
# calculate avg, create hash for each driver
average = total_rate/total_ride_count
driver_and_rating[driver] = average
end
# add hash into the array
driver_and_rating
end
# return value is the array
return driver_info
end
each_driver_rides = driver_total_rides(ride_share_info)
each_driver_total = total_amount_per_driver(ride_share_info)
each_driver_rating = average_rating(ride_share_info)
def driver_most_amount(each_driver)
# goal: get the driver ID that raised most amount
raised_most = nil
highest_amount = 0
# find highest amount, assign the corresponding driver to raised_most
each_driver.each do |info|
info.each do |driver, amount|
if amount > highest_amount
highest_amount = amount
raised_most = driver
end
end
end
# return value is the string assigned to raised_most (driver ID)
return raised_most
end
def driver_highest_rating(each_driver)
# goal: get the driver ID that had the highest avg rating
rated_highest = nil
highest_rating = 0
# find the highest avg rating, assign the corresponding driver to rated_highest
each_driver.each do |info|
info.each do |driver, rating|
if rating > highest_rating
highest_rating = rating
rated_highest = driver
end
end
end
# return value is the string assigned to rated_highest (driver ID)
return rated_highest
end
def day_highest_earned(info)
# goal: make an array of hashes [{driver: date}]
# find highest amount made per day, per driver
driver_info = info.map do |driver, rides|
driver_and_day = {}
cost_per_ride = 0
raised_most_date = nil
rides.each do |date, each_ride|
# assign the corresponding date to the highest amount made
each_ride.each do |amount|
if amount[:cost] > cost_per_ride
cost_per_ride = amount[:cost]
raised_most_date = date
end
end
# create hash for each driver
driver_and_day[driver] = raised_most_date
end
# add each hash into the array
driver_and_day
end
# return value is the array
return driver_info
end
# Output message
puts
puts "=" * 80
puts "\t\t\t\tRIDE SHARE INFO"
puts "=" * 80
puts
puts "TOTAL RIDES PER DRIVER: "
each_driver_rides.each do |info|
info.each do |driver, total_rides|
puts "#{driver}: #{total_rides}"
end
end
puts
puts
puts "TOTAL AMOUNT MADE PER DRIVER: "
each_driver_total.each do |info|
info.each do |driver, total_amount|
puts "#{driver}: $#{"%.02f" % total_amount}"
end
end
puts
puts
puts "AVERAGE RATING PER DRIVER: "
each_driver_rating.each do |info|
info.each do |driver, rating|
puts "#{driver}: #{"%.01f" % rating}"
end
end
puts
puts
highest_earned_per_day = day_highest_earned(ride_share_info)
puts "DAY THAT YIELDED HIGHEST EARNED AMOUNT PER DRIVER:"
highest_earned_per_day.each do |info|
info.each do |driver, date|
puts "#{driver}: #{date}"
end
end
puts
puts
raised_most_driver = driver_most_amount(each_driver_total)
puts "DRIVER THAT MADE THE MOST MONEY: #{raised_most_driver}"
puts
highest_rated_driver = driver_highest_rating(each_driver_rating)
puts "DRIVER RATED THE HIGHEST: #{highest_rated_driver}"
puts
puts