forked from Ada-C12/ride-share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ride_share.rb
138 lines (126 loc) · 2.48 KB
/
ride_share.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
drivers = [
{
id: "DR0001",
trips:
[
{
date: "3rd Feb 2016",
cost: 10,
rider_id: "RD0003",
rating: 3
},
{
date: "3rd Feb 2016",
cost: 30,
rider_id: "RD0015",
rating: 3
},
{
date: "5th Feb 2016",
cost: 45,
rider_id: "RD0003",
rating: 2
}
]
},
{
id: "DR0002",
trips:
[
{
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
}
]
},
{
id: "DR0003",
trips:
[
{
date: "4th Feb 2016",
cost: 5,
rider_id: "RD0003",
rating: 5
},
{
date: "5th Feb 2016",
cost: 50,
rider_id: "RD0003",
rating: 2
}
]
},
{
id: "DR0004",
trips:
[
{
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
}
]
}
]
puts "Number of rides each driver has given:"
drivers.each do |driver|
puts "#{driver[:id]}: #{driver[:trips].length} trips"
end
puts "Total money each driver has made:"
drivers.each do |driver|
total_cost = driver[:trips].map do |trip|
trip[:cost]
end
driver[:total_cost] = total_cost.sum
puts "#{driver[:id]}: $#{driver[:total_cost]}"
end
puts "Average rating for each driver:"
drivers.each do |driver|
ratings = driver[:trips].map do |trip|
trip[:rating].to_f
end
driver[:avg_rating] = (ratings.sum / ratings.length).round(1)
puts "#{driver[:id]}: #{driver[:avg_rating]}"
end
richest_driver = drivers.max_by do |driver|
driver[:total_cost]
end
puts "The driver who made the most money is #{richest_driver[:id]}."
highest_rated_driver = drivers.max_by do |driver|
driver[:avg_rating]
end
puts "The driver with the highest average rating is #{highest_rated_driver[:id]}."
drivers.each do |driver|
trips_by_cost = driver[:trips].sort_by do |trip|
trip[:cost]
end
puts "Driver #{driver[:id]} made the most money on #{trips_by_cost.last[:date]}."
end