-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rideshare Reflection #42
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,26 @@ | |
|
||
# In this section of the file, as a series of comments, | ||
# create a list of the layers you identify. | ||
# driver_records = hash | ||
# drivers = array | ||
# date cost, riders, ratings = nested hash that have nested arrays | ||
# Which layers are nested in each other? | ||
# arrays in each hash, that's nested in an array | ||
# drivers are nested in the whole hash structure, driver_records | ||
# hashes containing data: date/cost/rider/rating nested in the driver's array | ||
# collection of arrays nested in the data hash | ||
# Which layers of data "have" within it a different layer? | ||
# depending on the date, the cost/rider/ratings may have a collection of array | ||
# Which layers are "next" to each other? | ||
|
||
# hashes of the next date the driver drove | ||
# arrays of different drivers are next to each other (separated by hashes) | ||
######################################################## | ||
# 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 | ||
# Hash of an array of hashes that will have nested arrays...? | ||
# Some will have a collection of arrays inside a hash data structure | ||
|
||
######################################################## | ||
# Step 3: Make the data structure! | ||
|
@@ -31,4 +42,134 @@ | |
# - 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? | ||
# - Which driver has the highest average rating? | ||
|
||
driver_records = { | ||
DR0001: [ | ||
{ | ||
date: "3rd Feb 2016", | ||
cost: 10, | ||
rider: "RD0003", | ||
rating: 3 | ||
}, | ||
{ | ||
date: "3rd Feb 2016", | ||
cost: 30, | ||
rider: "RD0015", | ||
rating: 4 | ||
}, | ||
{ | ||
date: "5th Feb 2016", | ||
cost: 45, | ||
rider: "RD0003", | ||
rating: 2 | ||
} | ||
], | ||
DR0002: [ | ||
{ | ||
date: "3rd Feb 2016", | ||
cost: 25, | ||
rider: "RD0073", | ||
rating: 5 | ||
}, | ||
{ | ||
date: "4th Feb 2016", | ||
cost: 15, | ||
rider: "RD0013", | ||
rating: 1 | ||
}, | ||
{ | ||
date: "5th Feb 2016", | ||
cost: 35, | ||
rider: "RD0066", | ||
rating: 3 | ||
} | ||
], | ||
DR0003: [ | ||
{ | ||
date: "4th Feb 2016", | ||
cost: 5, | ||
rider: "RD0066", | ||
rating: 5 | ||
}, | ||
{ | ||
date: "5th Feb 2016", | ||
cost: 50, | ||
rider: "RD0003", | ||
rating: 2 | ||
} | ||
], | ||
DR0004: [ | ||
{ | ||
date: "3rd Feb 2016", | ||
cost: 5, | ||
rider: "RD0022", | ||
rating: 5 | ||
}, | ||
{ | ||
date: "4th Feb 2016", | ||
cost: 10, | ||
rider: "RD0022", | ||
rating: 4 | ||
}, | ||
{ | ||
date: "5th Feb 2016", | ||
cost: 20, | ||
rider: "RD0073", | ||
rating: 5 | ||
} | ||
] | ||
} | ||
|
||
# - the number of rides each driver has given | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like you pass the following three functions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On that note, consider combining the following three methods into one to reduce repeated code. You could call it something like |
||
def sum_rides(rides_list) | ||
rides_list.each do |driver, trip| | ||
puts "#{driver} gave #{trip.length} rides" | ||
end | ||
return rides_list | ||
end | ||
|
||
# - the total amount of money each driver has made | ||
def sum_money(total_cost) | ||
total_earned = 0 | ||
|
||
total_cost.each do |driver, cost_value| | ||
cost_value.each do |earning| | ||
trip_cost = earning[:cost] | ||
total_earned += trip_cost | ||
end | ||
puts "#{driver} made $#{'%.2f' % total_earned}" | ||
total_earned = 0 | ||
end | ||
return total_earned | ||
end | ||
|
||
# - the average rating for each driver | ||
def find_average(driver_rating) | ||
rating_total = 0 | ||
rating_average = 0 | ||
|
||
driver_rating.each do |driver, rating| | ||
rating.each do |review| | ||
rider_review = review[:rating] | ||
rating_total += rider_review | ||
rating_average = rating_total / rating.length | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that you used integer division for the average rating, and lost a little precision. |
||
end | ||
puts "#{driver} average rating: #{rating_average}" | ||
rating_total = 0 | ||
end | ||
return rating_average | ||
end | ||
|
||
# - Which driver made the most money? | ||
# - Which driver has the highest average rating? | ||
Comment on lines
+164
to
+165
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have done all the work above to make answering these questions possible. If you call the methods above for all the drivers, you could collect the data in an array, and then find the max. Let me know if you'd like to go over this! |
||
|
||
#### OUTPUTS #### | ||
puts "Number of rides each driver has given:" | ||
sum_rides(driver_records) | ||
|
||
puts "\nTotal amount of money each driver has made:" | ||
sum_money(driver_records) | ||
|
||
puts "\nAverage rating for each driver:" | ||
find_average(driver_records) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work formatting this data structure for readability.