forked from AdaGold/ride-share
-
Notifications
You must be signed in to change notification settings - Fork 54
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
Update worksheet.rb #34
Open
beauttie
wants to merge
2
commits into
Ada-C14:master
Choose a base branch
from
beauttie:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,27 @@ | |
# Which layers of data "have" within it a different layer? | ||
# Which layers are "next" to each other? | ||
|
||
# The first layer is the driver's ID, and nested in that layer | ||
# is a list of rides. Each list in that second layer corresponds | ||
# to a date (the second layer has a different layer within it), | ||
# and given that multiple rides can occur in a day, we have | ||
# another layer that lists the cost, rider's ID, and rating | ||
# for each ride (these three layers are next to each other). | ||
|
||
######################################################## | ||
# 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 | ||
|
||
# The first layer is a hash with DRIVER_ID as the keys. | ||
# The second layer is an array of rides in chronological order. | ||
# The third layer is a hash listing the characteristics of | ||
# the ride(s) in a single day (DATE, COST, RIDER_ID, RATING). | ||
# The fourth layer consists of arrays for COST, RIDER_ID, and RATING | ||
# where the element at each index corresponds to a characteristic | ||
# of a single ride on a given day. | ||
|
||
######################################################## | ||
# Step 3: Make the data structure! | ||
|
||
|
@@ -23,12 +38,135 @@ | |
# into this data structure, such as "DR0004" | ||
# and "3rd Feb 2016" and "RD0022" | ||
|
||
drivers_data = { | ||
"DR0001" => [ | ||
{ | ||
date: "3rd Feb 2016", | ||
cost: [10, 30], | ||
rider_id: ["RD0003", "RD0015"], | ||
rating: [3, 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 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 | ||
|
||
drivers_rides = drivers_data.map do |driver_id, rides| | ||
num_rides = 0 | ||
earnings = 0 | ||
sum_rating = 0 | ||
rides.each do |ride| | ||
num_rides += ride[:rider_id].length | ||
|
||
ride[:cost].each { |ride_cost| earnings += ride_cost } | ||
ride[:rating].each { |ride_rating| sum_rating += ride_rating } | ||
Comment on lines
+127
to
+128
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 very clear! You could consider using the |
||
end | ||
|
||
{ | ||
driver_id => { | ||
num_rides: num_rides, | ||
earnings: earnings, | ||
avg_rating: sum_rating.to_f / num_rides | ||
} | ||
} | ||
end | ||
|
||
# map outputs an array | ||
# p drivers_rides | ||
|
||
drivers_rides.each do |nested_hash| | ||
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. Consider giving |
||
nested_hash.each do |driver_id, hash| | ||
puts "Driver #{driver_id} has given #{hash[:num_rides]} rides, earned $#{hash[:earnings]}, and has an average rating of #{hash[:avg_rating]} stars." | ||
end | ||
end | ||
|
||
# - Which driver made the most money? | ||
# - Which driver has the highest average rating? | ||
# - Which driver has the highest average rating? | ||
|
||
def flatten_by(array_of_nested_hashes, symbol) | ||
data_subset = {} | ||
|
||
array_of_nested_hashes.each do |nested_hash| | ||
nested_hash.each do |key, hash| | ||
data_subset[key] = hash[symbol] | ||
end | ||
end | ||
|
||
return data_subset | ||
end | ||
|
||
earnings_hash = flatten_by(drivers_rides, :earnings) | ||
avg_rating_hash = flatten_by(drivers_rides, :avg_rating) | ||
|
||
# p earnings_hash | ||
# p avg_rating_hash | ||
|
||
puts "Driver #{earnings_hash.key(earnings_hash.values.max)} made the most money with a total of $#{earnings_hash.values.max}." | ||
|
||
puts "Driver #{avg_rating_hash.key(avg_rating_hash.values.max)} has the highest average rating of #{avg_rating_hash.values.max} stars." |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Great work formatting this data structure for readability.