-
Notifications
You must be signed in to change notification settings - Fork 47
Branches - Erika #40
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
base: master
Are you sure you want to change the base?
Branches - Erika #40
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 |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| drive_history = { | ||
| DR0001: [ | ||
| {cost: 10, rating: 3, id_number: "RD0003", date: "3rd Feb 2016"}, | ||
| {cost: 30, rating: 4, id_number: "RD0015", date: "3rd Feb 2016"}, | ||
| {cost: 45, rating: 2, id_number: "RD0003", date: "5th Feb 2016"} | ||
| ], | ||
| DR0002: [ | ||
| {cost: 25, rating: 5, id_number: "RD0073", date: "3rd Feb 2016"}, | ||
| {cost: 15, rating: 1, id_number: "RD0013", date: "4th Feb 2016"}, | ||
| {cost: 35, rating: 3, id_number: "RD0066", date: "5th Feb 2016"} | ||
| ], | ||
| DR0003: [ | ||
| {cost: 05, rating: 5, id_number: "RD0066", date: "4th Feb 2016"}, | ||
| {cost: 50, rating: 2, id_number: "RD0003", date: "5th Feb 2016"} | ||
| ], | ||
| DR0004: [ | ||
| {cost: 05, rating: 5, id_number: "RD0022", date: "3rd Feb 2016"}, | ||
| {cost: 10, rating: 4, id_number: "RD0022", date: "4th Feb 2016"}, | ||
| {cost: 20, rating: 5, id_number: "RD0073", date: "5th Feb 2016"} | ||
| ] | ||
| } | ||
|
|
||
| puts " *** Total Rides Per Driver:" | ||
| rides_completed = [] | ||
| drive_history.each do |driver, key| | ||
| puts "For driver #{driver}, the number of rides given was: #{key.length}" | ||
| drive_number = key.length | ||
| rides_completed << drive_number | ||
| end | ||
|
|
||
|
|
||
| def profit_per_driver(drive_history, driver) | ||
| cost_array = drive_history[driver].map do |ride| | ||
| cost_each = ride[:cost] | ||
| end | ||
| end | ||
|
|
||
|
|
||
| prof_one = profit_per_driver(drive_history,:DR0001).sum | ||
| prof_two = profit_per_driver(drive_history,:DR0002).sum | ||
| prof_three = profit_per_driver(drive_history,:DR0003).sum | ||
| prof_four = profit_per_driver(drive_history,:DR0004).sum | ||
|
|
||
|
|
||
| puts " *** Total Earnings:" | ||
| puts "Driver DR0001 made a total of $#{prof_one}." | ||
| puts "Driver DR0002 made a total of $#{prof_two}." | ||
| puts "Driver DR0003 made a total of $#{prof_three}." | ||
| puts "Driver DR0004 made a total of $#{prof_four}." | ||
|
|
||
|
|
||
| profit_array = [prof_one, prof_two, prof_three, prof_four] | ||
| max = profit_array.max | ||
|
|
||
| puts " *** Max Profit:" | ||
| if max == prof_one | ||
|
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. This is not critical but I wanted to mention: this is a good case where you could keep up with the driver name for each profit so that you could read the driver_id rather than creating a whole conditional chain like this. prof_one = { driver_id: :DR0001, profit: profit_per_driver(drive_history,:DR0001).sum }
...
max = profit_array.max_by | profit_hash |
profit_hash.profit
end
puts "The maximum was earned by driver #{max.driver_id} with a total profit of $#{max.profit}."The reason I want to call this out is because if we had 100 drivers, the code below would be unmanageable. Reach out to me if that doesn't make sense. |
||
| puts "The maximum was earned by driver DR0001 with a total profit of $#{max}." | ||
| elsif max == prof_two | ||
| puts "The maximum was earned by driver DR0002 with a total profit of $#{max}." | ||
| elsif max == prof_three | ||
| puts "The maximum was earned by driver DR0003 with a total profit of $#{max}." | ||
| elsif max == prof_four | ||
| puts "The maximum was earned by driver DR0004 with a total profit of $#{max}." | ||
| end | ||
|
|
||
|
|
||
| # method for average rating per driver | ||
| def average_driver_rating(drive_history, driver, rides_completed) | ||
| rating_array = drive_history[driver].map do |ride| | ||
| rating_each = ride[:rating] | ||
| end | ||
| rating = rating_array.sum.to_f | ||
| average_rating = rating / rides_completed | ||
| end | ||
|
|
||
| # calling method for driver average | ||
| average_one = average_driver_rating(drive_history,:DR0001, rides_completed[0]) | ||
| average_two = average_driver_rating(drive_history,:DR0002, rides_completed[1]) | ||
| average_three = average_driver_rating(drive_history,:DR0003, rides_completed[2]) | ||
| average_four = average_driver_rating(drive_history,:DR0004, rides_completed[3]) | ||
|
|
||
| puts " *** Average Driver Rating:" | ||
| puts "Driver DR0001 had an average rating of #{average_one}." | ||
| puts "Driver DR0002 had an average rating of #{average_two}." | ||
| puts "Driver DR0003 had an average rating of #{average_three}." | ||
| puts "Driver DR0004 had an average rating of #{average_four}." | ||
|
|
||
| # conditional statement - print highest average | ||
| averages_array = [average_one, average_two, average_three, average_four] | ||
| max_average = averages_array.max | ||
|
|
||
| puts " *** Highest Driver Rating: " | ||
|
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. Same down here about keeping up with the driver name so that you don't need a whole conditional chain. |
||
| if max_average == average_one | ||
| puts "Driver DR0001 had highest average rating with an average of #{average_one}." | ||
| elsif max_average == average_two | ||
| puts "Driver DR0002 had highest average rating with an average of #{average_two}." | ||
| elsif max_average == average_three | ||
| puts "Driver DR0003 had highest average rating with an average of #{average_three}." | ||
| elsif max_average == average_four | ||
| puts "Driver DR0004 had highest average rating with an average of #{average_four}." | ||
| end | ||
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.
Considering this is called profit_per_driver, it'd be preferable that it found and returned the sum of the profit rather than an array that still needs to be summed.