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

Laura Robertson -- Carets #30

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
105 changes: 103 additions & 2 deletions worksheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,119 @@
# Step 1: Establish the layers

# Write a list of the layers here
# Hash, Array, Hash.

########################################################
# Step 2: Assign a data structure to each layer

# Copy your list from above, and write what data structure each layer should have
# Driver id HASH (key = id#) to an ARRAY (value) of all rides under
# that id. Then a HASH of information from each of their rides.

########################################################
# Step 3: Make the data structure!

# Setup the data strcture and manually write in data presented in rides.csv
# Setup the data structure and manually write in data presented in rides.csv

driver_id_rides = {

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

########################################################
# Step 4: Total Divers Earnings and Number of Rides
# Step 4: Total Drivers Earnings and Number of Rides

# Use an iteration block to print driver's total rides and money made

# Inside the first hash, key=driver_id_rides value=array
driver_id_rides.each do |key, value|
puts "\n-----------------"
puts "Driver Id: " + key.to_s
puts "-----------------"
puts "Has given #{value.length} rides and"

cost_array = []
rating_array = []

value.each do |index|
# For each matching key, it adds the value to the selected array
index.each {|key2,value2| cost_array << value2 if key2 == :cost}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is kinda neat, but overly complicated.

You could instead do:

cost_array << value[:cost]

Be careful about overly complicating things.

index.each {|key2,value2| rating_array << value2 if key2 == :rating}
end

puts "has earned $" + cost_array.inject(:+).to_s + " dollars"
puts "Average rating: " + (rating_array.inject(:+).to_f / rating_array.length).round.to_s
end