forked from Ada-C14/ride-share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworksheet.rb
166 lines (124 loc) · 5.37 KB
/
worksheet.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
########################################################
# 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?
########################################################
# 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
########################################################
# 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"
########################################################
# 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?
ride_log = { DR0004: [["3rd Feb 2016",5,"RD0022",5],
["4th Feb 2016",10,"RD0022",4],
["5th Feb 2016",20,"RD0073",5],
["5th Feb 2016",45,"RD0003",2]],
DR0001: [["3rd Feb 2016",10,"RD0003",3],
["3rd Feb 2016",30,"RD0015",4],
["5th Feb 2016",45,"RD0003",2]],
DR0002: [["3rd Feb 2016",25,"RD0073",5],
["4th Feb 2016",15,"RD0013",1],
["5th Feb 2016",35,"RD0066",3]],
DR0003: [["4th Feb 2016",5,"RD0066",5],
["5th Feb 2016",50,"RD0003",2]]
}
ride_log = ride_log.sort.to_h #orders & assigns according to key
def sum(array)
array.inject(0){|sum,x| sum + x }
end
def to_dollars(num)
"$#{'%.2f' % num}"
end
def rides_each_driver(log)
big_array = ["RIDES GIVEN:\n\n"] #first item, bc I'm printing this first/as a header
log.each do |driver_id, nested_ride_info|
dr_id_w_tot_earned = []
dr_id_w_tot_earned << "Driver: #{driver_id}"
dr_id_w_tot_earned << "Total rides: #{nested_ride_info.length}\n\n"
big_array << dr_id_w_tot_earned
end
return big_array
end
def total_each_driver(log)
big_array = ["\nDRIVER EARNINGS:\n\n"]
log.each do |driver_id, nested_ride_info|
dr_id_w_tot_earned = []
dr_id_w_tot_earned << "Driver: #{driver_id}"
total_earned = sum(nested_ride_info.map {|column| column[1].to_f})
dr_id_w_tot_earned << "Total earned: #{to_dollars(total_earned)}\n\n" #https://stackoverflow.com/questions/11688466/select-all-elements-from-one-column-in-an-array-of-arrays-in-ruby
big_array << dr_id_w_tot_earned
end
return big_array
end
def avg_rating_each_driver(log) #average to be computed & outputted as a float
big_array = ["\n\nAVERAGE RATINGS:\n\n"]
log.each do |driver_id, nested_ride_info|
dr_id_w_tot_earned = []
dr_id_w_tot_earned << "Driver: #{driver_id}"
average = sum(nested_ride_info.map {|column| column[3].to_f}) / (nested_ride_info.length) #computes average: gets total of ratings, div by no. of rides
dr_id_w_tot_earned << "Average rating: #{average}\n\n"
big_array << dr_id_w_tot_earned
end
return big_array
end
def highest_earners(log)
big_array = []
log.each do |driver_id, nested_ride_info|
dr_id_w_tot_earned = []
dr_id_w_tot_earned << driver_id
dr_id_w_tot_earned << sum(nested_ride_info.map {|column| column[1]})
big_array << dr_id_w_tot_earned
end
max_earned = big_array.map(&:last).max
highest_earner_arr = big_array.each do |dr_id_w_tot_earned, index|
dr_id_w_tot_earned[index] == max_earned #picks up any ties :)
end #nested array that holds array w paired info for any max earners
outputs = ["\nHIGHEST EARNER(S):\n\n"]
highest_earner_arr.each_with_index do |high_earner, index|
outputs << "Driver #{high_earner[0]} => #{to_dollars(high_earner[1])}" #might have to move to_dollars() (not sure how the original values being foat instaed might change the equivalency on line )
end
return outputs
end
def highest_rated_drivers(log)
big_array = []
log.each do |driver_id, nested_ride_info|
dr_id_w_avg_rating = []
dr_id_w_avg_rating << driver_id
average = sum(nested_ride_info.map {|column| column[3].to_f}) / (nested_ride_info.length)
dr_id_w_avg_rating << average
big_array << dr_id_w_avg_rating
end
max_avg_rating = big_array.map(&:last).max
highest_rated_arr = big_array.select.with_index do |dr_id_w_avg_rating, index|
dr_id_w_avg_rating[1] == max_avg_rating
end
outputs = ["\nHIGHEST RATED DRIVER(S):\n\n"]
highest_rated_arr.each_with_index do |high_rated, index|
outputs << "Driver #{high_rated[0]} => Avg. #{high_rated[1]}\n\n"
end
return outputs
end
puts "\n\n---WELCOME TO THE RIDE SHARE LOGGER---\n\n"
puts rides_each_driver(ride_log)
puts total_each_driver(ride_log)
puts highest_earners(ride_log)
puts avg_rating_each_driver(ride_log)
puts highest_rated_drivers(ride_log)
#https://stackoverflow.com/questions/30528128/ruby-turn-a-nested-array-into-a-hash
#https://stackoverflow.com/questions/32521066/how-can-i-find-the-max-value-from-an-array-of-arrays