Skip to content
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

Leaves - Bri #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions ride_share.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
rides = [
{
driver_id: 'DR0004',
date: '3rd Feb 2016',
cost: '5',
rating: '5',
rider_id: 'RD0022',
},
{
driver_id: 'DR0001',
date: '3rd Feb 2016',
cost: '10',
rating: '3',
rider_id: 'RD0003',
},
{
driver_id: 'DR0002',
date: '3rd Feb 2016',
cost: '25',
rating: '5',
rider_id: 'RD0073'
},
{
driver_id: 'DR0001',
date: '3rd Feb 2016',
cost: '30',
rating: '4',
rider_id: 'RD0015'
},
{
driver_id: 'DR0003',
date: '4th Feb 2016',
cost: '5',
rating: '5',
rider_id: 'RD0066'
},
{
driver_id: 'DR0004',
date: '4th Feb 2016',
cost: '10',
rating: '4',
rider_id: 'RD0022'
},
{
driver_id: 'DR0002',
date: '4th Feb 2016',
cost: '15',
rating: '1',
rider_id: 'RD0013'
},
{
driver_id: 'DR0003',
date: '5th Feb 2016',
cost: '50',
rating: '2',
rider_id: 'RD0003'
},
{
driver_id: 'DR0002',
date: '5th Feb 2016',
cost: '35',
rating: '3',
rider_id: 'RD0066'
},
{
driver_id: 'DR0004',
date: '5th Feb 2016',
cost: '20',
rating: '5',
rider_id: 'RD0073'
},
{
driver_id: 'DR0001',
date: '5th Feb 2016',
cost: '45',
rating: '2',
rider_id: 'RD0003'
}
]

driver_ids = ["DR0001", "DR0002", "DR0003", "DR0004"]

# Question 1: The number of rides each driver has given?
def driver_ride_count(driver_id, rides)
driver_rides = rides.select { |ride| ride [:driver_id] == driver_id }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's interesting that you start this method with select, because it means that the first thing that you do is group all of your rides by driver. This process costs your program a little bit of time as it has to loop through the whole array. Could you remove this extra work by restructuring your data?

total_num_rides_for_driver = driver_rides.length
puts "Problem 1: #{driver_id}: #{total_num_rides_for_driver} rides"
end

driver_ids.each do |id|
driver_ride_count(id, rides)
end

# Question 2: The total amount of money each driver has made?
def driver_money_made(driver_id, rides)
driver_rides = rides.select { |ride| ride [:driver_id] == driver_id }
total_cost_for_driver = driver_rides.sum {|ride| ride [:cost].to_i }
puts "Problem 2: $#{total_cost_for_driver} was made by Driver #{driver_id}"
end

driver_ids.each do |id|
driver_money_made(id, rides)
end

# Question 3: The average rating for each driver?
def driver_average_rating(driver_id, rides)
driver_rides = rides.select { |ride| ride [:driver_id] == driver_id }
sum_rating_per_driver = driver_rides.sum {|ride| ride [:rating].to_f }
total_num_rides_for_driver = driver_rides.length
puts "Problem 3: #{driver_id}'s average rating: #{sum_rating_per_driver / total_num_rides_for_driver}"
end

driver_ids.each do |id|
driver_average_rating(id, rides)
end

# Question 4: Which driver made the most money?
def driver_money_made(driver_id, rides)
driver_rides = rides.select { |ride| ride [:driver_id] == driver_id }
total_cost_for_driver = driver_rides.sum {|ride| ride [:cost].to_i }
return total_cost_for_driver
end

drivers_income = driver_ids.map do |driver_id|
driver_money_made(driver_id, rides)
end

max_income = drivers_income.max()
index_of_max = drivers_income.find_index(max_income)
puts "Problem 4: #{driver_ids[index_of_max]} made the most money"

# Question 5: Which driver has the highest average rating?
def driver_average_rating(driver_id, rides)
driver_rides = rides.select { |ride| ride [:driver_id] == driver_id }
sum_rating_per_driver = driver_rides.sum {|ride| ride [:rating].to_f }
total_num_rides_for_driver = driver_rides.length
return sum_rating_per_driver / total_num_rides_for_driver
end

rating_of_drivers = driver_ids.map do |driver_id|
driver_average_rating(driver_id, rides)
end

max_rating = rating_of_drivers.max()
index_of_max = rating_of_drivers.find_index(max_rating)
puts "Problem 5: #{driver_ids[index_of_max]} had the highest average rating"