Skip to content
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
104 changes: 104 additions & 0 deletions branches- vi rideshare
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
########################################################
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Now that we're working in git, we'll have better ways to upload files, but I wanna point out that you named this file after your PR, which is nice, but I wanted this as worksheet.rb instead of branches- vi rideshare :)

# Step 1: Establish the layers
# 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?

# The drivers in the data set are a layer, which have 'nested' in them
# a set of information. They all have the same type of data, such as cost.
# Their cost, rating, date, etc are all 'next' to each other. The drivers
# themselves are also next to each other within a different layer as well,
# which contains each trips information.
########################################################
# 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
# In my code, I have created individual hashes for each trip the driver
# has taken. These hashes are nested within an array which belongs to each driver.
# Each driver also represents a key in a hash, where the array represents their value.

########################################################
# 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

drivers =
{
"DR0001": [
{"date": "3_feb_2016", "cost": "10", "rider_id": "RD0003", "rating": "3"},
{"date": "3_feb_2016", "cost": "30", "rider_id": "RD0015", "rating": "4"},
{"date": "5_feb_2016", "cost": "45", "rider_id": "RD0003", "rating": "2"}
],
"DR0002": [
{"date": "3_feb_2016", "cost": "25", "rider_id": "RD0073", "rating": "5"},
{"date": "4_feb_2016", "cost": "15", "rider_id": "RD0013", "rating": "1"},
{"date": "5_feb_2016", "cost": "35", "rider_id": "RD0066", "rating": "3"}
],
"DR0003": [
{"date": "4_feb_2016", "cost": "5", "rider_id": "RD0066", "rating": "5"},
{"date": "5_feb_2016", "cost": "50", "rider_id": "RD0003", "rating": "2"},
],
"DR0004": [
{ "date": "3_feb_2016", "cost": "5", "rider_id": "RD0022", "rating": "5" },
{ "date": "4_feb_2016", "cost": "10", "rider_id": "RD0022", "rating": "4"},
{ "date": "5_feb_2016", "cost": "20", "rider_id": "RD0073", "rating": "5"}
]
}

########################################################
# Step 4: Total Driver's Earnings and Number of Rides
# Use an iteration blocks to print the following answers:

#1. The number of rides each driver has given

#Loop inside the drivers hash
#List each key along with the # of elements in its associated array.
drivers.each do |driverName, rides|
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These variable names prove to me that you know the structure of your data deeply. Nice work!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In Ruby, we tend to name variables using snake_case, meaning instead of driverName I may see driver_name instead

puts "Driver #{driverName} gave #{rides.length} rides"
end

#2. Find the total amount of money each driver has made

#Loop inside drivers hash
drivers.each do |driverName, trips|
total = trips.inject(0) { |sum, trip| sum + trip[:cost].to_i}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice use of inject!

puts "Driver #{driverName} has made $#{total}"
end

#3. The average rating for each driver

drivers.each do |driverName, trips|
avg_rating = trips.inject(0) { |sum, trip| sum + trip[:rating].to_i } / trips.size
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It would probably be a more accurate calculation if we did trip[:rating].to_f instead of to_i

puts "Driver #{driverName} had an average rating of #{avg_rating} stars"
end

# - Which driver made the most money?
most_money = 0
winner = ""

drivers.each do |driverName, trips|
driver_total = trips.inject(0) { |sum, trip| sum + trip[:cost].to_i}
if driver_total > most_money
most_money = driver_total
winner = driverName
end
end
puts "The driver who made the most money is #{winner}"


# - Which driver has the highest average rating?
highest_rating = 0
rating_winner_driver = ""

drivers.each do |driverName, trips|
avg_rating = trips.inject(0) { |sum, trip| sum + trip[:rating].to_i } / trips.size
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You end up having this line that calculates average rating based off of an array called trips duplicated. If you had more time to refactor, I may suggest pulling it into a new method. Imagine a method like this:

def calculate_avg_rating(trips)
  return trips.inject(0) { |sum, trip| sum + trip[:rating].to_i } / trips.size 
end

Therefore, this line may be changed to this:

avg_rating = calculate_avg_rating(trips)

if avg_rating > highest_rating
highest_rating = avg_rating
rating_winner_driver = driverName
end
end
puts "The driver with the highest average rating is #{rating_winner_driver}"