From 376a40481192f9fb80c9b99fe2185af8841b894a Mon Sep 17 00:00:00 2001 From: Mina Date: Mon, 18 Feb 2019 19:28:38 -0800 Subject: [PATCH 1/3] Rename worksheet.rb to ride-share.rb --- ride-share.rb | 191 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 ride-share.rb diff --git a/ride-share.rb b/ride-share.rb new file mode 100644 index 0000000..6accf5f --- /dev/null +++ b/ride-share.rb @@ -0,0 +1,191 @@ +# DRIVER_ID,DATE,COST,RIDER_ID,RATING +# DR0004,3rd Feb 2016,5,RD0022,5 +# DR0001,3rd Feb 2016,10,RD0003,3 +# DR0002,3rd Feb 2016,25,RD0073,5 +# DR0001,3rd Feb 2016,30,RD0015,4 +# DR0003,4th Feb 2016,5,RD0066,5 +# DR0004,4th Feb 2016,10,RD0022,4 +# DR0002,4th Feb 2016,15,RD0013,1 +# DR0003,5th Feb 2016,50,RD0003,2 +# DR0002,5th Feb 2016,35,RD0066,3 +# DR0004,5th Feb 2016,20,RD0073,5 +# DR0001,5th Feb 2016,45,RD0003,2 + +drivers_log = { + DR0001: [ + { + rating_of_driver: 3, + rider: "RD0003", + cost_of_ride: 10, + date_of_ride: "3rd Feb 2016" + }, + { + rating_of_driver: 2, + rider: "RD0003", + cost_of_ride: 45, + date_of_ride: "5th Feb 2016" + }, + { + rating_of_driver: 4, + rider: "RD0015", + cost_of_ride: 30, + date_of_ride: "3rd Feb 2016" + } + ], + DR0002: [ + { + rating_of_driver: 5, + rider: "RD0073", + cost_of_ride: 25, + date_of_ride: "3rd Feb 2016" + }, + { + rating_of_driver: 1, + rider: "RD0013", + cost_of_ride: 15, + date_of_ride: "4th Feb 2016" + }, + { + rating_of_driver: 3, + rider: "RD0066", + cost_of_ride: 35, + date_of_ride: "5th Feb 2016" + } + ], + DR0003: [ + { + rating_of_driver: 5, + rider: "RD0066", + cost_of_ride: 5, + date_of_ride: "4th Feb 2016" + }, + { + rating_of_driver: 2, + rider: "RD0003", + cost_of_ride: 50, + date_of_ride: "5th Feb 2016" + } + ], + DR0004: [ + { + rating_of_driver: 5, + rider: "RD0022", + cost_of_ride: 5, + date_of_ride: "3rd Feb 2016" + }, + { + rating_of_driver: 4, + rider: "RD0022", + cost_of_ride: 10, + date_of_ride: "4th Feb 2016" + }, + { + rating_of_driver: 5, + rider: "RD0073", + cost_of_ride: 20, + date_of_ride: "5th Feb 2016" + } + ] +} + +# The number of rides each driver has given +def number_of_rides(drives) + drives.each { |driver_id, length_of_hash| + puts "Driver: #{ driver_id } has given #{ length_of_hash.length } rides." + } +end + +number_of_rides(drivers_log) + +# the total amount of money each driver has made +def driver_total(trips) + total_amount = trips.map { |drivers_id, cost| + [drivers_id, cost.sum { + |cost| cost[:cost_of_ride] + } + ] + } + index = 0 + trips.length.times { + puts "Driver: #{ total_amount[index][0] } earned a total of $#{ total_amount[index][1] }." + index += 1 + } +end + +driver_total(drivers_log) + +# The average rating for each driver +def driver_rating(ratings) + average_rating = ratings.map { |drivers_id, rating| + [drivers_id, rating.sum { + |rating| rating[:rating_of_driver] + } / rating.length.to_f + ] + } + + index = 0 + ratings.length.times { + puts "Driver: #{ average_rating[index][0] } has an average rating of %.1f. " % [ average_rating[index][1] ] + index += 1 + } +end + +driver_rating(drivers_log) + +# Which driver made the most money? +def largest_sum(sum_money) + most_money = sum_money.map { |drivers_id, money| [ + drivers_id, money.sum { + |money| money[:cost_of_ride] + } + ] + } + index = 0 + highest_earner = 0 + driver = 0 + most_money.length.times { + if most_money[index][1] > highest_earner + highest_earner = most_money[index][1] + driver = most_money[index][0] + index += 1 + elsif most_money[index][1] < highest_earner + highest_earner = most_money[index][1] + index += 1 + else + highest_earner = most_money[index][1] + index += 1 + end + } + puts "\nDriver ID - #{ driver } earned the most money." +end + +largest_sum(drivers_log) + +# Which driver has the highest average rating? +def highest_driver_rating(driver_ratings) + highest_rating = driver_ratings.map { |drivers_id, ratings| + [drivers_id, ratings.sum { + |ratings| ratings[:rating_of_driver] + } / ratings.length.to_f + ] + } + index = 0 + number_one = 0 + driver = 0 + highest_rating.length.times { + if highest_rating[index][1] > number_one + number_one = highest_rating[index][1] + driver = highest_rating[index][0] + index += 1 + elsif highest_rating[index][1] < number_one + number_one = highest_rating[index][1] + index += 1 + else + number_one = highest_rating[index][1] + index += 1 + end + } + puts "\nDriver ID: #{ driver } has the highest rating." +end + +highest_driver_rating(drivers_log) From 29474285963d1527393ba9bc68ba17db2ab0f79d Mon Sep 17 00:00:00 2001 From: Mina Date: Mon, 18 Feb 2019 19:57:46 -0800 Subject: [PATCH 2/3] Update ride-share.rb --- ride-share.rb | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/ride-share.rb b/ride-share.rb index 6accf5f..baa43b5 100644 --- a/ride-share.rb +++ b/ride-share.rb @@ -1,16 +1,3 @@ -# DRIVER_ID,DATE,COST,RIDER_ID,RATING -# DR0004,3rd Feb 2016,5,RD0022,5 -# DR0001,3rd Feb 2016,10,RD0003,3 -# DR0002,3rd Feb 2016,25,RD0073,5 -# DR0001,3rd Feb 2016,30,RD0015,4 -# DR0003,4th Feb 2016,5,RD0066,5 -# DR0004,4th Feb 2016,10,RD0022,4 -# DR0002,4th Feb 2016,15,RD0013,1 -# DR0003,5th Feb 2016,50,RD0003,2 -# DR0002,5th Feb 2016,35,RD0066,3 -# DR0004,5th Feb 2016,20,RD0073,5 -# DR0001,5th Feb 2016,45,RD0003,2 - drivers_log = { DR0001: [ { From df2667ffe508914f47bdcbb8e013aa677c915f5d Mon Sep 17 00:00:00 2001 From: Mina Date: Tue, 19 Feb 2019 20:45:20 -0800 Subject: [PATCH 3/3] Update ride-share.rb --- ride-share.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/ride-share.rb b/ride-share.rb index baa43b5..19d9dee 100644 --- a/ride-share.rb +++ b/ride-share.rb @@ -138,9 +138,6 @@ def largest_sum(sum_money) elsif most_money[index][1] < highest_earner highest_earner = most_money[index][1] index += 1 - else - highest_earner = most_money[index][1] - index += 1 end } puts "\nDriver ID - #{ driver } earned the most money." @@ -167,9 +164,6 @@ def highest_driver_rating(driver_ratings) elsif highest_rating[index][1] < number_one number_one = highest_rating[index][1] index += 1 - else - number_one = highest_rating[index][1] - index += 1 end } puts "\nDriver ID: #{ driver } has the highest rating."