forked from Ada-C14/ride-share
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworksheet.rb
More file actions
335 lines (301 loc) · 8.05 KB
/
worksheet.rb
File metadata and controls
335 lines (301 loc) · 8.05 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
########################################################
# Step 1: Establish the layers
# In this section of the file, as a series of comments,
# create a list of the layers you identify.
# Which layers are nested in each other?
# Which layers of data "have" within it a different layer?
# Which layers are "next" to each other?
#~~~~NOUNS/OBJECTS~~~~~
# Driver,
# Date,
# Cost,
# Rider,
# Rating,
# Ride
# Rides
#~~~RELATIONSHIPS TO EACH OTHER~~~~~~
# A ride has a driver and a rider and a cost and a date
# A driver has an id and a rating
# A rider has an id
########################################################
# 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
# Driver - Sits within a single ride hash. Nested within: Hash with Driver Id & Rating
# Date - Sits within a single ride hash
# Cost - Sits within a single ride hash
# Rider - Sits within a single ride hash. Nested within: Hash with Rider Id (for scalability)
# Rating - Nested in Driver hash
# Rides = Outermost Layer - An array that holds several 'ride' hashes
#Looks like
# Rides [
# ride
# {
# Date
# Driver {
# id
# rating
# }
# Rider{
# id
# }
# Cost {
# }
# }
# ]
########################################################
# 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"
rides = [
{
date: "3rd Feb 2016",
driver:
{
id:"DR0001",
rating: 3
},
rider:
{
id:"RD0003"
},
cost: 10
},
{
date: "3rd Feb 2016",
driver:
{
id: "DR0001",
rating: 4
},
rider:
{
id:"RD0015"
},
cost: 30
},
{
date: "5th Feb 2016",
driver:
{
id: "DR0001",
rating: 2
},
rider:
{
id:"RD0003"
},
cost: 45
},
{
date: "3rd Feb 2016",
driver:
{
id: "DR0002",
rating: 5
},
rider:
{
id:"RD0073"
},
cost: 25
},
{
date: "4th Feb 2016",
driver:
{
id: "DR0002",
rating: 1
},
rider:
{
id:"RD0013"
},
cost: 15
},
{
date: "5th Feb 2016",
driver:
{
id: "DR0002",
rating: 3
},
rider:
{
id:"RD0066"
},
cost: 35
},
{
date: "4th Feb 2016",
driver:
{
id: "DR0003",
rating: 5
},
rider:
{
id:"RD0066"
},
cost: 5
},
{
date: "5th Feb 2016",
driver:
{
id: "DR0003",
rating: 2
},
rider:
{
id:"RD0003"
},
cost: 50
},
{
date: "3rd Feb 2016",
driver:
{
id: "DR0004",
rating: 5
},
rider:
{
id:"RD0022"
},
cost: 5
},
{
date: "4th Feb 2016",
driver:
{
id: "DR0004",
rating: 4
},
rider:
{
id:"RD0022"
},
cost: 10
},
{
date: "5th Feb 2016",
driver:
{
id: "DR0004",
rating: 5
},
rider:
{
id:"RD0073"
},
cost: 20
}
]
########################################################
# 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?
#this method returns a total number of rides by a given driver from a data set 'rides'
def get_ride_count_by_driver(driver_id, rides)
count = 0
rides.each do |ride|
if ride[:driver][:id] == driver_id
count += 1
end
end
return count
end
#this method returns the total money made by a given driver from the data set 'rides'
def get_total_money_made_by_driver (driver_id, rides)
total_money = 0
rides.each do |ride|
if ride[:driver][:id] == driver_id
total_money += ride[:cost]
end
end
return total_money
end
#this method returns the average rating made by a given driver from the data set 'rides'
def get_average_rating_by_driver (driver_id, rides)
rating_sum = 0.0
rides.each do |ride|
if ride[:driver][:id] == driver_id
rating_sum += ride[:driver][:rating]
end
end
return rating_sum / get_ride_count_by_driver(driver_id, rides)
end
#this method returns the array of the highest_paid driver_ids from the data set 'rides'
def get_drivers_with_most_money (driver_ids, rides)
highest_paid = {
drivers: [],
amount: 0.00
}
driver_ids.each do |driver_id|
current_driver_money = get_total_money_made_by_driver(driver_id, rides)
if current_driver_money > highest_paid[:amount]
highest_paid[:drivers].clear #empties amount from highest paid hash if new high high value found
highest_paid[:amount] = current_driver_money
highest_paid[:drivers] << driver_id
elsif current_driver_money == highest_paid[:amount]
highest_paid[:drivers] << driver_id
end
end
return highest_paid
end
#This method returns the array of the highest_rated driver_ids from the data set 'rides'
def get_drivers_with_highest_average_rating (driver_ids, rides)
highest_rated = {
drivers: [],
rating: 0.0
}
driver_ids.each do |driver_id|
current_driver_average_rating = get_average_rating_by_driver(driver_id, rides)
if current_driver_average_rating > highest_rated[:rating]
highest_rated[:drivers].clear #empties rating from highest paid hash if new high high rating found
highest_rated[:rating] = current_driver_average_rating
highest_rated[:drivers] << driver_id
elsif current_driver_average_rating == highest_rated[:rating]
highest_rated[:drivers] << driver_id
end
end
return highest_rated
end
#PROGRAM STARTS
#Local variable array to collect unique driver_ids
driver_ids = rides.map {|ride| ride[:driver][:id]}.uniq
#Output Message
puts "*" * 65
puts "\n\tRide Share Tally\n\n".upcase
#Beginning of Loop 1
driver_ids.each do |driver_id|
#The number of rides each driver has given
puts "\tDriver #{driver_id}:"
puts "\t\t* gave #{get_ride_count_by_driver(driver_id, rides)} rides"
#The total amount of money each driver has made
puts "\t\t* earned $#{get_total_money_made_by_driver(driver_id, rides)} dollars"
#The average rating for each driver
puts "\t\t* has an average rating of #{get_average_rating_by_driver(driver_id,rides).round(1)} stars\n\n"
end
#Loops 2 and 3 to find highest paid and highest rated (methods account for collisions)
highest_paid = get_drivers_with_most_money(driver_ids, rides)
puts "\tThe highest paid driver(s):".upcase
highest_paid[:drivers].each do |driver|
puts "\t\t* #{driver} with $#{highest_paid[:amount]} dollars.\n\n"
end
highest_rated = get_drivers_with_highest_average_rating(driver_ids, rides)
puts "\tdriver(s) with a highest average rating:".upcase
highest_rated[:drivers].each do |driver|
puts "\t\t* #{driver} - #{highest_rated[:rating].round(1)} stars!\n\n"
end
puts "*" * 65
exit