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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
154 changes: 125 additions & 29 deletions worksheet.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,130 @@
########################################################
# Step 1: Establish the layers
ride_share = {
DR0001: [
{
date: "3rd Feb 2016",
rider: "RD0003",
cost: 10,
rating: 3
},
{
date: "3rd Feb 2016",
rider: "RD0015",
cost: 30,
rating: 4
},
{
date: "5th Feb 2016",
rider: "RD0003",
cost: 45,
rating: 2
}
],
DR0002: [
{
date: "3rd Feb 2016",
rider: "RD0073",
cost: 25,
rating: 5
},
{
date: "4th Feb 2016",
rider: "RD0013",
cost: 15,
rating: 1
},
{
date: "5th Feb 2016",
rider: "RD0066",
cost: 35,
rating: 3
}
],
DR0003: [
{
date: "4th Feb 2016",
rider: "RD0066",
cost: 5,
rating: 5
},
{
date: "5th Feb 2016",
rider: "RD0003",
cost: 50,
rating: 2
}
],
DR0004: [
{
date: "3rd Feb 2016",
rider: "RD0022",
cost: 5,
rating: 5
},
{
date: "4th Feb 2016",
rider: "RD0022",
cost: 10,
rating: 4
},
{
date: "5th Feb 2016",
rider: "RD0073",
cost: 20,
rating: 5
}
]
}

# 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?
new_data = Hash.new

########################################################
# Step 2: Assign a data structure to each layer
def find_driver_with_max (drivers, criteria)
winner = nil
winning_amount = 0
drivers.each do |driver, data|
amount = data[criteria]
if amount > winning_amount
winning_amount = amount
winner = driver
end
# This doesn't catch ties!
end
return winner
end

# Copy your list from above, and in this section
# determine what data structure each layer should have
ride_share.each do |driver, rides|
# Make cost array for calculating amount made by each driver
cost_array = rides.map {|ride| ride[:cost]}
# Make rating array for calculating average rating for each driver
rating_array = rides.map {|ride| ride[:rating]}
average_rating = rating_array.sum / rides.length.to_f

# Calculate most lucrative day (must combine dates first)
combine_repeat_dates = Hash.new
rides.each do |ride|
if combine_repeat_dates.has_key?(ride[:date])
combine_repeat_dates[ride[:date]] += (ride[:cost])
else
combine_repeat_dates[ride[:date]] = (ride[:cost])
end
end
most_made_in_a_day = combine_repeat_dates.values.max
most_lucrative_date = combine_repeat_dates.key(most_made_in_a_day)

# Store new data in a hash of hashes
new_data[driver] = {
total_moolah: cost_array.sum,

Choose a reason for hiding this comment

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

Moolah, nice!

average_rating: average_rating
}

# Output info for each driver
puts "Driver #{driver.slice(5)} has given #{rides.length} rides and earned an average rating of #{average_rating}.
They made $#{cost_array.sum} in total. Their most lucrative day was #{most_lucrative_date} on which they made $#{most_made_in_a_day}."
end

########################################################
# Step 3: Make the data structure!
# Determine which driver made the most money and which had the highest avg rating
richest_driver = find_driver_with_max(new_data, :total_moolah)
best_driver = find_driver_with_max(new_data, :average_rating)

Choose a reason for hiding this comment

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

Nice use of helper methods here!


# 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?
# Output overall stats info
puts "The driver who made the most money is Driver #{richest_driver.slice(5)}!"
puts "The driver with the highest average rating is Driver #{best_driver.slice(5)}!"