Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
Nice work, you hit all the learning goals here. Well done. The only minor thing I would suggest is to make the methods do the calculation and just return the answer. Then your main program can print the output. That way your methods are doing 1 thing. Currently they are doing calculation and output. Otherwise this is outstanding. Well done.
| # There are three layers: all data for all rides, data for drivers, and data for rides | ||
| # Things: driver id, rider id, cost, date, rating, rides | ||
| # Relationships: driver id, rides belong to a driver; rider id, cost, date, rating belong to a ride, etc. |
There was a problem hiding this comment.
The file is a 2D data structure rows and columns. The whole file is a list of ride data. Each row is information about each trip.
| # Within the outermost layer, the drivers hash, there are two additional layers. | ||
| # There is an array layer that contains information about the trips completed by each driver. | ||
| # There is also a hash layer that contains details about each of those trips (i.e. rider id, date, cost, rating). | ||
| # Similarly, the middle array layer can also be considered to have within it a different layer | ||
| # (i.e. the hash layer that contains the specific details for the trips). The outermost drivers hash layer is next to | ||
| # the middle trips array layer which is next to the innermost hash layer containing details of the trips. |
| # into this data structure, such as "DR0004" | ||
| # and "3rd Feb 2016" and "RD0022" | ||
|
|
||
| drivers = { |
| puts "====================== NUMBER OF RIDES ======================" | ||
| num_rides(drivers) | ||
|
|
||
| # The total amount of money each driver has made | ||
| def total_earnings(drivers) | ||
| drivers.map do |driver, rides| | ||
| total = rides.map { |ride| ride[:cost]} | ||
| puts "#{driver} earned a total of $#{'%.2f' % total.sum}" | ||
| end | ||
| end |
There was a problem hiding this comment.
👍 , Nice use of nested map and sum.
| puts "====================== TOTAL EARNINGS =======================" | ||
| total_earnings(drivers) | ||
|
|
||
| # The average rating for each driver | ||
| def average_rating(drivers) | ||
| drivers.map do |driver, rides| | ||
| average_rating = ((rides.sum { |ride| ride[:rating] }) / rides.length.to_f) | ||
| puts "#{driver}'s average rating is #{'%.2f' % average_rating}" | ||
| end | ||
| end |
There was a problem hiding this comment.
Nice use of map and sum.
| puts "====================== AVERAGE RATING ========================" | ||
| average_rating(drivers) | ||
|
|
||
| # Which driver made the most money? | ||
| def highest_earning_driver(drivers) | ||
| highest_earnings = 0 | ||
| highest_earner = "" | ||
| drivers.each do |driver, rides| | ||
| earnings = rides.map { |ride| ride[:cost] }.sum | ||
| if earnings > highest_earnings | ||
| highest_earnings = earnings | ||
| highest_earner = driver | ||
| end | ||
| end | ||
| puts "#{highest_earner} made the most money for a total of $#{'%.2f' % highest_earnings}" | ||
| end |
| highest_earning_driver(drivers) | ||
|
|
||
| # Which driver has the highest average rating? | ||
| def highest_average_rating(drivers) |
There was a problem hiding this comment.
Could you use max_by here somehow?
| def best_day(drivers) | ||
| drivers.each do |driver, rides| | ||
| highest_daily_earnings = 0 | ||
| best_earnings_day = "" | ||
| rides.each do |ride| | ||
| if ride[:cost] > highest_daily_earnings | ||
| best_earnings_day = ride[:date] | ||
| highest_daily_earnings = ride[:cost] | ||
| end | ||
| end | ||
| puts "#{driver}'s best day was #{best_earnings_day} when they earned $#{'%.2f' % highest_daily_earnings}" | ||
| end | ||
| end |
There was a problem hiding this comment.
Also a place you could use max_by
Assignment Submission: Ride Share
Congratulations! You're submitting your assignment. Please reflect on the assignment with these questions.
Reflection
.map? If so, when? If not, why, or when would be a good opportunity to use it?