From 456f3150fc243814694d4a2d27d0c633458ef03f Mon Sep 17 00:00:00 2001 From: jaitch <37591322+jaitch@users.noreply.github.com> Date: Fri, 9 Aug 2019 21:14:31 -0700 Subject: [PATCH 01/11] Copy code from VS Code --- worksheet.rb | 152 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 121 insertions(+), 31 deletions(-) diff --git a/worksheet.rb b/worksheet.rb index 95b085d..935b734 100644 --- a/worksheet.rb +++ b/worksheet.rb @@ -1,34 +1,124 @@ -######################################################## -# Step 1: Establish the layers +ride_share = { + DR0001: [ + { + date: "3rd Feb 2016", + rider: "RD0003", + cost: 10, + rating: 3 + }, + { + date: "3rd Feb 2016", + rider: "RD0015", + cost: 30, + rating: 4 + }, + { + date: "5th Feb 2016", + rider: "RD0003", + cost: 45, + rating: 2 + } + ], + DR0002: [ + { + date: "3rd Feb 2016", + rider: "RD0073", + cost: 25, + rating: 5 + }, + { + date: "4th Feb 2016", + rider: "RD0013", + cost: 15, + rating: 1 + }, + { + date: "5th Feb 2016", + rider: "RD0066", + cost: 35, + rating: 3 + } + ], + DR0003: [ + { + date: "4th Feb 2016", + rider: "RD0066", + cost: 5, + rating: 5 + }, + { + date: "5th Feb 2016", + rider: "RD0003", + cost: 50, + rating: 2 + } + ], + DR0004: [ + { + date: "3rd Feb 2016", + rider: "RD0022", + cost: 5, + rating: 5 + }, + { + date: "4th Feb 2016", + rider: "RD0022", + cost: 10, + rating: 4 + }, + { + date: "5th Feb 2016", + rider: "RD0073", + cost: 20, + rating: 5 + } + ] +} -# 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? +count = 1 +most_moolah = 0 +richest_driver = nil +highest_average = 0 +best_driver = nil -######################################################## -# Step 2: Assign a data structure to each layer +ride_share.each do |key, value| + # Calculate total money made and most lucrative day + total_moolah = 0 + most_lucrative_date = nil + most_made_in_a_day = 0 + value.each do |hash| + total_moolah += hash[:cost] + if hash[:cost] > most_made_in_a_day + most_made_in_a_day = hash[:cost] + most_lucrative_date = hash[:date] + end + end + + # Calculate average rating + total_points = 0 + value.each do |hash| + total_points += hash[:rating] + end + average = total_points / value.length.to_f + + # Output info for each driver + puts "Driver #{count} has given #{value.length} rides and earned an average rating of #{average}. + They made $#{total_moolah} in total. Their most lucrative day was #{most_lucrative_date} on which they made $#{most_made_in_a_day}." + + # Determine which driver made the most money + if total_moolah > most_moolah + most_moolah = total_moolah + richest_driver = count + end + + # Determine which driver had the highest average rating + if average > highest_average + highest_average = average + best_driver = count + end + count += 1 +end -# 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? \ No newline at end of file +# Output overall stats info +puts "The driver who made the most money is Driver #{richest_driver}!" +puts "The driver with the highest average rating is Driver #{best_driver}!" From 532288abfdeb827fede9e29dbe66a6b7de26096c Mon Sep 17 00:00:00 2001 From: jaitch <37591322+jaitch@users.noreply.github.com> Date: Sun, 11 Aug 2019 09:27:31 -0700 Subject: [PATCH 02/11] Update worksheet.rb --- worksheet.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/worksheet.rb b/worksheet.rb index 935b734..48fb7c9 100644 --- a/worksheet.rb +++ b/worksheet.rb @@ -1,3 +1,15 @@ +# Comprehension Questions: +# create a list of the layers you identify. +# -- drivers; rides: riders, date, cost, rating +# Which layers are nested in each other? +# -- riders, date, cost and rating are nested within rides; rides are nested within drivers +# -- the above nesting is maximized for comparing drivers, but in other cases one could organize by rider or date +# Which layers of data "have" within it a different layer? +# -- drivers and rides +# Which layers are "next" to each other? +# -- cost and rating both apply to specific rides, so are next to each other +# -- for the purposes of this exercise, date and rider are also next to cost and rating + ride_share = { DR0001: [ { From b29c4411a713f09323e7493ad1b0a2c4401c4606 Mon Sep 17 00:00:00 2001 From: Janice Huang Date: Sun, 11 Aug 2019 11:34:28 -0700 Subject: [PATCH 03/11] Answer comprehension questions more thoroughly --- worksheet.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/worksheet.rb b/worksheet.rb index 48fb7c9..42a4f0a 100644 --- a/worksheet.rb +++ b/worksheet.rb @@ -1,14 +1,14 @@ # Comprehension Questions: -# create a list of the layers you identify. +# Create a list of the layers you identify. # -- drivers; rides: riders, date, cost, rating # Which layers are nested in each other? # -- riders, date, cost and rating are nested within rides; rides are nested within drivers # -- the above nesting is maximized for comparing drivers, but in other cases one could organize by rider or date # Which layers of data "have" within it a different layer? -# -- drivers and rides +# -- drivers (and rides); if organized differently, without drivers as the topmost layer, riders and date could have layers within them # Which layers are "next" to each other? # -- cost and rating both apply to specific rides, so are next to each other -# -- for the purposes of this exercise, date and rider are also next to cost and rating +# -- for the purposes of this exercise, date and rider are also next to cost and rating because I'm not prioritizing finding data among dates and riders ride_share = { DR0001: [ From fa7d7b53911e859649d529003e533bcdef72843f Mon Sep 17 00:00:00 2001 From: Janice Huang Date: Sun, 11 Aug 2019 11:37:11 -0700 Subject: [PATCH 04/11] Delete comprehension questions (realized there's another set) --- worksheet.rb | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/worksheet.rb b/worksheet.rb index 42a4f0a..935b734 100644 --- a/worksheet.rb +++ b/worksheet.rb @@ -1,15 +1,3 @@ -# Comprehension Questions: -# Create a list of the layers you identify. -# -- drivers; rides: riders, date, cost, rating -# Which layers are nested in each other? -# -- riders, date, cost and rating are nested within rides; rides are nested within drivers -# -- the above nesting is maximized for comparing drivers, but in other cases one could organize by rider or date -# Which layers of data "have" within it a different layer? -# -- drivers (and rides); if organized differently, without drivers as the topmost layer, riders and date could have layers within them -# Which layers are "next" to each other? -# -- cost and rating both apply to specific rides, so are next to each other -# -- for the purposes of this exercise, date and rider are also next to cost and rating because I'm not prioritizing finding data among dates and riders - ride_share = { DR0001: [ { From b00c2c62f4c790ac3fb6521de1027070d06c5baf Mon Sep 17 00:00:00 2001 From: Janice Huang Date: Sun, 11 Aug 2019 14:37:48 -0700 Subject: [PATCH 05/11] Use .map in place of loops to find total made and avg rating --- worksheet.rb | 55 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/worksheet.rb b/worksheet.rb index 935b734..3fe8b84 100644 --- a/worksheet.rb +++ b/worksheet.rb @@ -75,6 +75,20 @@ ] } +# def find_most (array, key1, key2) +# total = 0 +# most = nil +# array.each do +# puts array[key1] +# puts array[key2] +# if [key1] > total +# total = [key1] +# most = [key2] +# end +# end +# return [total, most] +# end + count = 1 most_moolah = 0 richest_driver = nil @@ -82,28 +96,30 @@ best_driver = nil ride_share.each do |key, value| - # Calculate total money made and most lucrative day - total_moolah = 0 - most_lucrative_date = nil - most_made_in_a_day = 0 - value.each do |hash| - total_moolah += hash[:cost] - if hash[:cost] > most_made_in_a_day - most_made_in_a_day = hash[:cost] - most_lucrative_date = hash[:date] - end - end + # Make cost array for calculating amount made by each driver + cost_array = value.map {|hash| hash[:cost]} + # Make rating array for alculating average rating for each driver + rating_array = value.map {|hash| hash[:rating]} + average_rating = rating_array.sum / value.length.to_f + # Add these + value << {total_moolah: cost_array.sum, average_rating: average_rating} + p ride_share + + # Calculate most lucrative day + + # # most_lucrative_date = nil + # # most_made_in_a_day = 0 + + # most_lucrative_date = find_most(value, [:cost], [:date])[0] + # most_made_in_a_day = value[:cost] + # most_made_in_a_day = most_made_in_a_day.max + # puts most_made_in_a_day + # # ACCOUNT FOR DRIVERS W/ MULTIPLE RIDES ON ONE DAY - # Calculate average rating - total_points = 0 - value.each do |hash| - total_points += hash[:rating] - end - average = total_points / value.length.to_f # Output info for each driver - puts "Driver #{count} has given #{value.length} rides and earned an average rating of #{average}. - They made $#{total_moolah} in total. Their most lucrative day was #{most_lucrative_date} on which they made $#{most_made_in_a_day}." + puts "Driver #{count} has given #{value.length} rides and earned an average rating of #{average_rating}. + They made $#{cost_array.sum} in total. Their most lucrative day was #{most_lucrative_date} on which they made $#{most_made_in_a_day}." # Determine which driver made the most money if total_moolah > most_moolah @@ -122,3 +138,4 @@ # Output overall stats info puts "The driver who made the most money is Driver #{richest_driver}!" puts "The driver with the highest average rating is Driver #{best_driver}!" +puts ride_share \ No newline at end of file From 7c2c3ef8f035e458d3a5a399f05c4ff4e7d4de7d Mon Sep 17 00:00:00 2001 From: Janice Huang Date: Sun, 11 Aug 2019 17:13:04 -0700 Subject: [PATCH 06/11] Get code to running state with all questions answered --- worksheet.rb | 77 ++++++++++++++++++++-------------------------------- 1 file changed, 29 insertions(+), 48 deletions(-) diff --git a/worksheet.rb b/worksheet.rb index 3fe8b84..6ba0695 100644 --- a/worksheet.rb +++ b/worksheet.rb @@ -75,67 +75,48 @@ ] } -# def find_most (array, key1, key2) -# total = 0 -# most = nil -# array.each do -# puts array[key1] -# puts array[key2] -# if [key1] > total -# total = [key1] -# most = [key2] -# end -# end -# return [total, most] -# end - -count = 1 -most_moolah = 0 richest_driver = nil -highest_average = 0 best_driver = nil ride_share.each do |key, value| # Make cost array for calculating amount made by each driver cost_array = value.map {|hash| hash[:cost]} - # Make rating array for alculating average rating for each driver + # Make rating array for calculating average rating for each driver rating_array = value.map {|hash| hash[:rating]} average_rating = rating_array.sum / value.length.to_f - # Add these - value << {total_moolah: cost_array.sum, average_rating: average_rating} - p ride_share - - # Calculate most lucrative day - # # most_lucrative_date = nil - # # most_made_in_a_day = 0 - - # most_lucrative_date = find_most(value, [:cost], [:date])[0] - # most_made_in_a_day = value[:cost] - # most_made_in_a_day = most_made_in_a_day.max - # puts most_made_in_a_day - # # ACCOUNT FOR DRIVERS W/ MULTIPLE RIDES ON ONE DAY + # Calculate most lucrative day (must combine dates first) + combine_repeat_dates = Hash.new + value.each do |hash| + if combine_repeat_dates.has_key?(hash[:date]) + combine_repeat_dates[hash[:date]] += (hash[:cost]) + else + combine_repeat_dates[hash[:date]] = (hash[:cost]) + end + end + most_made_in_a_day = combine_repeat_dates.values.max + most_lucrative_date = combine_repeat_dates.key(most_made_in_a_day) + # Add new data to the array for each driver value + value << {total_moolah: cost_array.sum, average_rating: average_rating} # Output info for each driver - puts "Driver #{count} has given #{value.length} rides and earned an average rating of #{average_rating}. + puts "Driver #{key.slice(5)} has given #{value.length-1} rides and earned an average rating of #{average_rating}. They made $#{cost_array.sum} in total. Their most lucrative day was #{most_lucrative_date} on which they made $#{most_made_in_a_day}." - - # Determine which driver made the most money - if total_moolah > most_moolah - most_moolah = total_moolah - richest_driver = count - end - - # Determine which driver had the highest average rating - if average > highest_average - highest_average = average - best_driver = count - end - count += 1 +end + +# Determine which driver made the most money and which had the highest avg rating +driver_moolah_totals = {} +driver_rating_totals = {} +ride_share.each do |key, value| + driver_moolah_totals[key] = value[-1][:total_moolah] + driver_rating_totals[key] = value[-1][:average_rating] end +richest_driver_income = driver_moolah_totals.values.max +best_driver_rating = driver_rating_totals.values.max +richest_driver = driver_moolah_totals.key(richest_driver_income) +best_driver = driver_rating_totals.key(best_driver_rating) # Output overall stats info -puts "The driver who made the most money is Driver #{richest_driver}!" -puts "The driver with the highest average rating is Driver #{best_driver}!" -puts ride_share \ No newline at end of file +puts "The driver who made the most money is Driver #{richest_driver.slice(5)}!" +puts "The driver with the highest average rating is Driver #{best_driver.slice(5)}!" From 7097abb2f745bf18043b6dfc1122114b3a65b93a Mon Sep 17 00:00:00 2001 From: Janice Huang Date: Sun, 11 Aug 2019 17:22:26 -0700 Subject: [PATCH 07/11] Add method --- worksheet.rb | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/worksheet.rb b/worksheet.rb index 6ba0695..60f0c18 100644 --- a/worksheet.rb +++ b/worksheet.rb @@ -75,8 +75,15 @@ ] } -richest_driver = nil -best_driver = nil +def find_winner (hash, criteria) + totals = {} + hash.each do |key, value| + totals[key] = value[-1][criteria] + end + winning_amount = totals.values.max + winner = totals.key(winning_amount) + return winner +end ride_share.each do |key, value| # Make cost array for calculating amount made by each driver @@ -106,16 +113,8 @@ end # Determine which driver made the most money and which had the highest avg rating -driver_moolah_totals = {} -driver_rating_totals = {} -ride_share.each do |key, value| - driver_moolah_totals[key] = value[-1][:total_moolah] - driver_rating_totals[key] = value[-1][:average_rating] -end -richest_driver_income = driver_moolah_totals.values.max -best_driver_rating = driver_rating_totals.values.max -richest_driver = driver_moolah_totals.key(richest_driver_income) -best_driver = driver_rating_totals.key(best_driver_rating) +richest_driver = find_winner(ride_share, :total_moolah) +best_driver = find_winner(ride_share, :average_rating) # Output overall stats info puts "The driver who made the most money is Driver #{richest_driver.slice(5)}!" From 78e8780a6a3a1818ce2d9c2b23271d18152e41d6 Mon Sep 17 00:00:00 2001 From: Janice Huang Date: Sun, 11 Aug 2019 17:34:58 -0700 Subject: [PATCH 08/11] With trailing whitespace trimmed --- worksheet.rb | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/worksheet.rb b/worksheet.rb index 60f0c18..e65dd41 100644 --- a/worksheet.rb +++ b/worksheet.rb @@ -5,40 +5,40 @@ rider: "RD0003", cost: 10, rating: 3 - }, + }, { date: "3rd Feb 2016", rider: "RD0015", cost: 30, rating: 4 - }, + }, { date: "5th Feb 2016", rider: "RD0003", cost: 45, rating: 2 - } - ], + } + ], DR0002: [ { date: "3rd Feb 2016", rider: "RD0073", cost: 25, rating: 5 - }, + }, { date: "4th Feb 2016", rider: "RD0013", cost: 15, rating: 1 - }, + }, { date: "5th Feb 2016", rider: "RD0066", cost: 35, rating: 3 } - ], + ], DR0003: [ { date: "4th Feb 2016", @@ -59,7 +59,7 @@ rider: "RD0022", cost: 5, rating: 5 - }, + }, { date: "4th Feb 2016", rider: "RD0022", @@ -78,12 +78,12 @@ def find_winner (hash, criteria) totals = {} hash.each do |key, value| - totals[key] = value[-1][criteria] - end - winning_amount = totals.values.max + totals[key] = value[-1][criteria] + end + winning_amount = totals.values.max winner = totals.key(winning_amount) return winner -end +end ride_share.each do |key, value| # Make cost array for calculating amount made by each driver @@ -97,18 +97,18 @@ def find_winner (hash, criteria) value.each do |hash| if combine_repeat_dates.has_key?(hash[:date]) combine_repeat_dates[hash[:date]] += (hash[:cost]) - else + else combine_repeat_dates[hash[:date]] = (hash[:cost]) end end - most_made_in_a_day = combine_repeat_dates.values.max + most_made_in_a_day = combine_repeat_dates.values.max most_lucrative_date = combine_repeat_dates.key(most_made_in_a_day) # Add new data to the array for each driver value - value << {total_moolah: cost_array.sum, average_rating: average_rating} + value << {total_moolah: cost_array.sum, average_rating: average_rating} # Output info for each driver - puts "Driver #{key.slice(5)} has given #{value.length-1} rides and earned an average rating of #{average_rating}. + puts "Driver #{key.slice(5)} has given #{value.length-1} rides and earned an average rating of #{average_rating}. They made $#{cost_array.sum} in total. Their most lucrative day was #{most_lucrative_date} on which they made $#{most_made_in_a_day}." end From 3af0a87e95a0076538f65772afeb81f9c49b7ca7 Mon Sep 17 00:00:00 2001 From: Janice Huang Date: Sun, 11 Aug 2019 20:34:00 -0700 Subject: [PATCH 09/11] Make method simpler and clearer --- worksheet.rb | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/worksheet.rb b/worksheet.rb index e65dd41..a60eec8 100644 --- a/worksheet.rb +++ b/worksheet.rb @@ -75,13 +75,17 @@ ] } -def find_winner (hash, criteria) - totals = {} - hash.each do |key, value| - totals[key] = value[-1][criteria] +def find_driver_with_max (drivers, criteria) + winner = nil + winning_amount = 0 + drivers.each do |driver, value| + amount = value[-1][criteria] + if amount > winning_amount + winning_amount = amount + winner = driver + end + # This doesn't catch ties! end - winning_amount = totals.values.max - winner = totals.key(winning_amount) return winner end @@ -113,8 +117,8 @@ def find_winner (hash, criteria) end # Determine which driver made the most money and which had the highest avg rating -richest_driver = find_winner(ride_share, :total_moolah) -best_driver = find_winner(ride_share, :average_rating) +richest_driver = find_driver_with_max(ride_share, :total_moolah) +best_driver = find_driver_with_max(ride_share, :average_rating) # Output overall stats info puts "The driver who made the most money is Driver #{richest_driver.slice(5)}!" From 6e5c8390d46eb68862d5076150674ad94a239166 Mon Sep 17 00:00:00 2001 From: Janice Huang Date: Sun, 11 Aug 2019 20:40:00 -0700 Subject: [PATCH 10/11] Change variable names to be more descriptive --- worksheet.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/worksheet.rb b/worksheet.rb index a60eec8..fb31f90 100644 --- a/worksheet.rb +++ b/worksheet.rb @@ -89,30 +89,30 @@ def find_driver_with_max (drivers, criteria) return winner end -ride_share.each do |key, value| +ride_share.each do |driver, rides| # Make cost array for calculating amount made by each driver - cost_array = value.map {|hash| hash[:cost]} + cost_array = rides.map {|ride| ride[:cost]} # Make rating array for calculating average rating for each driver - rating_array = value.map {|hash| hash[:rating]} - average_rating = rating_array.sum / value.length.to_f + rating_array = rides.map {|ride| ride[:rating]} + average_rating = rating_array.sum / rides.length.to_f # Calculate most lucrative day (must combine dates first) combine_repeat_dates = Hash.new - value.each do |hash| - if combine_repeat_dates.has_key?(hash[:date]) - combine_repeat_dates[hash[:date]] += (hash[:cost]) + rides.each do |ride| + if combine_repeat_dates.has_key?(ride[:date]) + combine_repeat_dates[ride[:date]] += (ride[:cost]) else - combine_repeat_dates[hash[:date]] = (hash[:cost]) + combine_repeat_dates[ride[:date]] = (ride[:cost]) end end most_made_in_a_day = combine_repeat_dates.values.max most_lucrative_date = combine_repeat_dates.key(most_made_in_a_day) # Add new data to the array for each driver value - value << {total_moolah: cost_array.sum, average_rating: average_rating} + rides << {total_moolah: cost_array.sum, average_rating: average_rating} # Output info for each driver - puts "Driver #{key.slice(5)} has given #{value.length-1} rides and earned an average rating of #{average_rating}. + puts "Driver #{driver.slice(5)} has given #{rides.length-1} rides and earned an average rating of #{average_rating}. They made $#{cost_array.sum} in total. Their most lucrative day was #{most_lucrative_date} on which they made $#{most_made_in_a_day}." end From cf92207ed3353fbcd26d0e6a7fbaf3daa0a6c045 Mon Sep 17 00:00:00 2001 From: Janice Huang Date: Sun, 11 Aug 2019 21:21:01 -0700 Subject: [PATCH 11/11] Added new hash to make things less weird --- worksheet.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/worksheet.rb b/worksheet.rb index fb31f90..581b651 100644 --- a/worksheet.rb +++ b/worksheet.rb @@ -75,11 +75,13 @@ ] } +new_data = Hash.new + def find_driver_with_max (drivers, criteria) winner = nil winning_amount = 0 - drivers.each do |driver, value| - amount = value[-1][criteria] + drivers.each do |driver, data| + amount = data[criteria] if amount > winning_amount winning_amount = amount winner = driver @@ -108,17 +110,20 @@ def find_driver_with_max (drivers, criteria) most_made_in_a_day = combine_repeat_dates.values.max most_lucrative_date = combine_repeat_dates.key(most_made_in_a_day) - # Add new data to the array for each driver value - rides << {total_moolah: cost_array.sum, average_rating: average_rating} + # Store new data in a hash of hashes + new_data[driver] = { + total_moolah: cost_array.sum, + average_rating: average_rating + } # Output info for each driver - puts "Driver #{driver.slice(5)} has given #{rides.length-1} rides and earned an average rating of #{average_rating}. + puts "Driver #{driver.slice(5)} has given #{rides.length} rides and earned an average rating of #{average_rating}. They made $#{cost_array.sum} in total. Their most lucrative day was #{most_lucrative_date} on which they made $#{most_made_in_a_day}." end # Determine which driver made the most money and which had the highest avg rating -richest_driver = find_driver_with_max(ride_share, :total_moolah) -best_driver = find_driver_with_max(ride_share, :average_rating) +richest_driver = find_driver_with_max(new_data, :total_moolah) +best_driver = find_driver_with_max(new_data, :average_rating) # Output overall stats info puts "The driver who made the most money is Driver #{richest_driver.slice(5)}!"