-
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
Ride-Share Project #53
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 |
---|---|---|
@@ -1,34 +1,188 @@ | ||
|
||
######################################################## | ||
# Step 1: Establish the layers | ||
# Step 1: Establish the layers: Create a list of the layers you identify. | ||
# TOP LEVEL: Drivers | ||
# MIDDLE LAYER: All trips taken | ||
# BOTTOM LAYER: Individual Rides Info | ||
|
||
# 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? | ||
# Bottom layer has the nested info | ||
# | ||
# # Which layers of data "have" within it a different layer? | ||
# driver_array >> drivers_id >> date- cost -rider_id rating | ||
|
||
# Which layers are "next" to each other? | ||
# 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 | ||
# TOP LEVEL: A Hash of All Drivers info | ||
# MIDDLE LAYER: Is an Array with unique Driver IDs | ||
# BOTTOM LAYER: Nested within array is a Hash with individual Ride Info Keys (Date/Cost/Rider_ID/Rating) | ||
|
||
|
||
######################################################## | ||
# 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? | ||
drivers_array = { | ||
DR0001: [ | ||
{ | ||
date: "Feb 3rd 2016", | ||
cost: 10, | ||
rider_id: "RD0003", | ||
rating: 3 | ||
}, | ||
{ | ||
date: "Feb 3rd 2016", | ||
cost: 30, | ||
rider_id: "RD0015", | ||
rating: 4 | ||
}, | ||
{ | ||
date: "Feb 2nd 2016", | ||
cost: 45, | ||
rider_id: "RD0003", | ||
rating: 2 | ||
} | ||
], | ||
DR0002: [ | ||
{ | ||
date: "Feb 2nd 2016", | ||
cost: 25, | ||
rider_id: "RD0073", | ||
rating: 5 | ||
}, | ||
{ | ||
date: "Feb 2 2016", | ||
cost: 15, | ||
rider_id: "RD0013", | ||
rating: 1 | ||
}, | ||
{ | ||
date: "Feb 2nd 2016", | ||
cost: 35, | ||
rider_id: "RD0066", | ||
rating: 3 | ||
} | ||
], | ||
DR0003: [ | ||
{ | ||
date: "Feb 2nd 2016", | ||
cost: 5, | ||
rider_id: "RD0066", | ||
rating: 5 | ||
}, | ||
{ | ||
date: "Feb 2nd 2016", | ||
cost: 50, | ||
rider_id: "RD0003", | ||
rating: 2 | ||
} | ||
], | ||
DR0004: [ | ||
{ | ||
date: "Feb 2nd 2016", | ||
cost: 5, | ||
rider_id: "RD0022", | ||
rating: 5 | ||
}, | ||
{ | ||
day: "Feb 2nd 2016", | ||
cost: 10, | ||
rider_id: "RD0022", | ||
rating: 4 | ||
}, | ||
{ | ||
date: "Feb 2nd 2016", | ||
cost: 20, | ||
rider_id: "RD0073", | ||
rating: 5 | ||
} | ||
] | ||
} | ||
|
||
|
||
# QUESTION ONE: The number of rides each driver has given? | ||
# ----------------------------------- | ||
# method to see how many rides each driver has done | ||
def num_of_rides_method(drivers_array) | ||
drivers_array.each do |driver_id, rides_array| | ||
puts "The driver with ID: #{driver_id} gave #{rides_array.length} rides" | ||
end | ||
end | ||
|
||
num_of_rides_method(drivers_array) | ||
|
||
# QUESTION TWO: The total amount of money each driver has made? | ||
# ----------------------------------- | ||
# method that sees how much money each driver made | ||
|
||
def driver_earning_method(driver_array) | ||
drivers_earnings= 0 | ||
driver_array.each do |driver,ride_array| | ||
total = 0 | ||
ride_array.each do |ride| | ||
total += ride[:cost] | ||
end | ||
puts "The driver with ID: #{driver} made $ #{total}" | ||
end | ||
end | ||
puts "" | ||
|
||
driver_earning_method(drivers_array) | ||
puts "" | ||
|
||
|
||
#QUESTION THREE # - What is the average rating for each driver? | ||
|
||
drivers_array.each do |driver,rides_array| | ||
total_driver_ratings = 0 | ||
|
||
rides_array.each do |ride_hash| | ||
total_driver_ratings += ride_hash[:rating] | ||
end | ||
average= total_driver_ratings.to_f/rides_array.length | ||
puts "Driver with ID: #{driver}, has a rating average of #{average.round(1)}" | ||
end | ||
puts "" | ||
Comment on lines
+140
to
+149
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. Consider encapsulating this functionality into a method to be consistent with the work for questions 1 and 2. Also, you might consider combining the work for question 1-3 into a single method since they all require the same driving each loop. It is a tricky balance between dividing up functionality so make each method truly only do one thing, and reducing repeated code. |
||
|
||
# QUESTION FOUR: Which driver made the most money? | ||
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. Just as with questions 1-3, you could consider combining the work for questions 4 and 5. |
||
|
||
def driver_max(driver_array) | ||
total_hash = {} | ||
driver_array.each do |driver, rides_array| | ||
total = 0 | ||
rides_array.each do |ride| | ||
total += ride[:cost] | ||
end | ||
total_hash[driver] = total | ||
end | ||
return total_hash | ||
end | ||
|
||
highest_earning_driver = driver_max(drivers_array).max_by {|driver,ride_total|ride_total} | ||
puts "The driver with ID:#{highest_earning_driver[0]} earned the most with a total of $ #{highest_earning_driver[1]}." | ||
|
||
#QUESTION FIVE: Which driver has the highest average rating? | ||
#method to find the highest rating | ||
def highest_rating_method(drivers_array) | ||
total_rating_hash= {} | ||
drivers_array.each do |driver,rides_array| | ||
total = 0 | ||
rides_array.each do |ride| | ||
total += ride[:rating] | ||
end | ||
Comment on lines
+174
to
+176
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. Here as a place your could use the |
||
average = (total.to_f/rides_array.length) | ||
total_rating_hash[driver]= average.round(1) | ||
end | ||
return total_rating_hash | ||
end | ||
|
||
puts "" | ||
|
||
highest_rating= highest_rating_method(drivers_array).max_by { |driver,rate_totals|rate_totals} | ||
|
||
puts " The driver with ID:#{highest_rating[0]} had the highest rating with #{highest_rating[1]}" | ||
|
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.
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.
Minor note: the data structure is a hash, but you name it with _array.