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
Earth-Anya #54
Open
anyatokar
wants to merge
3
commits into
Ada-C14:master
Choose a base branch
from
anyatokar: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
Earth-Anya #54
Changes from all commits
Commits
Show all changes
3 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 |
---|---|---|
|
@@ -2,17 +2,23 @@ | |
# Step 1: Establish the layers | ||
|
||
# In this section of the file, as a series of comments, | ||
# create a list of the layers you identify. | ||
# create a list of the layers you identify: | ||
#driver_id, rides, date, cost, rider_id, rating | ||
# Which layers are nested in each other? | ||
# driver id < rides < date - cost - rider_id - rating | ||
# Which layers of data "have" within it a different layer? | ||
# each ride has ride-specific info within it | ||
# Which layers are "next" to each other? | ||
# driver_ids and date - cost - rider_id - rating | ||
|
||
######################################################## | ||
# 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 | ||
|
||
# determine what data structure each layer should have: | ||
# drivers_hash = hash | ||
# each driver_id(key) within it contains array(value) of | ||
# hashes which contain ride-specific info | ||
######################################################## | ||
# Step 3: Make the data structure! | ||
|
||
|
@@ -22,13 +28,150 @@ | |
# You should be copying and pasting the literal data | ||
# into this data structure, such as "DR0004" | ||
# and "3rd Feb 2016" and "RD0022" | ||
# | ||
drivers_hash = { | ||
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 Driver's Earnings and Number of Rides | ||
def line | ||
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. Nice choice to put this in a method :) 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. hrmm, but I don't see where it's called. |
||
puts "########################################################" | ||
end | ||
|
||
# the total amount of money each driver has made ----------------------------------- | ||
def income_method(rides_array) | ||
return rides_array.reduce(0) { |sum, ride_hash| sum + ride_hash[:cost] } | ||
end | ||
|
||
# the average rating for each driver ----------------------------------- | ||
|
||
def average_rating_method(rides_array, num_of_rides) | ||
sum_ratings = rides_array.reduce(0) { |sum, ride_hash| | ||
sum + ride_hash[:rating] } | ||
return (sum_ratings / num_of_rides.to_f).round(1) # why can't this be included in the reduce block? | ||
end | ||
|
||
# Which driver made the most money? ----------------------------------- | ||
# this returns the first value, does not handle ties. | ||
def highest_income_method(tallies_array_of_hashes) | ||
return tallies_array_of_hashes.max_by { |tally_hash| tally_hash[:income] }[:driver_id] | ||
end | ||
|
||
# Which driver has the highest average rating? ----------------------------------- | ||
# this returns the first value, does not handle ties. | ||
def highest_average_rating_method(tallies_array_of_hashes) | ||
return tallies_array_of_hashes.max_by { |tally_hash| tally_hash[:average_rating] }[:driver_id] | ||
end | ||
|
||
# For each driver, on which day did they make the most money? ----------------------------------- | ||
def date_highest_income_method(rides_array) | ||
rides_array.max_by { |ride_hash| ride_hash[:cost] }[:date] | ||
end | ||
|
||
# runs all the methods ----------------------------------- | ||
def summary(drivers_hash) | ||
tallies_array_of_hashes = Array.new | ||
drivers_hash.each do |driver_id, rides_array| | ||
|
||
num_of_rides = (drivers_hash[driver_id]).length # the number of rides each driver has given ----------------------------------- | ||
income = income_method(rides_array) | ||
average_rating = average_rating_method(rides_array, num_of_rides) | ||
date_highest_income = date_highest_income_method(rides_array) | ||
|
||
tallies_hash = { | ||
date_highest_income: date_highest_income, | ||
driver_id: driver_id, | ||
income: income, | ||
average_rating: average_rating | ||
} | ||
tallies_array_of_hashes << tallies_hash | ||
|
||
puts "Driver with id #{driver_id} gave #{num_of_rides} ride(s) and \ | ||
made $#{income}. | ||
On average, their rating was #{average_rating} stars. | ||
They made the most money on #{date_highest_income}." | ||
puts "------" | ||
end | ||
|
||
highest_income = highest_income_method(tallies_array_of_hashes) | ||
highest_average_rating = highest_average_rating_method(tallies_array_of_hashes) | ||
|
||
# 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? | ||
puts "Driver with id #{highest_income} made the most money." | ||
puts "Driver with id #{highest_average_rating} \ | ||
had the highest rating, on average." | ||
end | ||
summary(drivers_hash) |
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.