forked from Ada-C14/ride-share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ride_share_noor.rb
192 lines (165 loc) · 5.25 KB
/
ride_share_noor.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
########################################################
# Step 1: Establish the layers
# # In this section of the file, as a series of comments,
# # create a list of the layers you identify.
# There are three main layers:
# Trips_data (hash)
# rides (arrays of hashes in which each single hash represents a single ride )
# Which layers are nested in each other?
# In Trips_data (hash), for each driver there is rides array which represents all rides for that driver.
# Inside rides (array of hashes) there are hashes with information of each ride.
# Which layers of data "have" within it a different layer?
# The trips_data (hash) has rides (array) inside it.
# Also rides (array) has multiple hashes inside it.
# Which layers are "next" to each other?
# the rides(array) layers of each driver(hashes), the details of each ride is inside a hash
########################################################
# 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
# trips_data (hashes)
# rides (arrays of hashes)
# each single ride has its own hash
########################################################
# 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"
trips_data = [{
driver_id: "DR0001",
rides: [
{
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
}]},{
driver_id: "DR0002",
rides: [
{
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
}]},{
driver_id: "DR0003",
rides: [
{
date: "4th Feb 2016",
cost: 5,
rider_id: "RD0066",
rating: 5
},{
date: "5th Feb 2016",
cost: 50,
rider_id: "RD0003",
rating: 2
}]},{
driver_id: "DR0004",
rides: [
{
date: "4th Feb 2016",
cost: 10,
rider_id: "RD0022",
rating: 4
},{
date: "5th Feb 2016",
cost: 20,
rider_id: "RD0073",
rating: 5
},{
date: "3rd Feb 2016",
cost: 5,
rider_id: "RD0022",
rating: 5
}]}]
def line
puts "-" * 55
end
def line2
puts "-" * 104
end
########################################################
# 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
line
puts "Number of rides each driver has given"
line
trips_data.map { |driver_trips|
puts "Driver with ID of #{driver_trips[:driver_id]} made #{driver_trips[:rides].length} rides"}
line
# - the total amount of money each driver has made
puts "Total amount of money each driver has made"
line
trips_data.map do |driver_trips|
sum = driver_trips[:rides].sum do |ride|
ride[:cost]
end
puts "Driver with ID of #{driver_trips[:driver_id]} earned $#{sum} "
end
line
# - the average rating for each driver
puts "Average rating for each driver"
line
trips_data.map do |driver_trips|
sum = driver_trips[:rides].sum { |ride|
ride[:rating]}
count = driver_trips[:rides].count { |ride|
ride[:rating]}
average = sum/count
puts "Driver with ID of #{driver_trips[:driver_id]} has an average ratings of #{average} "
end
# - Which driver made the most money?
line2
drivers_earning = []
trips_data.map do |driver_trips|
earning = driver_trips[:rides].sum do |ride|
ride[:cost]
end
drivers_earning << {
driver_id: driver_trips[:driver_id],
earnings: earning
}
end
drivers_earning.sort_by!{|driver| driver[:earnings]}
puts "The driver who earned the most is the driver with ID of #{drivers_earning[-1][:driver_id]} who earned $#{drivers_earning[-1][:earnings]} ."
# - Which driver has the highest average rating?
drivers_ratings = []
trips_data.map do |driver_trips|
sum = driver_trips[:rides].sum { |ride|
ride[:rating]}
count = driver_trips[:rides].count { |ride|
ride[:rating]}
average = sum/count
drivers_ratings << {
driver_id: driver_trips[:driver_id],
ratings: average
}
end
drivers_ratings.sort_by!{|driver| driver[:ratings]}
puts "The driver with the highest average rating is the driver with ID of #{drivers_ratings[-1][:driver_id]} with an average rating of #{drivers_ratings[-1][:ratings]} ."
line2