forked from Ada-C14/ride-share
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathride_share.rb
More file actions
171 lines (146 loc) · 4.23 KB
/
ride_share.rb
File metadata and controls
171 lines (146 loc) · 4.23 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
########################################################
# Step 1: Establish the layers
#
# The outermost layer is "ride_share".
# It has nested in it each individual driver in the ride share.
# The individual drivers are next to each other in this layer.
#
# Nested within the "ride_share" layer is the "drivers" layer.
# Each driver is next to every other driver.
#
# Nested within the "drivers" layer is the "ride_info" layer.
# All rides by one driver are next to the other rides by the same driver.
########################################################
# Step 2: Assign a data structure to each layer
#
# ride_share: hash
# drivers: key-value pairs where the value is an array
# ride_info: hashes
########################################################
# Step 3: Make the data structure!
ride_share = {
DR0001: [
{
date: "3rd Feb 2016",
cost: 10,
rider: "RD0003",
rating: 3
},
{
date: "3rd Feb 2016",
cost: 30,
rider: "RD0015",
rating: 4
},
{
date: "5th Feb 2016",
cost: 45,
rider: "RD0003",
rating: 2
}
],
DR0002: [
{
date: "3rd Feb 2016",
cost: 25,
rider: "RD0073",
rating: 5
},
{
date: "4th Feb 2016",
cost: 15,
rider: "RD0013",
rating: 1
},
{
date: "5th Feb 2016",
cost: 35,
rider: "RD0066",
rating: 3
}
],
DR0003: [
{
date: "4th Feb 2016",
cost: 5,
rider: "RD0066",
rating: 5
},
{
date: "5th Feb 2016",
cost: 50,
rider: "RD0003",
rating: 2
}
],
DR0004: [
{
date: "3rd Feb 2016",
cost: 5,
rider: "RD0022",
rating: 5
},
{
date: "4th Feb 2016",
cost: 10,
rider: "RD0022",
rating: 4
},
{
date: "5th Feb 2016",
cost: 20,
rider: "RD0073",
rating: 5
}
]
}
########################################################
# Step 4: Total Driver's Earnings and Number of Rides
# 1. number of rides each driver has given
ride_share.each do |driver, ride_array|
total_rides = ride_array.length
puts "Driver #{driver} has given #{total_rides} rides."
end
# 2. total amount of money each driver has made
def total_earnings(array)
driver_earnings = array.sum { |ride| ride[:cost] }
return driver_earnings
end
earnings_array = ride_share.map do |driver, ride_array|
{ id: driver, earnings: total_earnings(ride_array) }
end
earnings_array.each do |driver|
puts "Driver #{driver[:id]} made $#{driver[:earnings]}"
end
# 3. average rating for each driver
def average_rating(ride_array)
ratings_total = ride_array.sum { |ride| ride[:rating] }
ride_quantity = ride_array.length.to_f
average_rating = (ratings_total/ride_quantity).truncate(1)
return average_rating
end
rating_array = ride_share.map do |driver, ride_array|
{ id: driver, rating: average_rating(ride_array) }
end
rating_array.each do |driver|
puts "Driver #{driver[:id]} has an average rating of #{driver[:rating]}"
end
# 4. which driver has made the most money
most_paid_driver = earnings_array.max_by { |driver| driver[:earnings] }
puts "#{most_paid_driver[:id]} has made the most money: $#{most_paid_driver[:earnings]}"
# 5. which driver has the highest average rating
highest_rated_driver = rating_array.max_by { |driver| driver[:rating] }
puts "#{highest_rated_driver[:id]} has the highest rating: #{highest_rated_driver[:rating]}"
# 6. for each driver, on which day did they make the most money
ride_share.each do |driver, ride_array|
driver_hash = {}
ride_array.each do |ride|
if driver_hash.include?(ride[:date])
driver_hash[ride[:date]] += ride[:cost]
else
driver_hash[ride[:date]] = ride[:cost]
end
end
lucrative_date = driver_hash.max_by { |date, value| value }
puts "#{driver} made the most money on #{lucrative_date[0]}: $#{lucrative_date[1]}"
end