From 88b60fd3ff7bfc9ac242b1dfe0eef7594e099024 Mon Sep 17 00:00:00 2001 From: anyatokar <61166946+anyatokar@users.noreply.github.com> Date: Sun, 13 Sep 2020 21:50:28 -0700 Subject: [PATCH 1/3] Update worksheet.rb --- worksheet.rb | 180 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 171 insertions(+), 9 deletions(-) diff --git a/worksheet.rb b/worksheet.rb index 95b085d..3e86542 100644 --- a/worksheet.rb +++ b/worksheet.rb @@ -2,17 +2,23 @@ # Step 1: Establish the layers # In this section of the file, as a series of comments, -# create a list of the layers you identify. +# create a list of the layers you identify: + #driver_id, rides, date, cost, rider_id, rating # Which layers are nested in each other? + # driver id < rides < date - cost - rider_id - rating # Which layers of data "have" within it a different layer? + # each ride has ride-specific info within it # Which layers are "next" to each other? +# driver_ids and date - cost - rider_id - rating ######################################################## # 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 - +# determine what data structure each layer should have: +# drivers_hash = hash +# each driver_id(key) within it contains array(value) of +# hashes which contain ride-specific info ######################################################## # Step 3: Make the data structure! @@ -22,13 +28,169 @@ # You should be copying and pasting the literal data # into this data structure, such as "DR0004" # and "3rd Feb 2016" and "RD0022" +# +drivers_hash = { + DR0001: [ + { + day: 3, + month: 2, + year: 2016, + cost: 10, + rider_id: "RD0003", + rating: 3 + }, + { + day: 3, + month: 2, + year: 2016, + cost: 30, + rider_id: "RD0015", + rating: 4 + }, + { + day: 5, + month: 2, + year: 2016, + cost: 45, + rider_id: "RD0003", + rating: 2 + } + ], + DR0002: [ + { + day: 3, + month: 2, + year: 2016, + cost: 25, + rider_id: "RD0073", + rating: 5 + }, + { + day: 4, + month: 2, + year: 2016, + cost: 15, + rider_id: "RD0013", + rating: 1 + }, + { + day: 5, + month: 2, + year: 2016, + cost: 35, + rider_id: "RD0066", + rating: 3 + } + ], + DR0003: [ + { + day: 4, + month: 2, + year: 2016, + cost: 5, + rider_id: "RD0066", + rating: 5 + }, + { + day: 5, + month: 2, + year: 2016, + cost: 50, + rider_id: "RD0003", + rating: 2 + } + ], + DR0004: [ + { + day: 3, + month: 2, + year: 2016, + cost: 5, + rider_id: "RD0022", + rating: 5 + }, + { + day: 4, + month: 2, + year: 2016, + cost: 10, + rider_id: "RD0022", + rating: 4 + }, + { + day: 5, + month: 2, + year: 2016, + cost: 20, + rider_id: "RD0073", + rating: 5 + } + ] +} ######################################################## # Step 4: Total Driver's Earnings and Number of Rides +def line + puts "########################################################" +end + +# the number of rides each driver has given ----------------------------------- +def num_of_rides_method(driver_id, drivers_hash) + return (drivers_hash[driver_id]).length +end +line + +# the total amount of money each driver has made ----------------------------------- +def income_method(rides_array) + return rides_array.reduce(0) { |sum, ride_hash| sum + ride_hash[:cost] } +end + +# the average rating for each driver ----------------------------------- + +def average_rating_method(rides_array, num_of_rides) + sum_ratings = rides_array.reduce(0) { |sum, ride_hash| + sum + ride_hash[:rating] } + return (sum_ratings / num_of_rides.to_f).round(1) # why can't this be included in the reduce block? +end + +# Which driver made the most money? ----------------------------------- +# this returns the first value, does not handle ties. +def highest_income_method(tallies_array_of_hashes) + return tallies_array_of_hashes.max_by { |tally_hash| tally_hash[:income] }[:driver_id] +end + +# Which driver has the highest average rating? ----------------------------------- +def highest_average_rating_method(tallies_array_of_hashes) + return tallies_array_of_hashes.max_by { |tally_hash| tally_hash[:average_rating] }[:driver_id] +end + +# runs all the methods ----------------------------------- +def summary(drivers_hash) + tallies_array_of_hashes = Array.new + drivers_hash.each do |driver_id, rides_array| + + num_of_rides = num_of_rides_method(driver_id, drivers_hash) + income = income_method(rides_array) + average_rating = average_rating_method(rides_array, num_of_rides) + + tallies_hash = { + driver_id: driver_id, + income: income, + average_rating: average_rating + } + tallies_array_of_hashes << tallies_hash + + puts "Driver with id #{driver_id} gave #{num_of_rides} ride(s) and \ +made $#{income}. +On average, their rating was #{average_rating} stars." + puts "------" + end + + highest_income = highest_income_method(tallies_array_of_hashes) + highest_average_rating = highest_average_rating_method(tallies_array_of_hashes) -# 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? \ No newline at end of file + puts "Driver with id #{highest_income} made the most money." + puts "Driver with id #{highest_average_rating} \ +had the highest rating, on average." +end +summary(drivers_hash) From 0872f49465d4f00e77238fdd5c994ef3776fa8c0 Mon Sep 17 00:00:00 2001 From: anyatokar <61166946+anyatokar@users.noreply.github.com> Date: Mon, 14 Sep 2020 12:26:42 -0700 Subject: [PATCH 2/3] Update worksheet.rb i added the optional --- worksheet.rb | 69 +++++++++++++++++++++------------------------------- 1 file changed, 28 insertions(+), 41 deletions(-) diff --git a/worksheet.rb b/worksheet.rb index 3e86542..ebce96b 100644 --- a/worksheet.rb +++ b/worksheet.rb @@ -17,7 +17,7 @@ # Copy your list from above, and in this section # determine what data structure each layer should have: # drivers_hash = hash -# each driver_id(key) within it contains array(value) of +# each driver_id(key) within it contains array(value) of # hashes which contain ride-specific info ######################################################## # Step 3: Make the data structure! @@ -32,25 +32,19 @@ drivers_hash = { DR0001: [ { - day: 3, - month: 2, - year: 2016, + date: "3rd Feb 2016", cost: 10, rider_id: "RD0003", rating: 3 }, { - day: 3, - month: 2, - year: 2016, + date: "3rd Feb 2016", cost: 30, rider_id: "RD0015", rating: 4 }, { - day: 5, - month: 2, - year: 2016, + date: "5th Feb 2016", cost: 45, rider_id: "RD0003", rating: 2 @@ -58,25 +52,19 @@ ], DR0002: [ { - day: 3, - month: 2, - year: 2016, + date: "3rd Feb 2016", cost: 25, rider_id: "RD0073", rating: 5 }, { - day: 4, - month: 2, - year: 2016, + date: "4th Feb 2016", cost: 15, rider_id: "RD0013", rating: 1 }, { - day: 5, - month: 2, - year: 2016, + date: "5th Feb 2016", cost: 35, rider_id: "RD0066", rating: 3 @@ -84,17 +72,13 @@ ], DR0003: [ { - day: 4, - month: 2, - year: 2016, + date: "4th Feb 2016", cost: 5, rider_id: "RD0066", rating: 5 }, { - day: 5, - month: 2, - year: 2016, + date: "5th Feb 2016", cost: 50, rider_id: "RD0003", rating: 2 @@ -102,25 +86,19 @@ ], DR0004: [ { - day: 3, - month: 2, - year: 2016, + date: "3rd Feb 2016", cost: 5, rider_id: "RD0022", rating: 5 }, { - day: 4, - month: 2, - year: 2016, + date: "4th Feb 2016", cost: 10, rider_id: "RD0022", rating: 4 }, { - day: 5, - month: 2, - year: 2016, + date: "5th Feb 2016", cost: 20, rider_id: "RD0073", rating: 5 @@ -134,11 +112,11 @@ def line puts "########################################################" end -# the number of rides each driver has given ----------------------------------- -def num_of_rides_method(driver_id, drivers_hash) - return (drivers_hash[driver_id]).length -end -line +# # the number of rides each driver has given ----------------------------------- +# def num_of_rides_method(driver_id, drivers_hash) +# return (drivers_hash[driver_id]).length +# end +# line # the total amount of money each driver has made ----------------------------------- def income_method(rides_array) @@ -160,20 +138,28 @@ def highest_income_method(tallies_array_of_hashes) end # Which driver has the highest average rating? ----------------------------------- +# this returns the first value, does not handle ties. def highest_average_rating_method(tallies_array_of_hashes) return tallies_array_of_hashes.max_by { |tally_hash| tally_hash[:average_rating] }[:driver_id] end +# For each driver, on which day did they make the most money? ----------------------------------- +def date_highest_income_method(rides_array) + rides_array.max_by { |ride_hash| ride_hash[:cost] }[:date] +end + # runs all the methods ----------------------------------- def summary(drivers_hash) tallies_array_of_hashes = Array.new drivers_hash.each do |driver_id, rides_array| - num_of_rides = num_of_rides_method(driver_id, drivers_hash) + num_of_rides = (drivers_hash[driver_id]).length income = income_method(rides_array) average_rating = average_rating_method(rides_array, num_of_rides) + date_highest_income = date_highest_income_method(rides_array) tallies_hash = { + date_highest_income: date_highest_income, driver_id: driver_id, income: income, average_rating: average_rating @@ -182,7 +168,8 @@ def summary(drivers_hash) puts "Driver with id #{driver_id} gave #{num_of_rides} ride(s) and \ made $#{income}. -On average, their rating was #{average_rating} stars." +On average, their rating was #{average_rating} stars. +They made the most money on #{date_highest_income}." puts "------" end From 7b9301a7854b88fe19ed71a9cb5db091be432d80 Mon Sep 17 00:00:00 2001 From: anyatokar <61166946+anyatokar@users.noreply.github.com> Date: Mon, 14 Sep 2020 12:29:29 -0700 Subject: [PATCH 3/3] Update worksheet.rb removed unnecessary comment --- worksheet.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/worksheet.rb b/worksheet.rb index ebce96b..0ce9fba 100644 --- a/worksheet.rb +++ b/worksheet.rb @@ -112,12 +112,6 @@ def line puts "########################################################" end -# # the number of rides each driver has given ----------------------------------- -# def num_of_rides_method(driver_id, drivers_hash) -# return (drivers_hash[driver_id]).length -# end -# line - # the total amount of money each driver has made ----------------------------------- def income_method(rides_array) return rides_array.reduce(0) { |sum, ride_hash| sum + ride_hash[:cost] } @@ -153,7 +147,7 @@ def summary(drivers_hash) tallies_array_of_hashes = Array.new drivers_hash.each do |driver_id, rides_array| - num_of_rides = (drivers_hash[driver_id]).length + num_of_rides = (drivers_hash[driver_id]).length # the number of rides each driver has given ----------------------------------- income = income_method(rides_array) average_rating = average_rating_method(rides_array, num_of_rides) date_highest_income = date_highest_income_method(rides_array)