forked from Ada-C10/ride-share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ride_share_correct.rb
139 lines (124 loc) · 2.75 KB
/
ride_share_correct.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
require "awesome_print"
def max_value(hash)
hash.max_by{ |k, v| v }
end
ap drivers ={
dr0001: [
{
rider: :RD0003,
cost: 10,
date: "3rd Feb 2016",
rating: 3
},
{
rider: :RD00015,
cost: 30,
date: "3rd Feb 2016",
rating: 4
},
{
rider: :RD0003,
cost: 45,
date: "5th Feb 2016",
rating: 2
}
],
dr0002: [
{
rider: :RD0073,
cost: 25,
date: "3rd Feb 2016",
rating: 5
},
{
rider: :RD0013,
cost: 15,
date: "4th Feb 2016",
rating: 1
},
{
rider: :RD0066,
cost: 35,
date: "5th Feb 2016",
rating: 3
}
],
dr0003: [
{
rider: :RD0066,
cost: 5,
date: "4th Feb 2016",
rating: 5
},
{
rider: :RD0003,
cost: 50,
date: "5th Feb 2016",
rating: 2
}
],
dr0004: [
{
rider: :RD0022,
cost: 5,
date: "3rd Feb 2016",
rating: 5
},
{
rider: :RD0022,
cost: 10,
date: "4th Feb 2016",
rating: 4
},
{
rider: :RD0073,
cost: 20,
date: "5th Feb 2016",
rating: 5
}
]
}
total_money_made ={}
total_averages = {}
rider_dates = []
best_day = ""
drivers.keys.each.with_index do |driver, x|
money_made = 0
total_rating = 0
average_rating = 0
best_day_money = 0
rider_dates = []
merged_rider_dates = []
puts "\n\n#{driver.upcase} INFO:"
puts "Total Rides: #{drivers[driver].length}"
drivers[driver].length.times do |x|
money_made += drivers[driver][x][:cost]
total_rating += drivers[driver][x][:rating]
rider_dates << {date: drivers[driver][x][:date],
cost: (drivers[driver][x][:cost])}
end
# money_earned
total_money_made[driver] = money_made
puts "Money Made: $#{money_made}"
#average ratings
average_rating = total_rating.to_f / drivers[driver].length
total_averages[driver] = average_rating.round(2)
puts "Average Rating: #{average_rating.round(2)}"
# most profitable day
# https://stackoverflow.com/questions/24233493/merge-duplicates-in-array-of-hashes
# grouping by date (3rd Feb 2016, etc.)
merged_rider_dates = rider_dates.group_by { |ride| ride[:date] }.map do |k,v|
{ :date => k, :cost => v.map { |same_day_ride| same_day_ride[:cost] }.sum }
end
best_day = merged_rider_dates.max_by { |hash| hash[:cost] }[:date]
print "Most profitable day: #{best_day}"
end
money_winner = max_value(total_money_made)
# .first puts key
# .last puts value
puts "\n\n#{money_winner.first} made the most money with a total of \
$#{money_winner.last}"
#Driver with best average
average_winner = max_value(total_averages)
print "\n#{average_winner.first} had the best average \
with #{average_winner.last} stars"